forked from WesKetch/quickbooks_integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickbooks_endpoint.rb
92 lines (77 loc) · 2.35 KB
/
quickbooks_endpoint.rb
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
80
81
82
83
84
85
86
87
88
89
90
91
92
require_relative 'lib/qb_integration'
if File.exists? File.join(File.expand_path(File.dirname(__FILE__)), '.env')
# TODO check an ENV variable i.e. RACK_ENV
begin
require 'dotenv'
Dotenv.load
rescue => e
puts e.message
puts e.backtrace.join("\n")
end
end
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
set :show_exceptions, false
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