Class: LibPixel::Client
- Inherits:
-
Object
- Object
- LibPixel::Client
- Defined in:
- lib/libpixel/client.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#https ⇒ Object
Returns the value of attribute https.
-
#secret ⇒ Object
Returns the value of attribute secret.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #sign(uri) ⇒ Object
- #url(path, options = {}) ⇒ Object
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(={}) .each do |key, value| send("#{key}=", value) end end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
6 7 8 |
# File 'lib/libpixel/client.rb', line 6 def host @host end |
#https ⇒ Object
Returns the value of attribute https.
6 7 8 |
# File 'lib/libpixel/client.rb', line 6 def https @https end |
#secret ⇒ Object
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, ={}) query = .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 |