Class: ApiSignature::Builder
- Inherits:
-
Object
- Object
- ApiSignature::Builder
- Defined in:
- lib/api_signature/builder.rb
Constant Summary collapse
- SPLITTER =
"\n"
Instance Attribute Summary collapse
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
Instance Method Summary collapse
- #body ⇒ Object
- #build_sign_headers(apply_checksum_header = false) ⇒ Object
- #canonical_request(path) ⇒ Object
- #content_sha256 ⇒ Object
- #date ⇒ Object
- #datetime ⇒ Object
- #full_headers ⇒ Object
- #headers ⇒ Object
- #host ⇒ Object
- #http_method ⇒ Object
-
#initialize(settings = {}, unsigned_headers = []) ⇒ Builder
constructor
A new instance of Builder.
- #signed_headers ⇒ Object
- #signed_headers_names ⇒ Object
- #uri ⇒ Object
Constructor Details
#initialize(settings = {}, unsigned_headers = []) ⇒ Builder
Returns a new instance of Builder.
11 12 13 14 |
# File 'lib/api_signature/builder.rb', line 11 def initialize(settings = {}, unsigned_headers = []) @settings = settings @unsigned_headers = unsigned_headers end |
Instance Attribute Details
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
7 8 9 |
# File 'lib/api_signature/builder.rb', line 7 def settings @settings end |
Instance Method Details
#body ⇒ Object
44 45 46 |
# File 'lib/api_signature/builder.rb', line 44 def body @body ||= (settings[:body] || '') end |
#build_sign_headers(apply_checksum_header = false) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/api_signature/builder.rb', line 48 def build_sign_headers(apply_checksum_header = false) @sign_headers = { 'host' => host, 'x-datetime' => datetime } @sign_headers['x-content-sha256'] = content_sha256 if apply_checksum_header @sign_headers end |
#canonical_request(path) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/api_signature/builder.rb', line 69 def canonical_request(path) [ http_method, path, Utils.normalized_querystring(uri.query), canonical_headers + SPLITTER, signed_headers_names, content_sha256 ].join(SPLITTER) end |
#content_sha256 ⇒ Object
40 41 42 |
# File 'lib/api_signature/builder.rb', line 40 def content_sha256 @content_sha256 ||= Utils.sha256_hexdigest(body) end |
#date ⇒ Object
36 37 38 |
# File 'lib/api_signature/builder.rb', line 36 def date @date ||= datetime.to_s.scan(/\d/).take(8).join end |
#datetime ⇒ Object
32 33 34 |
# File 'lib/api_signature/builder.rb', line 32 def datetime @datetime ||= extract_datetime end |
#full_headers ⇒ Object
57 58 59 |
# File 'lib/api_signature/builder.rb', line 57 def full_headers @full_headers ||= merge_sign_with_origin_headers end |
#headers ⇒ Object
28 29 30 |
# File 'lib/api_signature/builder.rb', line 28 def headers @headers ||= Utils.normalize_keys(settings[:headers]) end |
#host ⇒ Object
24 25 26 |
# File 'lib/api_signature/builder.rb', line 24 def host @host ||= extract_host_from_uri end |
#http_method ⇒ Object
16 17 18 |
# File 'lib/api_signature/builder.rb', line 16 def http_method @http_method ||= extract_http_method end |
#signed_headers ⇒ Object
61 62 63 |
# File 'lib/api_signature/builder.rb', line 61 def signed_headers @signed_headers ||= full_headers.reject { |key, _value| @unsigned_headers.include?(key) } end |
#signed_headers_names ⇒ Object
65 66 67 |
# File 'lib/api_signature/builder.rb', line 65 def signed_headers_names @signed_headers_names ||= signed_headers.keys.sort.join(';') end |
#uri ⇒ Object
20 21 22 |
# File 'lib/api_signature/builder.rb', line 20 def uri @uri ||= extract_uri end |