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
# File 'lib/libpixel/client.rb', line 8

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

Instance Attribute Details

#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



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/libpixel/client.rb', line 14

def sign(uri)
  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, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/libpixel/client.rb', line 31

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

  if query == ""
    query = nil
  end

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

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