Class: LedgerSync::Adaptors::QuickBooksOnline::Adaptor
- Inherits:
-
Adaptor
- Object
- Adaptor
- LedgerSync::Adaptors::QuickBooksOnline::Adaptor
show all
- Defined in:
- lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb
Constant Summary
collapse
{ 'Accept' => 'application/json', 'Content-Type' => 'application/json' }.freeze
- ROOT_URI =
'https://quickbooks.api.intuit.com'
- REVOKE_TOKEN_URI =
'https://developer.api.intuit.com/v2/oauth2/tokens/revoke'
- ROOT_SANDBOX_URI =
'https://sandbox-quickbooks.api.intuit.com'
- API_RESOURCE_MAPPING_OVERRIDE =
{
LedgerSync::Expense => 'purchase',
LedgerSync::LedgerClass => 'class'
}
- API_RESOURCE_MAPPING_OVERRIDE_REVERSE =
API_RESOURCE_MAPPING_OVERRIDE.invert
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#authorization_url(redirect_uri:) ⇒ Object
-
#find(path:) ⇒ Object
-
#initialize(access_token:, client_id:, client_secret:, realm_id:, refresh_token:, test: false, update_dotenv: true) ⇒ Adaptor
constructor
A new instance of Adaptor.
-
#oauth ⇒ Object
-
#oauth_client ⇒ Object
-
#post(path:, payload:) ⇒ Object
-
#query(limit: 10, offset: 1, query:, resource_class:) ⇒ Object
-
#refresh! ⇒ Object
-
#revoke_token! ⇒ Object
-
#set_credentials_from_oauth_code(code:, realm_id: nil, redirect_uri:) ⇒ Object
-
#update_secrets_in_dotenv ⇒ Object
-
#url_for(resource:) ⇒ Object
Methods inherited from Adaptor
#adaptor_configuration, #base_module, base_module, base_operation_module_for, config, #ledger_attributes_to_save, #parse_operation_error, #searcher_for?, #searcher_klass_for, url_for
#valid?, #validate, #validate_or_fail
Constructor Details
#initialize(access_token:, client_id:, client_secret:, realm_id:, refresh_token:, test: false, update_dotenv: true) ⇒ Adaptor
Returns a new instance of Adaptor.
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 33
def initialize(
access_token:,
client_id:,
client_secret:,
realm_id:,
refresh_token:,
test: false,
update_dotenv: true
)
@access_token = access_token
@client_id = client_id
@client_secret = client_secret
@realm_id = realm_id
@refresh_token = refresh_token
@test = test
@update_dotenv = update_dotenv
@previous_access_tokens = []
@previous_refresh_tokens = []
@root_uri = (test ? ROOT_SANDBOX_URI : ROOT_URI)
update_secrets_in_dotenv if update_dotenv
end
|
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
20
21
22
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 20
def access_token
@access_token
end
|
#client_id ⇒ Object
Returns the value of attribute client_id.
20
21
22
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 20
def client_id
@client_id
end
|
#client_secret ⇒ Object
Returns the value of attribute client_secret.
20
21
22
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 20
def client_secret
@client_secret
end
|
#expires_at ⇒ Object
Returns the value of attribute expires_at.
20
21
22
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 20
def expires_at
@expires_at
end
|
#previous_access_tokens ⇒ Object
Returns the value of attribute previous_access_tokens.
20
21
22
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 20
def previous_access_tokens
@previous_access_tokens
end
|
#previous_refresh_tokens ⇒ Object
Returns the value of attribute previous_refresh_tokens.
20
21
22
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 20
def previous_refresh_tokens
@previous_refresh_tokens
end
|
#realm_id ⇒ Object
Returns the value of attribute realm_id.
20
21
22
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 20
def realm_id
@realm_id
end
|
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
20
21
22
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 20
def refresh_token
@refresh_token
end
|
#refresh_token_expires_at ⇒ Object
Returns the value of attribute refresh_token_expires_at.
20
21
22
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 20
def refresh_token_expires_at
@refresh_token_expires_at
end
|
#root_uri ⇒ Object
Returns the value of attribute root_uri.
20
21
22
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 20
def root_uri
@root_uri
end
|
#test ⇒ Object
Returns the value of attribute test.
20
21
22
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 20
def test
@test
end
|
#update_dotenv ⇒ Object
Returns the value of attribute update_dotenv.
20
21
22
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 20
def update_dotenv
@update_dotenv
end
|
Class Method Details
.ledger_attributes_to_save ⇒ Object
183
184
185
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 183
def self.ledger_attributes_to_save
%i[access_token expires_at refresh_token refresh_token_expires_at]
end
|
.ledger_resource_type_for(resource_class:) ⇒ Object
187
188
189
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 187
def self.ledger_resource_type_for(resource_class:)
API_RESOURCE_MAPPING_OVERRIDE[resource_class] || resource_class.resource_type.to_s
end
|
.new_from_env(**override) ⇒ Object
191
192
193
194
195
196
197
198
199
200
201
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 191
def self.new_from_env(**override)
new(
{
access_token: ENV.fetch('QUICKBOOKS_ONLINE_ACCESS_TOKEN'),
client_id: ENV.fetch('QUICKBOOKS_ONLINE_CLIENT_ID'),
client_secret: ENV.fetch('QUICKBOOKS_ONLINE_CLIENT_SECRET'),
realm_id: ENV.fetch('QUICKBOOKS_ONLINE_REALM_ID'),
refresh_token: ENV.fetch('QUICKBOOKS_ONLINE_REFRESH_TOKEN')
}.merge(override)
)
end
|
.new_from_oauth_client_uri(oauth_client:, uri:, **override) ⇒ Object
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 203
def self.new_from_oauth_client_uri(oauth_client:, uri:, **override)
parsed_uri = OAuthClient::RedirectURIParser.new(uri: uri)
oauth_token = oauth_client.get_token(
code: parsed_uri.code,
redirect_uri: parsed_uri.redirect_uri
)
new(
{
access_token: oauth_token.token,
client_id: oauth_client.client_id,
client_secret: oauth_client.client_secret,
realm_id: parsed_uri.realm_id,
refresh_token: oauth_token.refresh_token
}.merge(override)
)
end
|
.resource_from_ledger_type(type:) ⇒ Object
Instance Method Details
#authorization_url(redirect_uri:) ⇒ Object
58
59
60
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 58
def authorization_url(redirect_uri:)
oauth_client.authorization_url(redirect_uri: redirect_uri)
end
|
#find(path:) ⇒ Object
62
63
64
65
66
67
68
69
70
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 62
def find(path:)
url = "#{oauth_base_uri}/#{path}"
request(
headers: OAUTH_HEADERS.dup,
method: :get,
url: url
)
end
|
#oauth ⇒ Object
72
73
74
75
76
77
78
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 72
def oauth
OAuth2::AccessToken.new(
oauth_client.client,
access_token,
refresh_token: refresh_token
)
end
|
#oauth_client ⇒ Object
80
81
82
83
84
85
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 80
def oauth_client
@oauth_client ||= OAuthClient.new(
client_id: client_id,
client_secret: client_secret
)
end
|
#post(path:, payload:) ⇒ Object
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 87
def post(path:, payload:)
url = "#{oauth_base_uri}/#{path}"
request(
headers: OAUTH_HEADERS.dup,
method: :post,
body: payload,
url: url
)
end
|
#query(limit: 10, offset: 1, query:, resource_class:) ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 98
def query(limit: 10, offset: 1, query:, resource_class:)
ledger_resource_type = self.class.ledger_resource_type_for(
resource_class: resource_class
).classify
full_query = "SELECT * FROM #{ledger_resource_type} WHERE #{query} STARTPOSITION #{offset} MAXRESULTS #{limit}"
url = "#{oauth_base_uri}/query?query=#{CGI.escape(full_query)}"
request(
headers: OAUTH_HEADERS.dup,
method: :get,
url: url
)
end
|
#refresh! ⇒ Object
112
113
114
115
116
117
118
119
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 112
def refresh!
set_credentials_from_oauth_token(
token: Request.new(
adaptor: self
).refresh!
)
self
end
|
#revoke_token! ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 121
def revoke_token!
= OAUTH_HEADERS.dup.merge('Authorization' => 'Basic ' + Base64.strict_encode64(client_id + ':' + client_secret))
LedgerSync::Adaptors::Request.new(
body: {
token: access_token
},
headers: ,
method: :post,
url: REVOKE_TOKEN_URI
).perform.status == 200
end
|
#set_credentials_from_oauth_code(code:, realm_id: nil, redirect_uri:) ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 133
def set_credentials_from_oauth_code(code:, realm_id: nil, redirect_uri:)
oauth_token = oauth_client.get_token(
code: code,
redirect_uri: redirect_uri
)
set_credentials_from_oauth_token(
realm_id: realm_id,
token: oauth_token
)
oauth_token
end
|
#update_secrets_in_dotenv ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 147
def update_secrets_in_dotenv
return if ENV['TEST_ENV'] && !ENV['USE_DOTENV_ADAPTOR_SECRETS']
filename = File.join(LedgerSync.root, '.env')
return unless File.exist?(filename)
prefix = 'QUICKBOOKS_ONLINE_'
Tempfile.open(".#{File.basename(filename)}", File.dirname(filename)) do |tempfile|
File.open(filename).each do |line|
env_key = line.split('=').first
adaptor_method = env_key.split(prefix).last.downcase
if line =~ /\A#{prefix}/ && respond_to?(adaptor_method)
env_value = ENV[env_key]
new_value = send(adaptor_method)
tempfile.puts "#{env_key}=#{new_value}"
next if env_value == new_value
ENV[env_key] = new_value
tempfile.puts "# #{env_key}=#{env_value} # Updated on #{Time.now}"
else
tempfile.puts line
end
end
tempfile.close
FileUtils.mv tempfile.path, filename
end
Dotenv.load
end
|
#url_for(resource:) ⇒ Object
179
180
181
|
# File 'lib/ledger_sync/adaptors/quickbooks_online/adaptor.rb', line 179
def url_for(resource:)
DashboardURLHelper.new(resource: resource, test: test).url
end
|