Class: LibPixel::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/libpixel/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
# File 'lib/libpixel/client.rb', line 8

def initialize(options={})
  options.each do |key, value|
    send("#{key}=", value)
  end

  self.host   ||= ENV["LIBPIXEL_HOST"]
  self.secret ||= ENV["LIBPIXEL_SECRET"]
end

Instance Attribute Details

#default_sourceObject

Returns the value of attribute default_source.



6
7
8
# File 'lib/libpixel/client.rb', line 6

def default_source
  @default_source
end

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/libpixel/client.rb', line 6

def host
  @host
end

#httpsObject

Returns the value of attribute https.



6
7
8
# File 'lib/libpixel/client.rb', line 6

def https
  @https
end

#secretObject

Returns the value of attribute secret.



6
7
8
# File 'lib/libpixel/client.rb', line 6

def secret
  @secret
end

Instance Method Details

#sign(uri) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/libpixel/client.rb', line 17

def sign(uri)
  if secret.nil?
    raise "Your LibPixel secret must be defined (e.g. LibPixel.secret = 'SECRET')"
  end

  uri = URI.parse(uri) unless uri.kind_of?(URI::Generic)

  query = uri.query
  data = uri.path
  data += "?#{query}" if query && query != ""

  digest = OpenSSL::Digest.new("sha1")
  signature = OpenSSL::HMAC.hexdigest(digest, secret, data)

  query += "&" if query && query != ""
  query = "#{query}signature=#{signature}"
  uri.query = query

  uri.to_s
end

#url(path_or_options, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/libpixel/client.rb', line 38

def url(path_or_options, options={})
  path = nil
  
  if host.nil?
    raise "Your LibPixel host name must be defined (e.g. LibPixel.host = 'example.libpx.com')"
  end

  if path_or_options.respond_to? :fetch
    options = path_or_options
  elsif !path_or_options.nil?
    uri = URI(path_or_options)
    if uri.scheme == "http" || uri.scheme == "https"
      options[:src] = path_or_options
    else
      path = path_or_options
    end
  end

  source = options.fetch(:source) {default_source}
  options = options.reject {|k| k == :source}
  
  use_https = options.fetch(:https) {https}
  options = options.reject {|k| k == :https}

  query = options.map { |k,v| "#{k}=#{URI.encode_www_form_component(v)}" }.join("&")

  if query == ""
    query = nil
  end
  
  if !source.nil? && !source.empty?
    source_clean = source.gsub(/^\//, "").gsub(/\/$/, "")
    path_clean = (path || "").gsub(/^\//, "")
    path = "/#{source_clean}/#{path_clean}"
  else
    if path.nil? || path !~ /^\//
      path = "/#{path}"
    end
  end

  uri = URI::Generic.new(
    (use_https ? "https" : "http"), nil, host, nil, nil, path, nil, query, nil
  )

  if secret
    sign(uri)
  else
    uri.to_s
  end
end