class UriSigner::RequestSignature

This is responsible for preparing the raw request in the format necessary for signing. API URI signatures come in a few different flavors. This one will take HTTP_METHOD & ESCAPED_BASE_URI & SORTED_AND_ESCAPED_QUERY_PARAMS

@example

request_signature = UriSigner::RequestSignature.new('get', 'https://api.example.com/core/people.json', { 'page' => 5, 'per_page' => 25 })

request_signature.http_method
# => 'GET'

request_signature.base_uri
# => 'https://api.example.com/core/people.json'

request_signature.query_params
# => { 'page' => 5, 'per_page' => 25' }

request_signature.query_params?
# => true

request_signature.encoded_base_uri
# => "https%3A%2F%2Fapi.example.com%2Fcore%2Fpeople.json"

request_signature.encoded_query_params
# => "page%3D5%26per_page%3D25"

request_signature.signature
# => "GET&https%3A%2F%2Fapi.example.com%2Fcore%2Fpeople.json&page%3D5%26per_page%3D25"

Attributes

separator[R]

The default separator used to join the http_method, encoded_base_uri, and encoded_query_params

Public Class Methods

new(http_method, base_uri, query_params = {}) click to toggle source

Create a new RequestSignature instance

@param http_method [String] The HTTP method from the request (GET, POST, PUT, or DELETE) @param base_uri [String] The base URI of the request. This is everything except the query string params @param query_params [Hash] A hash of the provided query string params

It's required that you provide at least the http_method and base_uri. Params are optional

@return [void]

# File lib/uri_signer/request_signature.rb, line 44
def initialize(http_method, base_uri, query_params = {})
  @http_method  = http_method
  @base_uri     = base_uri
  @query_params = query_params
  @separator    = '&'

  raise UriSigner::Errors::MissingHttpMethodError.new("Please provide an HTTP method") unless http_method?
  raise UriSigner::Errors::MissingBaseUriError.new("Please provide a Base URI") unless base_uri?
end

Public Instance Methods

base_uri() click to toggle source

Returns the base URI

@return [String]

# File lib/uri_signer/request_signature.rb, line 74
def base_uri
  @base_uri
end
encoded_base_uri() click to toggle source

Returns the encoded base_uri

This can be used for comparison to ensure the escaping is what you want

@return [String] Escaped string of the base_uri

# File lib/uri_signer/request_signature.rb, line 83
def encoded_base_uri
  self.base_uri.extend(UriSigner::Helpers::String).escaped
end
encoded_query_params() click to toggle source

Returns the encoded query params as a string

This joins the keys and values in one string, then joins them. Then it will escape the final contents.

@return [String] Escaped string of the query params

# File lib/uri_signer/request_signature.rb, line 106
def encoded_query_params
  query_params_string.extend(UriSigner::Helpers::String).escaped
end
http_method() click to toggle source

Returns the uppercased HTTP Method

@return [String]

# File lib/uri_signer/request_signature.rb, line 67
def http_method
  @http_method.upcase
end
query_params() click to toggle source

Returns the Query String parameters

@return [Hash] The keys are stringified

# File lib/uri_signer/request_signature.rb, line 90
def query_params
  @query_params.extend(UriSigner::Helpers::Hash).stringify_keys
end
query_params?() click to toggle source

Returns true if query params were provided

@return [Bool]

# File lib/uri_signer/request_signature.rb, line 97
def query_params?
  !@query_params.blank?
end
signature() click to toggle source

Returns the full signature string

@return [String]

# File lib/uri_signer/request_signature.rb, line 57
def signature
  core_signature = [self.http_method, self.encoded_base_uri]
  core_signature << self.encoded_query_params if self.query_params?
  core_signature.join(self.separator)
end
Also aliased as: to_s
to_s()
Alias for: signature

Private Instance Methods

base_uri?() click to toggle source
# File lib/uri_signer/request_signature.rb, line 115
def base_uri?
  !@base_uri.blank?
end
http_method?() click to toggle source
# File lib/uri_signer/request_signature.rb, line 111
def http_method?
  !@http_method.blank?
end
query_params_string() click to toggle source
# File lib/uri_signer/request_signature.rb, line 119
def query_params_string
  @query_params_string ||= UriSigner::QueryHashParser.new(self.query_params).to_s
end