Class: Hyperb::Request
- Inherits:
-
Object
- Object
- Hyperb::Request
- Defined in:
- lib/hyperb/request.rb
Overview
wraps all requests, performing aws4 signature
Constant Summary collapse
- FMT =
'%Y%m%dT%H%M%SZ'.freeze
- VERSION =
'v1.23'.freeze
- HOST =
'us-west-1.hyper.sh'.freeze
- REGION =
'us-west-1'.freeze
- SERVICE =
'hyper'.freeze
- ALGORITHM =
'HYPER-HMAC-SHA256'.freeze
- KEYPARTS_REQUEST =
'hyper_request'.freeze
- BASE_URL =
"https://#{HOST}/".freeze
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#date ⇒ Object
Returns the value of attribute date.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#path ⇒ Object
Returns the value of attribute path.
-
#signed ⇒ Object
Returns the value of attribute signed.
-
#verb ⇒ Object
Returns the value of attribute verb.
Instance Method Summary collapse
-
#canonical_headers ⇒ Object
sorts all headers, join them by ‘:`, and re-join by n ie: content-type:applicationnhost:us-west-1.hyper.sh.
- #canonical_request ⇒ Object
- #credential_scope ⇒ Object
- #fail_or_return(code, body) ⇒ Object
- #hexdigest(value) ⇒ Object
- #hexhmac(key, value) ⇒ Object
- #hmac(key, value) ⇒ Object
-
#initialize(client, path, query = {}, verb = 'GET', body = '', optional_headers = {}) ⇒ Request
constructor
A new instance of Request.
- #perform ⇒ Object
-
#sign ⇒ Object
creates Authoriatization header.
-
#signature ⇒ Object
setup signature key docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/index.html.
-
#signed_headers ⇒ Object
join all headers by ‘;` ie: content-type;x-hyper-hmac-sha256.
- #string_to_sign ⇒ Object
Constructor Details
#initialize(client, path, query = {}, verb = 'GET', body = '', optional_headers = {}) ⇒ Request
Returns a new instance of Request.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/hyperb/request.rb', line 23 def initialize(client, path, query = {}, verb = 'GET', body = '', optional_headers = {}) @client = client @path = VERSION + path @query = URI.encode_www_form(query) @body = body.empty? ? body : body.to_json @hashed_body = hexdigest(@body) @verb = verb.upcase @date = Time.now.utc.strftime(FMT) @headers = { content_type: 'application/json', x_hyper_date: @date, host: HOST, x_hyper_content_sha256: @hashed_body } @headers.merge!(optional_headers) unless optional_headers.empty? @signed = false end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
21 22 23 |
# File 'lib/hyperb/request.rb', line 21 def client @client end |
#date ⇒ Object
Returns the value of attribute date.
21 22 23 |
# File 'lib/hyperb/request.rb', line 21 def date @date end |
#headers ⇒ Object
Returns the value of attribute headers.
21 22 23 |
# File 'lib/hyperb/request.rb', line 21 def headers @headers end |
#path ⇒ Object
Returns the value of attribute path.
21 22 23 |
# File 'lib/hyperb/request.rb', line 21 def path @path end |
#signed ⇒ Object
Returns the value of attribute signed.
21 22 23 |
# File 'lib/hyperb/request.rb', line 21 def signed @signed end |
#verb ⇒ Object
Returns the value of attribute verb.
21 22 23 |
# File 'lib/hyperb/request.rb', line 21 def verb @verb end |
Instance Method Details
#canonical_headers ⇒ Object
sorts all headers, join them by ‘:`, and re-join by n ie: content-type:applicationnhost:us-west-1.hyper.sh
66 67 68 69 70 71 |
# File 'lib/hyperb/request.rb', line 66 def canonical_headers canonical = @headers.sort.map do |header, value| "#{header.to_s.tr('_', '-')}:#{value}" end canonical.join("\n") + "\n" end |
#canonical_request ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/hyperb/request.rb', line 97 def canonical_request [ @verb, @path, @query, canonical_headers, signed_headers, @hashed_body ].join("\n") end |
#credential_scope ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'lib/hyperb/request.rb', line 108 def credential_scope [ @date[0, 8], REGION, SERVICE, KEYPARTS_REQUEST ].join("/") # rubocop:disable StringLiterals end |
#fail_or_return(code, body) ⇒ Object
50 51 52 53 54 |
# File 'lib/hyperb/request.rb', line 50 def fail_or_return(code, body) error = Hyperb::Error::ERRORS[code] raise(error.new(body, code)) if error body end |
#hexdigest(value) ⇒ Object
117 118 119 |
# File 'lib/hyperb/request.rb', line 117 def hexdigest(value) Digest::SHA256.new.update(value).hexdigest end |
#hexhmac(key, value) ⇒ Object
125 126 127 |
# File 'lib/hyperb/request.rb', line 125 def hexhmac(key, value) OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), key, value) end |
#hmac(key, value) ⇒ Object
121 122 123 |
# File 'lib/hyperb/request.rb', line 121 def hmac(key, value) OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), key, value) end |
#perform ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/hyperb/request.rb', line 41 def perform sign unless signed final = BASE_URL + @path + '?' + @query = {} [:body] = @body unless @body.empty? response = HTTP.headers(@headers).public_send(@verb.downcase.to_sym, final, ) fail_or_return(response.code, response.body) end |
#sign ⇒ Object
creates Authoriatization header
74 75 76 77 78 79 80 81 |
# File 'lib/hyperb/request.rb', line 74 def sign credential = "#{@client.access_key}/#{credential_scope}" auth = "#{ALGORITHM} Credential=#{credential}, " auth += "SignedHeaders=#{signed_headers}, " auth += "Signature=#{signature}" @headers[:authorization] = auth @signed = true end |
#signature ⇒ Object
setup signature key docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/index.html
85 86 87 88 89 90 91 |
# File 'lib/hyperb/request.rb', line 85 def signature k_date = hmac('HYPER' + @client.secret_key, @date[0, 8]) k_region = hmac(k_date, REGION) k_service = hmac(k_region, SERVICE) k_credentials = hmac(k_service, KEYPARTS_REQUEST) hexhmac(k_credentials, string_to_sign) end |
#signed_headers ⇒ Object
join all headers by ‘;` ie: content-type;x-hyper-hmac-sha256
59 60 61 |
# File 'lib/hyperb/request.rb', line 59 def signed_headers @headers.keys.sort.map { |header| header.to_s.tr('_', '-') }.join(';') end |
#string_to_sign ⇒ Object
93 94 95 |
# File 'lib/hyperb/request.rb', line 93 def string_to_sign [ALGORITHM, @date, credential_scope, hexdigest(canonical_request)].join("\n") end |