This repository has been archived by the owner on Nov 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
qbo_rails.rb
106 lines (93 loc) · 2.55 KB
/
qbo_rails.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
require 'quickbooks-ruby'
require 'quickbooks-ruby-base'
require_relative 'qbo_rails/error_handler'
class QboRails
attr_accessor :error, :only_run_once
attr_reader :result, :base
cattr_accessor(:foreign_key) { "qbo_id" }
include ActiveSupport::Rescuable
include ErrorHandler
rescue_from Quickbooks::IntuitRequestException,
Quickbooks::InvalidModelException,
Quickbooks::AuthorizationFailure,
Quickbooks::Forbidden,
Quickbooks::MissingRealmError, with: :record_error
def initialize(account, type = nil)
@base = Quickbooks::Base.new(account, type)
end
def create(qb_record)
with_rescued_exception do
@qb_record = qb_record
@result = @base.service.create(@qb_record)
@result
end
end
def create_or_update(record, qb_record)
with_rescued_exception do
@record = record
@qb_record = qb_record
if id = @record.send(foreign_key)
prepare_update(id, qb_record)
@result = @base.service.update(qb_record)
else
@result = @base.service.create(@qb_record)
set_qbo_id
end
end
end
def delete(record_or_qbo_id)
with_rescued_exception do
id = record_or_qbo_id.try(foreign_key) || record_or_qbo_id
@qb_record = @base.find_by_id(id)
@result = @base.service.delete(@qb_record)
end
end
protected
def with_rescued_exception
yield
rescue => e
if @only_run_once
rescue_with_handler(e) || raise
else
unless run_error_handlers(e)
rescue_with_handler(e) || raise
end
end
end
def run_error_handlers(e)
methods.detect do |method|
if method =~ /^handle_error_/
send(method, e)
end
end
end
def qbo_error_params(exception)
@error = {
message: exception.message[0..250],
body: "#{exception}\n\n#{exception.message}\n\n#{exception.backtrace.take(10)}",
resource_type: @record.try(:class).try(:name),
resource_id: @record.try(:id),
request_xml: request_xml(exception)
}
end
def record_error(exception)
QboError.create!(qbo_error_params(exception))
end
def request_xml(exception)
if exception.respond_to?(:request_xml)
exception.request_xml
elsif defined?(@qb_record)
@qb_record.to_xml.to_s
end
end
def prepare_update(id, qb_record)
result = @base.find_by_id(id)
qb_record.id = result.id
qb_record.sync_token = result.sync_token
qb_record
end
def set_qbo_id
@record.update(self.foreign_key => @result.id)
@record
end
end