Class: LibPixel::Client
- Inherits:
-
Object
- Object
- LibPixel::Client
- Defined in:
- lib/libpixel/client.rb
Instance Attribute Summary collapse
-
#default_source ⇒ Object
Returns the value of attribute default_source.
-
#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_or_options, options = {}) ⇒ Object
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(={}) .each do |key, value| send("#{key}=", value) end self.host ||= ENV["LIBPIXEL_HOST"] self.secret ||= ENV["LIBPIXEL_SECRET"] end |
Instance Attribute Details
#default_source ⇒ Object
Returns the value of attribute default_source.
6 7 8 |
# File 'lib/libpixel/client.rb', line 6 def default_source @default_source end |
#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
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 = nil if host.nil? raise "Your LibPixel host name must be defined (e.g. LibPixel.host = 'example.libpx.com')" end if .respond_to? :fetch = elsif !.nil? uri = URI() if uri.scheme == "http" || uri.scheme == "https" [:src] = else path = end end source = .fetch(:source) {default_source} = .reject {|k| k == :source} use_https = .fetch(:https) {https} = .reject {|k| k == :https} query = .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 |