Class: LedgerSync::Adaptors::QuickBooksOnline::Adaptor

Inherits:
Adaptor
  • Object
show all
Defined in:
lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb

Constant Summary collapse

AUTHORIZE_URL =
'https://appcenter.intuit.com/connect/oauth2'
OAUTH_HEADERS =
{ 'Accept' => 'application/json', 'Content-Type' => 'application/json' }.freeze
ROOT_URI =
'https://quickbooks.api.intuit.com'
ROOT_SANDBOX_URI =
'https://sandbox-quickbooks.api.intuit.com'
SITE_URL =
'https://appcenter.intuit.com/connect/oauth2'
TOKEN_URL =
'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Adaptor

#adaptor_configuration, #base_module, config, #ledger_attributes_to_save, #searcher_for?, #searcher_klass_for, url_for

Methods included from Validatable

#valid?, #validate, #validate_or_fail

Constructor Details

#initialize(access_token:, client_id:, client_secret:, realm_id:, refresh_token:, test: false) ⇒ Adaptor

Returns a new instance of Adaptor.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 28

def initialize(
  access_token:,
  client_id:,
  client_secret:,
  realm_id:,
  refresh_token:,
  test: false
)
  @access_token = access_token
  @client_id = client_id
  @client_secret = client_secret
  @previous_access_tokens = []
  @previous_refresh_tokens = []
  @realm_id = realm_id
  @refresh_token = refresh_token
  @test = test

  @root_uri = (test ? ROOT_SANDBOX_URI : ROOT_URI)
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



16
17
18
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 16

def access_token
  @access_token
end

#client_idObject (readonly)

Returns the value of attribute client_id.



16
17
18
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 16

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



16
17
18
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 16

def client_secret
  @client_secret
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



16
17
18
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 16

def expires_at
  @expires_at
end

#previous_access_tokensObject (readonly)

Returns the value of attribute previous_access_tokens.



16
17
18
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 16

def previous_access_tokens
  @previous_access_tokens
end

#previous_refresh_tokensObject (readonly)

Returns the value of attribute previous_refresh_tokens.



16
17
18
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 16

def previous_refresh_tokens
  @previous_refresh_tokens
end

#realm_idObject (readonly)

Returns the value of attribute realm_id.



16
17
18
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 16

def realm_id
  @realm_id
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



16
17
18
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 16

def refresh_token
  @refresh_token
end

#refresh_token_expires_atObject (readonly)

Returns the value of attribute refresh_token_expires_at.



16
17
18
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 16

def refresh_token_expires_at
  @refresh_token_expires_at
end

#root_uriObject (readonly)

Returns the value of attribute root_uri.



16
17
18
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 16

def root_uri
  @root_uri
end

#testObject (readonly)

Returns the value of attribute test.



16
17
18
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 16

def test
  @test
end

Class Method Details

.ledger_attributes_to_saveObject



136
137
138
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 136

def self.ledger_attributes_to_save
  i[access_token expires_at refresh_token refresh_token_expires_at]
end

Instance Method Details

#authorization_url(redirect_uri:) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 48

def authorization_url(redirect_uri:)
  oauth_client.auth_code.authorize_url(
    redirect_uri: redirect_uri,
    response_type: 'code',
    state: SecureRandom.hex(12),
    scope: 'com.intuit.quickbooks.accounting'
  )
end

#find(resource:, id:) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 57

def find(resource:, id:)
  resource = resource.to_s
  url = "#{oauth_base_uri}/#{resourcify(resource)}/#{id}"

  response = request(:get, url, headers: OAUTH_HEADERS.dup)
  JSON.parse(response.body).dig(resource.classify)
end

#parse_operation_error(error:, operation:) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 65

def parse_operation_error(error:, operation:)
  return nil unless error.is_a?(OAuth2::Error)

  Util::OperationErrorParser.new(
    error: error,
    operation: operation
  ).parse
end

#post(resource:, payload:) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 74

def post(resource:, payload:)
  resource = resource.to_s
  url = "#{oauth_base_uri}/#{resourcify(resource)}"

  response = request(:post, url, headers: OAUTH_HEADERS.dup, body: payload.to_json)
  JSON.parse(response.body).dig(resource.classify)
end

#query(resource:, query:, limit: 10, offset: 1) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 82

def query(resource:, query:, limit: 10, offset: 1)
  resource = resource.to_s
  full_query = "SELECT * FROM #{resource.classify} WHERE #{query} STARTPOSITION #{offset} MAXRESULTS #{limit}"
  url = "#{oauth_base_uri}/query?query=#{CGI.escape(full_query)}"

  response = request(:get, url, headers: OAUTH_HEADERS.dup)
  JSON.parse(response.body).dig('QueryResponse', resource.classify) || []
end

#refresh!Object



91
92
93
94
95
96
97
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 91

def refresh!
  set_credentials_from_oauth_token(token: oauth.refresh!)

  self
rescue OAuth2::Error => e
  raise parse_error(error: e)
end

#set_credentials_from_oauth_code(code:, redirect_uri:) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 99

def set_credentials_from_oauth_code(code:, redirect_uri:)
  oauth_token = oauth_client.auth_code.get_token(
    code,
    redirect_uri: redirect_uri
  )

  set_credentials_from_oauth_token(token: oauth_token)

  oauth_token
end

#url_for(resource:) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 110

def url_for(resource:)
  base_url = test ? 'https://app.sandbox.qbo.intuit.com/app' : 'https://qbo.intuit.com/app'
  resource_path = case resource
                  when LedgerSync::
                    "/register?accountId=#{resource.ledger_id}"
                  when LedgerSync::Bill
                    "/bill?txnId=#{resource.ledger_id}"
                  when LedgerSync::Customer
                    "/customerdetail?nameId=#{resource.ledger_id}"
                  when LedgerSync::Deposit
                    "/deposit?txnId=#{resource.ledger_id}"
                  when LedgerSync::Expense
                    "/expense?txnId=#{resource.ledger_id}"
                  when LedgerSync::JournalEntry
                    "/journal?txnId=#{resource.ledger_id}"
                  when LedgerSync::Payment
                    "/recvpayment?txnId=#{resource.ledger_id}"
                  when LedgerSync::Transfer
                    "/transfer?txnId=#{resource.ledger_id}"
                  when LedgerSync::Vendor
                    "/vendordetail?nameId=#{resource.ledger_id}"
                  end

  base_url + resource_path
end