-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathquickbooks_endpoint.rb
More file actions
79 lines (66 loc) · 2.11 KB
/
Copy pathquickbooks_endpoint.rb
File metadata and controls
79 lines (66 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
require File.expand_path(File.dirname(__FILE__) + '/lib/qb_integration')
class QuickbooksEndpoint < EndpointBase::Sinatra::Base
set :logging, true
Honeybadger.configure do |config|
config.api_key = ENV['HONEYBADGER_KEY']
config.environment_name = ENV['RACK_ENV']
end
error do
result 500, lookup_error_message
end
post '/add_product' do
code, summary = QBIntegration::Product.new(@payload, @config).import
result code, summary
end
post '/update_product' do
code, summary = QBIntegration::Product.new(@payload, @config).import
result code, summary
end
post '/add_order' do
begin
code, summary = QBIntegration::Order.new(@payload, @config).create
result code, summary
rescue QBIntegration::AlreadyPersistedOrderException => e
result 500, e.message
end
end
post '/update_order' do
code, summary = QBIntegration::Order.new(@payload, @config).update
result code, summary
end
post '/cancel_order' do
code, summary = QBIntegration::Order.new(@payload, @config).cancel
result code, summary
end
post '/add_return' do
code, summary = QBIntegration::ReturnAuthorization.new(@payload, @config).create
result code, summary
end
post '/update_return' do
code, summary = QBIntegration::ReturnAuthorization.new(@payload, @config).update
result code, summary
end
post '/get_inventory' do
stock = QBIntegration::Stock.new(@payload, @config)
if stock.name.present? && stock.item
add_object :inventory, stock.inventory
result 200
elsif stock.items.present?
stock.inventories.each { |item| add_object :inventory, item }
add_parameter 'quickbooks_poll_stock_timestamp', stock.last_modified_date
result 200
else
result 200
end
end
def lookup_error_message
case env['sinatra.error'].class.to_s
when "Quickbooks::AuthorizationFailure"
"Authorization failure. Please check your Quickbooks credentials"
when "Quickbooks::ServiceUnavailable"
"Quickbooks API appears to be inaccessible HTTP 503 returned."
else
env['sinatra.error'].message
end
end
end