Class: LedgerSync::NetSuite::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/ledger_sync/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(args = {}) ⇒ Token

Returns a new instance of Token.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ledger_sync/netsuite/token.rb', line 20

def initialize(args = {})
  @consumer_key     = args.fetch(:consumer_key)
  @consumer_secret  = args.fetch(:consumer_secret)
  @method           = args.fetch(:method).to_s.upcase
  @nonce            = args.fetch(:nonce, nil)
  @oauth_version    = args.fetch(:oauth_version, nil) || self.class::OAUTH_VERSION
  @signature_method = args.fetch(:signature_method, nil) || self.class::DEFAULT_SIGNATURE_METHOD
  @timestamp        = args.fetch(:timestamp, nil)
  @token_id         = args.fetch(:token_id)
  @token_secret     = args.fetch(:token_secret)
  @url              = args.fetch(:url)
end

Instance Attribute Details

#consumer_keyObject (readonly)

Returns the value of attribute consumer_key.



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

def consumer_key
  @consumer_key
end

#consumer_secretObject (readonly)

Returns the value of attribute consumer_secret.



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

def consumer_secret
  @consumer_secret
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#oauth_versionObject (readonly)

Returns the value of attribute oauth_version.



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

def oauth_version
  @oauth_version
end

#signature_methodObject (readonly)

Returns the value of attribute signature_method.



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

def signature_method
  @signature_method
end

#token_idObject (readonly)

Returns the value of attribute token_id.



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

def token_id
  @token_id
end

#token_secretObject (readonly)

Returns the value of attribute token_secret.



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

def token_secret
  @token_secret
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#alphanumericsObject



33
34
35
# File 'lib/ledger_sync/netsuite/token.rb', line 33

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

#headers(realm:) ⇒ Object



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

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



58
59
60
# File 'lib/ledger_sync/netsuite/token.rb', line 58

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

#signatureObject



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

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

#signature_data_stringObject



67
68
69
70
71
72
73
# File 'lib/ledger_sync/netsuite/token.rb', line 67

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

#signature_keyObject



75
76
77
78
79
80
# File 'lib/ledger_sync/netsuite/token.rb', line 75

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

#timestampObject



82
83
84
# File 'lib/ledger_sync/netsuite/token.rb', line 82

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