class Tinplate::RequestAuthenticator

Public Class Methods

new(action, params = {}, image_name = "") click to toggle source
# File lib/tinplate/request_authenticator.rb, line 10
def initialize(action, params = {}, image_name = "")
  @action = action
  @params = params
  @image_name = image_name || ""
  @nonce = SecureRandom.hex
  @date  = Time.now.to_i
end

Public Instance Methods

content_type() click to toggle source
# File lib/tinplate/request_authenticator.rb, line 31
def content_type
  verb == "GET" ? "" : "multipart/form-data; boundary=-----------RubyMultipartPost"
end
hash_to_sorted_query_string(params) click to toggle source
# File lib/tinplate/request_authenticator.rb, line 54
def hash_to_sorted_query_string(params)
  Hash[params.sort].map do |key, value|
    "#{key}=#{URI.encode_www_form_component(value)}"
  end.join("&")
end
params() click to toggle source
# File lib/tinplate/request_authenticator.rb, line 18
def params
  {
    api_key: Tinplate.configuration.public_key,
    api_sig: signature,
    nonce:   @nonce,
    date:    @date
  }
end
signature() click to toggle source
# File lib/tinplate/request_authenticator.rb, line 48
def signature
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"),
                          Tinplate.configuration.private_key,
                          signature_components.join)
end
signature_components() click to toggle source
# File lib/tinplate/request_authenticator.rb, line 35
def signature_components
  [
    Tinplate.configuration.private_key,
    verb,
    content_type,
    URI.encode_www_form_component(@image_name).downcase,
    @date.to_i,
    @nonce,
    "https://api.tineye.com/rest/#{@action}/",
    hash_to_sorted_query_string(@params),
  ]
end
verb() click to toggle source
# File lib/tinplate/request_authenticator.rb, line 27
def verb
  @image_name.empty? ? "GET" : "POST"
end