class UriSigner::RequestParser

This object takes the raw request from the inbound API call. It takes the http method used to make the request, and the full raw_uri of the request. This object extracts the pieces necessary to pass it to the signing class. The key components are http_method, base_uri, and query_params. query_params has the core params extracted (any param starting with an underscore)

@example

parser = UriSigner::RequestParser.new('get', 'https://api.example.com/core/people.json?_signature=1234&page=5&per_page=25')

parser.http_method
# => "GET"

parser.https?
# => true

parser.http?
# => false

parser.raw_uri
# => "https://api.example.com/core/people.json?_signature=1234&page=5&per_page=25

parser.query_params
# => {"page"=>"5", "per_page"=>"25"}

parser.query_params?
# => true

parser.signature
# => '1234'

parser.signature?
# => true

parser.base_uri
# => "https://api.example.com/core/people.json"

Public Class Methods

new(http_method, raw_uri) click to toggle source

Create a new RequestParser instance

@param http_method [String] The HTTP method used to make the request (GET, POST, PUT, or DELETE) @param raw_uri [String] The raw URI from the request

@return [void]

# File lib/uri_signer/request_parser.rb, line 45
def initialize(http_method, raw_uri)
  @http_method = http_method
  @raw_uri     = raw_uri

  raise UriSigner::Errors::MissingHttpMethodError.new("Please provide an HTTP method") unless http_method?
  raise UriSigner::Errors::MissingUriError.new("Please provide a URI") unless raw_uri?

  extract_core_params!
end

Public Instance Methods

base_uri() click to toggle source

Returns the base_uri of the request. This is the protocol, host with port, and the path.

@return [String]

# File lib/uri_signer/request_parser.rb, line 108
def base_uri
  [self.parsed_uri.normalized_site, self.parsed_uri.normalized_path].join('')
end
http?() click to toggle source

Returns true if the scheme/protocol used was HTTP

@return [Bool]

# File lib/uri_signer/request_parser.rb, line 72
def http?
  !self.https?
end
http_method() click to toggle source

Returns the uppercased HTTP Method

@return [String]

# File lib/uri_signer/request_parser.rb, line 58
def http_method
  @http_method.upcase
end
https?() click to toggle source

Returns true if the scheme/protocol used was HTTPS

@return [Bool]

# File lib/uri_signer/request_parser.rb, line 65
def https?
  'https' == self.parsed_uri.scheme.downcase
end
parsed_uri() click to toggle source

Returns an instance of Addressable::URI that has been parsed. This allows us to extract the core parts of the raw_uri

@return [Addressable]

# File lib/uri_signer/request_parser.rb, line 87
def parsed_uri
  @parsed_uri ||= self.raw_uri.extend(UriSigner::Helpers::String).to_parsed_uri
end
query_params() click to toggle source

Returns the query params with the core params removed

@return [Hash]

# File lib/uri_signer/request_parser.rb, line 94
def query_params
  @query_params ||= raw_query_params
end
query_params?() click to toggle source

Returns true if query params were given

@return [Bool]

# File lib/uri_signer/request_parser.rb, line 101
def query_params?
  !self.query_params.blank?
end
raw_uri() click to toggle source

Returns the raw_uri that was provided in the constructor

@return [String]

# File lib/uri_signer/request_parser.rb, line 79
def raw_uri
  @raw_uri
end
signature() click to toggle source

This returns the signature that was provided in the query params

@return [String]

# File lib/uri_signer/request_parser.rb, line 115
def signature
  @_signature.extend(UriSigner::Helpers::String).escaped
end
signature?() click to toggle source

Returns true if a signature was provided in the raw_uri

@return [Bool]

# File lib/uri_signer/request_parser.rb, line 122
def signature?
  !@_signature.blank?
end

Private Instance Methods

extract_core_params!() click to toggle source
# File lib/uri_signer/request_parser.rb, line 139
def extract_core_params!
  @_signature = raw_query_params.delete('_signature')
end
http_method?() click to toggle source
# File lib/uri_signer/request_parser.rb, line 127
def http_method?
  !@http_method.blank?
end
raw_query_params() click to toggle source
# File lib/uri_signer/request_parser.rb, line 135
def raw_query_params
  @raw_query_params ||= Rack::Utils.parse_query(self.parsed_uri.query)
end
raw_uri?() click to toggle source
# File lib/uri_signer/request_parser.rb, line 131
def raw_uri?
  !@raw_uri.blank?
end