Class: SageoneApiSigner
- Inherits:
-
Object
- Object
- SageoneApiSigner
- Defined in:
- lib/sageone_api_signer.rb,
lib/sageone_api_signer/version.rb
Overview
Sign a Sage One API request call following the steps detailed here: developers.sageone.com/docs#signing_your_requests
Constant Summary collapse
- VERSION =
"1.3.0"
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#body_params ⇒ Object
Returns the value of attribute body_params.
-
#nonce ⇒ Object
Returns the value of attribute nonce.
-
#request_method ⇒ Object
Returns the value of attribute request_method.
-
#signing_secret ⇒ Object
Returns the value of attribute signing_secret.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#base_url ⇒ Object
Return the base URL without query string and fragment.
-
#initialize(params = {}) ⇒ SageoneApiSigner
constructor
A new instance of SageoneApiSigner.
- #parameter_string ⇒ Object
-
#request_headers(user_agent) ⇒ Object
The request headers.
-
#signature ⇒ Object
generate a Base64 encoded signature.
- #signature_base_string ⇒ Object
- #signing_key ⇒ Object
- #uri ⇒ Object
- #url_params ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ SageoneApiSigner
Returns a new instance of SageoneApiSigner.
12 13 14 15 16 |
# File 'lib/sageone_api_signer.rb', line 12 def initialize(params = {}) params.each do |attr, val| self.public_send("#{attr}=", val) end end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
10 11 12 |
# File 'lib/sageone_api_signer.rb', line 10 def access_token @access_token end |
#body_params ⇒ Object
Returns the value of attribute body_params.
10 11 12 |
# File 'lib/sageone_api_signer.rb', line 10 def body_params @body_params end |
#nonce ⇒ Object
Returns the value of attribute nonce.
10 11 12 |
# File 'lib/sageone_api_signer.rb', line 10 def nonce @nonce end |
#request_method ⇒ Object
Returns the value of attribute request_method.
10 11 12 |
# File 'lib/sageone_api_signer.rb', line 10 def request_method @request_method end |
#signing_secret ⇒ Object
Returns the value of attribute signing_secret.
10 11 12 |
# File 'lib/sageone_api_signer.rb', line 10 def signing_secret @signing_secret end |
#url ⇒ Object
Returns the value of attribute url.
10 11 12 |
# File 'lib/sageone_api_signer.rb', line 10 def url @url end |
Instance Method Details
#base_url ⇒ Object
Return the base URL without query string and fragment
31 32 33 34 35 36 37 38 39 |
# File 'lib/sageone_api_signer.rb', line 31 def base_url @base_url ||= [ uri.scheme, '://', uri.host, uri_port_string, uri.path ].join end |
#parameter_string ⇒ Object
45 46 47 48 49 |
# File 'lib/sageone_api_signer.rb', line 45 def parameter_string @parameter_string ||= ( Hash[url_params.merge(body_params).sort].to_query.gsub('+','%20') ) end |
#request_headers(user_agent) ⇒ Object
The request headers
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/sageone_api_signer.rb', line 73 def request_headers(user_agent) { 'Authorization' => "Bearer #{access_token}", 'X-Nonce' => nonce, 'X-Signature' => signature, 'Accept' => '*/*', 'Content-Type' => 'application/x-www-form-urlencoded', 'User-Agent' => user_agent } end |
#signature ⇒ Object
generate a Base64 encoded signature
68 69 70 |
# File 'lib/sageone_api_signer.rb', line 68 def signature @signature ||= Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha1'), signing_key, signature_base_string)) end |
#signature_base_string ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/sageone_api_signer.rb', line 51 def signature_base_string @signature_base_string ||= [ request_method, percent_encode(base_url), percent_encode(parameter_string), percent_encode(nonce) ].join('&') end |
#signing_key ⇒ Object
60 61 62 63 64 65 |
# File 'lib/sageone_api_signer.rb', line 60 def signing_key @signing_key ||= [ percent_encode(signing_secret), percent_encode(access_token) ].join('&') end |
#uri ⇒ Object
26 27 28 |
# File 'lib/sageone_api_signer.rb', line 26 def uri @uri ||= URI(url) end |
#url_params ⇒ Object
41 42 43 |
# File 'lib/sageone_api_signer.rb', line 41 def url_params @url_params ||= Hash[URI::decode_www_form(uri.query || '')] end |