-
Notifications
You must be signed in to change notification settings - Fork 0
/
Intacct-senderID
168 lines (140 loc) · 4.25 KB
/
Intacct-senderID
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
107
108
109
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
Skip to content
Search or jump to…
Pull requests
Issues
Marketplace
Explore
@sanjeevane Sign out
4
1 10 privateprep/intacct-ruby
Code Issues 0 Pull requests 0 Projects 0 Wiki Insights
intacct-ruby/lib/intacct_ruby/request.rb
9bf4135 on Aug 3, 2017
@jzornow jzornow Allow Request#send to play nicely alongside Object#send
139 lines (113 sloc) 3.71 KB
require 'builder'
require 'intacct_ruby/api'
require 'intacct_ruby/response'
require 'intacct_ruby/function'
require 'intacct_ruby/exceptions/insufficient_credentials_exception'
require 'intacct_ruby/exceptions/empty_request_exception'
module IntacctRuby
# An outgoing request to the Intacct API. Can have multiple functions.
# Complete with send method that gets (and wraps) response from Intacct.
class Request
attr_reader :functions
DEFAULTS = {
uniqueid: false,
dtdversion: 3.0,
includewhitespace: false,
transaction: true
}.freeze
REQUIRED_AUTHENTICATION_KEYS = [
:senderid,
:sender_password,
:userid,
:companyid,
:user_password
].freeze
def initialize(*functions, request_params)
# request_params should contain all req'd authentication information. If
# not, an error will be thrown on #send
@opts = DEFAULTS.dup.merge request_params
# If a hash is provided + popped, the remaining attrs are functions
@functions = functions
end
def to_xml
@to_xml ||= begin
@request = Builder::XmlMarkup.new
@request.instruct!
@request.request do
control_block
operation_block
end
end
end
def send(opts = {})
if opts.is_a? Hash
api = opts[:api] || Api.new
validate_keys!
validate_functions!
Response.new api.send_request(self)
else
# so that Request#send can play nice alongside Object#send
super
end
end
private
def method_missing(method_name, *arguments, &block)
super unless Function::ALLOWED_TYPES.include? method_name.to_s
# object_type must be the first argument in arguments
@functions << Function.new(method_name, arguments.shift, *arguments)
end
def respond_to_missing?(method_name, include_private = false)
Function::ALLOWED_TYPES.include?(method_name) || super
end
def validate_functions!
unless functions.any?
raise Exceptions::EmptyRequestException,
'a successful request must contain at least one function'
end
end
def validate_keys!
missing_keys = REQUIRED_AUTHENTICATION_KEYS - @opts.keys
unless missing_keys.empty?
missing_keys.map! { |s| ":#{s}" } # so they appear as symbols in output
raise Exceptions::InsufficientCredentialsException,
"[#{missing_keys.join(', ')}] required for a valid request. "
'All authentication-related keys should be provided on ' \
'instantiation in IntacctRuby::Request#new'
end
end
def timestamp
@timestamp ||= Time.now.utc.to_s
end
def control_block
@request.control do
@request.senderid @opts[:senderid]
@request.password @opts[:sender_password]
# As recommended by Intacct API reference. This ID should be unique
# to the call: it's used to associate a response with a request.
@request.controlid timestamp
@request.uniqueid @opts[:uniqueid]
@request.dtdversion @opts[:dtdversion]
@request.includewhitespace @opts[:includewhitespace]
end
end
def authentication_block
@request.authentication do
@request.login do
@request.userid @opts[:userid]sanjeev
@request.companyid @opts[:companyid]senjeevtest01
@request.password @opts[:user_password]welcome1234
end
end
end
def operation_block
@request.operation transaction: @opts[:transaction] do
authentication_block
@request.content do
functions.each do |function|
@request << function.to_xml
end
end
end
end
end
end
© 2018 GitHub, Inc.
Terms
Privacy
Security
Status
Help
Contact GitHub
Pricing
API
Training
Blog
About
Press h to open a hovercard with more details.