Class: UriSigner::RequestSignature
- Inherits:
-
Object
- Object
- UriSigner::RequestSignature
- Defined in:
- lib/uri_signer/request_signature.rb
Overview
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
Instance Attribute Summary collapse
-
#separator ⇒ Object
readonly
The default separator used to join the http_method, encoded_base_uri, and encoded_query_params.
Instance Method Summary collapse
-
#base_uri ⇒ String
Returns the base URI.
-
#encoded_base_uri ⇒ String
Returns the encoded base_uri.
-
#encoded_query_params ⇒ String
Returns the encoded query params as a string.
-
#http_method ⇒ String
Returns the uppercased HTTP Method.
-
#initialize(http_method, base_uri, query_params = {}) ⇒ void
constructor
Create a new RequestSignature instance.
-
#query_params ⇒ Hash
Returns the Query String parameters.
-
#query_params? ⇒ Bool
Returns true if query params were provided.
-
#signature ⇒ String
(also: #to_s)
Returns the full signature string.
Constructor Details
#initialize(http_method, base_uri, query_params = {}) ⇒ void
Create a new RequestSignature instance
It’s required that you provide at least the http_method and base_uri. Params are optional
44 45 46 47 48 49 50 51 52 |
# 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 |
Instance Attribute Details
#separator ⇒ Object (readonly)
The default separator used to join the http_method, encoded_base_uri, and encoded_query_params
33 34 35 |
# File 'lib/uri_signer/request_signature.rb', line 33 def separator @separator end |
Instance Method Details
#base_uri ⇒ String
Returns the base URI
74 75 76 |
# File 'lib/uri_signer/request_signature.rb', line 74 def base_uri @base_uri end |
#encoded_base_uri ⇒ String
Returns the encoded base_uri
This can be used for comparison to ensure the escaping is what you want
83 84 85 |
# 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 ⇒ String
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.
106 107 108 |
# File 'lib/uri_signer/request_signature.rb', line 106 def encoded_query_params query_params_string.extend(UriSigner::Helpers::String).escaped end |
#http_method ⇒ String
Returns the uppercased HTTP Method
67 68 69 |
# File 'lib/uri_signer/request_signature.rb', line 67 def http_method @http_method.upcase end |
#query_params ⇒ Hash
Returns the Query String parameters
90 91 92 |
# File 'lib/uri_signer/request_signature.rb', line 90 def query_params @query_params.extend(UriSigner::Helpers::Hash).stringify_keys end |
#query_params? ⇒ Bool
Returns true if query params were provided
97 98 99 |
# File 'lib/uri_signer/request_signature.rb', line 97 def query_params? !@query_params.blank? end |
#signature ⇒ String Also known as: to_s
Returns the full signature string
57 58 59 60 61 |
# 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 |