Class: UriSigner::RequestParser

Inherits:
Object
  • Object
show all
Defined in:
lib/uri_signer/request_parser.rb

Overview

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)

Examples:

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"

Instance Method Summary collapse

Constructor Details

#initialize(http_method, raw_uri) ⇒ void

Create a new RequestParser instance

Parameters:

  • http_method (String)

    The HTTP method used to make the request (GET, POST, PUT, or DELETE)

  • raw_uri (String)

    The raw URI from the request

Raises:



45
46
47
48
49
50
51
52
53
# 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

Instance Method Details

#base_uriString

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

Returns:



108
109
110
# 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?Bool

Returns true if the scheme/protocol used was HTTP

Returns:

  • (Bool)


72
73
74
# File 'lib/uri_signer/request_parser.rb', line 72

def http?
  !self.https?
end

#http_methodString

Returns the uppercased HTTP Method

Returns:



58
59
60
# File 'lib/uri_signer/request_parser.rb', line 58

def http_method
  @http_method.upcase
end

#https?Bool

Returns true if the scheme/protocol used was HTTPS

Returns:

  • (Bool)


65
66
67
# File 'lib/uri_signer/request_parser.rb', line 65

def https?
  'https' == self.parsed_uri.scheme.downcase
end

#parsed_uriAddressable

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

Returns:

  • (Addressable)


87
88
89
# 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_paramsHash

Returns the query params with the core params removed

Returns:



94
95
96
# File 'lib/uri_signer/request_parser.rb', line 94

def query_params
  @query_params ||= raw_query_params
end

#query_params?Bool

Returns true if query params were given

Returns:

  • (Bool)


101
102
103
# File 'lib/uri_signer/request_parser.rb', line 101

def query_params?
  !self.query_params.blank?
end

#raw_uriString

Returns the raw_uri that was provided in the constructor

Returns:



79
80
81
# File 'lib/uri_signer/request_parser.rb', line 79

def raw_uri
  @raw_uri
end

#signatureString

This returns the signature that was provided in the query params

Returns:



115
116
117
# File 'lib/uri_signer/request_parser.rb', line 115

def signature
  @_signature.extend(UriSigner::Helpers::String).escaped
end

#signature?Bool

Returns true if a signature was provided in the raw_uri

Returns:

  • (Bool)


122
123
124
# File 'lib/uri_signer/request_parser.rb', line 122

def signature?
  !@_signature.blank?
end