Class: ApiSignature::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/api_signature/builder.rb

Constant Summary collapse

SPLITTER =
"\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#settingsObject (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

#bodyObject



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_sha256Object



40
41
42
# File 'lib/api_signature/builder.rb', line 40

def content_sha256
  @content_sha256 ||= Utils.sha256_hexdigest(body)
end

#dateObject



36
37
38
# File 'lib/api_signature/builder.rb', line 36

def date
  @date ||= datetime.to_s.scan(/\d/).take(8).join
end

#datetimeObject



32
33
34
# File 'lib/api_signature/builder.rb', line 32

def datetime
  @datetime ||= extract_datetime
end

#full_headersObject



57
58
59
# File 'lib/api_signature/builder.rb', line 57

def full_headers
  @full_headers ||= merge_sign_with_origin_headers
end

#headersObject



28
29
30
# File 'lib/api_signature/builder.rb', line 28

def headers
  @headers ||= Utils.normalize_keys(settings[:headers])
end

#hostObject



24
25
26
# File 'lib/api_signature/builder.rb', line 24

def host
  @host ||= extract_host_from_uri
end

#http_methodObject



16
17
18
# File 'lib/api_signature/builder.rb', line 16

def http_method
  @http_method ||= extract_http_method
end

#signed_headersObject



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_namesObject



65
66
67
# File 'lib/api_signature/builder.rb', line 65

def signed_headers_names
  @signed_headers_names ||= signed_headers.keys.sort.join(';')
end

#uriObject



20
21
22
# File 'lib/api_signature/builder.rb', line 20

def uri
  @uri ||= extract_uri
end