Class: LedgerSync::NetSuite::Client

Inherits:
Object
  • Object
show all
Includes:
Ledgers::Client::Mixin
Defined in:
lib/ledger_sync/netsuite/client.rb

Constant Summary collapse

HEADERS =
{
  # 'Accept' => 'application/schema+json'
}.freeze
WRITE_HEADERS =
{
  'Accept' => '*/*',
  'Content-Type' => 'application/json',
  'prefer' => 'transient'
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Client

Returns a new instance of Client.



26
27
28
29
30
31
32
33
34
# File 'lib/ledger_sync/netsuite/client.rb', line 26

def initialize(args = {})
  @account_id = args.fetch(:account_id)
  @consumer_key = args.fetch(:consumer_key)
  @consumer_secret = args.fetch(:consumer_secret)
  @token_id = args.fetch(:token_id)
  @token_secret = args.fetch(:token_secret)

  super()
end

Instance Attribute Details

#account_idObject (readonly)

Returns the value of attribute account_id.



20
21
22
# File 'lib/ledger_sync/netsuite/client.rb', line 20

def 
  @account_id
end

#consumer_keyObject (readonly)

Returns the value of attribute consumer_key.



20
21
22
# File 'lib/ledger_sync/netsuite/client.rb', line 20

def consumer_key
  @consumer_key
end

#consumer_secretObject (readonly)

Returns the value of attribute consumer_secret.



20
21
22
# File 'lib/ledger_sync/netsuite/client.rb', line 20

def consumer_secret
  @consumer_secret
end

#token_idObject (readonly)

Returns the value of attribute token_id.



20
21
22
# File 'lib/ledger_sync/netsuite/client.rb', line 20

def token_id
  @token_id
end

#token_secretObject (readonly)

Returns the value of attribute token_secret.



20
21
22
# File 'lib/ledger_sync/netsuite/client.rb', line 20

def token_secret
  @token_secret
end

Class Method Details

.ledger_attributes_to_saveObject



102
103
104
# File 'lib/ledger_sync/netsuite/client.rb', line 102

def self.ledger_attributes_to_save
  %i[]
end

.ledger_resource_type_overridesObject



125
126
127
128
129
130
131
# File 'lib/ledger_sync/netsuite/client.rb', line 125

def self.ledger_resource_type_overrides
  {
    CustomerDeposit => 'customerdeposit',
    LedgerClass => 'classification',
    JournalEntry => 'journalEntry'
  }
end

.new_from_env(**override) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ledger_sync/netsuite/client.rb', line 106

def self.new_from_env(**override)
  new(
    {
      account_id: ENV.fetch('NETSUITE_ACCOUNT_ID', nil),
      consumer_key: ENV.fetch('NETSUITE_CONSUMER_KEY', nil),
      consumer_secret: ENV.fetch('NETSUITE_CONSUMER_SECRET', nil),
      token_id: ENV.fetch('NETSUITE_TOKEN_ID', nil),
      token_secret: ENV.fetch('NETSUITE_TOKEN_SECRET', nil)
    }.merge(override)
  )
end

Instance Method Details

#account_id_for_oauthObject



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

def 
  .gsub('-', '_').upcase
end

#account_id_for_urlObject



40
41
42
# File 'lib/ledger_sync/netsuite/client.rb', line 40

def 
  .gsub('_', '-').downcase
end

#api_base_urlObject



44
45
46
47
48
49
50
51
# File 'lib/ledger_sync/netsuite/client.rb', line 44

def api_base_url
  @api_base_url ||= URI::HTTPS.build(
    host: api_host,
    path: '/services/rest/record/v1'
  ).to_s
rescue URI::InvalidComponentError => e
  raise LedgerSync::Error::LedgerError::ConfigurationError.new(client: self, message: e.message)
end

#api_hostObject



53
54
55
# File 'lib/ledger_sync/netsuite/client.rb', line 53

def api_host
  @api_host ||= "#{}.suitetalk.api.netsuite.com"
end

#delete(**keywords) ⇒ Object



57
58
59
# File 'lib/ledger_sync/netsuite/client.rb', line 57

def delete(**keywords)
  request(keywords.merge(method: :delete))
end

#get(**keywords) ⇒ Object



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

def get(**keywords)
  request(keywords.merge(method: :get))
end

#ledger_resource_path(args = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ledger_sync/netsuite/client.rb', line 65

def ledger_resource_path(args = {})
  resource = args.fetch(:resource, nil)
  params   = args.fetch(:params, {})

  ret = self.class.ledger_resource_type_for(resource_class: resource.class)
  ret += "/#{resource.ledger_id}" if resource.ledger_id.present? && args.fetch(:id, true)
  Util::URLHelpers.merge_params_in_path(
    path: ret,
    params: params
  )
end

#metadata_for(record:) ⇒ Object



77
78
79
80
81
82
# File 'lib/ledger_sync/netsuite/client.rb', line 77

def (record:)
  Record::Metadata.new(
    client: self,
    record: record
  )
end

#patch(headers: {}, **keywords) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/ledger_sync/netsuite/client.rb', line 84

def patch(headers: {}, **keywords)
  request(
    keywords.merge(
      headers: WRITE_HEADERS.merge(headers),
      method: :patch
    )
  )
end

#post(headers: {}, **keywords) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/ledger_sync/netsuite/client.rb', line 93

def post(headers: {}, **keywords)
  request(
    keywords.merge(
      headers: WRITE_HEADERS.merge(headers),
      method: :post
    )
  )
end

#url_for(resource:) ⇒ Object



118
119
120
121
122
123
# File 'lib/ledger_sync/netsuite/client.rb', line 118

def url_for(resource:)
  DashboardURLHelper.new(
    resource: resource,
    base_url: "https://#{}.app.netsuite.com"
  ).url
end