Class: Tinplate::RequestAuthenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/tinplate/request_authenticator.rb

Instance Method Summary collapse

Constructor Details

#initialize(action, params = {}, image_name = "") ⇒ RequestAuthenticator

Returns a new instance of RequestAuthenticator.



6
7
8
9
10
11
12
# File 'lib/tinplate/request_authenticator.rb', line 6

def initialize(action, params = {}, image_name = "")
  @action = action
  @params = params
  @image_name = image_name
  @nonce = SecureRandom.hex
  @date  = Time.now.to_i
end

Instance Method Details

#hash_to_sorted_query_string(params) ⇒ Object



42
43
44
45
46
# File 'lib/tinplate/request_authenticator.rb', line 42

def hash_to_sorted_query_string(params)
  Hash[params.sort].map do |key, value|
    "#{key}=#{URI.encode_www_form_component(value)}"
  end.join("&")
end

#paramsObject



14
15
16
17
18
19
20
21
# File 'lib/tinplate/request_authenticator.rb', line 14

def params
  {
    api_key: Tinplate.configuration.public_key,
    api_sig: signature,
    nonce:   @nonce,
    date:    @date
  }
end

#signatureObject



36
37
38
39
40
# File 'lib/tinplate/request_authenticator.rb', line 36

def signature
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha1"),
                          Tinplate.configuration.private_key,
                          signature_components.join)
end

#signature_componentsObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/tinplate/request_authenticator.rb', line 23

def signature_components
  [
    Tinplate.configuration.private_key,
    "GET",
    "", # Content-Type for GET requests is blank
    URI.encode(@image_name).downcase,
    @date.to_i,
    @nonce,
    "http://api.tineye.com/rest/#{@action}/",
    hash_to_sorted_query_string(@params),
  ]
end