Class: LedgerSync::Adaptors::NetSuite::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/ledger_sync/adaptors/netsuite/token.rb

Constant Summary collapse

DEFAULT_SIGNATURE_METHOD =
'HMAC-SHA256'
OAUTH_VERSION =
'1.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body: {}, consumer_key:, consumer_secret:, method:, nonce: nil, signature_method: nil, timestamp: nil, token_id:, token_secret:, url:) ⇒ Token

Returns a new instance of Token.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ledger_sync/adaptors/netsuite/token.rb', line 22

def initialize(body: {}, consumer_key:, consumer_secret:, method:, nonce: nil, signature_method: nil, timestamp: nil, token_id:, token_secret:, url:)
  @body = body || {}
  @body_json_string = body.to_json
  @consumer_key = consumer_key
  @consumer_secret = consumer_secret
  @method = method.to_s.upcase
  @nonce = nonce
  @signature_method = signature_method || DEFAULT_SIGNATURE_METHOD
  @timestamp = timestamp
  @token_id = token_id
  @token_secret = token_secret
  @url = url
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



12
13
14
# File 'lib/ledger_sync/adaptors/netsuite/token.rb', line 12

def body
  @body
end

#body_json_stringObject (readonly)

Returns the value of attribute body_json_string.



12
13
14
# File 'lib/ledger_sync/adaptors/netsuite/token.rb', line 12

def body_json_string
  @body_json_string
end

#consumer_keyObject (readonly)

Returns the value of attribute consumer_key.



12
13
14
# File 'lib/ledger_sync/adaptors/netsuite/token.rb', line 12

def consumer_key
  @consumer_key
end

#consumer_secretObject (readonly)

Returns the value of attribute consumer_secret.



12
13
14
# File 'lib/ledger_sync/adaptors/netsuite/token.rb', line 12

def consumer_secret
  @consumer_secret
end

#methodObject (readonly)

Returns the value of attribute method.



12
13
14
# File 'lib/ledger_sync/adaptors/netsuite/token.rb', line 12

def method
  @method
end

#signature_methodObject (readonly)

Returns the value of attribute signature_method.



12
13
14
# File 'lib/ledger_sync/adaptors/netsuite/token.rb', line 12

def signature_method
  @signature_method
end

#token_idObject (readonly)

Returns the value of attribute token_id.



12
13
14
# File 'lib/ledger_sync/adaptors/netsuite/token.rb', line 12

def token_id
  @token_id
end

#token_secretObject (readonly)

Returns the value of attribute token_secret.



12
13
14
# File 'lib/ledger_sync/adaptors/netsuite/token.rb', line 12

def token_secret
  @token_secret
end

#urlObject (readonly)

Returns the value of attribute url.



12
13
14
# File 'lib/ledger_sync/adaptors/netsuite/token.rb', line 12

def url
  @url
end

Instance Method Details

#alphanumericsObject



36
37
38
# File 'lib/ledger_sync/adaptors/netsuite/token.rb', line 36

def alphanumerics
  [*'0'..'9', *'A'..'Z', *'a'..'z']
end

#headers(realm:) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ledger_sync/adaptors/netsuite/token.rb', line 40

def headers(realm:)
  @headers ||= begin
    authorization_parts = [
      [:realm, realm],
      [:oauth_consumer_key, escape(consumer_key)],
      [:oauth_token, escape(token_id)],
      [:oauth_signature_method, signature_method],
      [:oauth_timestamp, timestamp],
      [:oauth_nonce, escape(nonce)],
      [:oauth_version, OAUTH_VERSION],
      [:oauth_signature, escape(signature)]
    ]

    ret = {
      'Authorization' => "OAuth #{authorization_parts.map { |k, v| "#{k}=\"#{v}\"" }.join(',')}"
    }

    ret
  end
end

#nonceObject



61
62
63
# File 'lib/ledger_sync/adaptors/netsuite/token.rb', line 61

def nonce
  @nonce ||= Array.new(20) { alphanumerics.sample }.join
end

#signatureObject



65
66
67
# File 'lib/ledger_sync/adaptors/netsuite/token.rb', line 65

def signature
  @signature ||= compute_digest(signature_data_string)
end

#signature_data_stringObject



70
71
72
73
74
75
76
77
78
# File 'lib/ledger_sync/adaptors/netsuite/token.rb', line 70

def signature_data_string
  @signature_data_string ||= begin
    [
      method,
      escape(url_without_params),
      escape(parameters_string)
    ].join('&')
  end
end

#signature_keyObject



80
81
82
83
84
85
# File 'lib/ledger_sync/adaptors/netsuite/token.rb', line 80

def signature_key
  @signature_key ||= [
    consumer_secret,
    token_secret
  ].join('&')
end

#timestampObject



87
88
89
# File 'lib/ledger_sync/adaptors/netsuite/token.rb', line 87

def timestamp
  @timestamp ||= Time.now.to_i
end