pesamoni_ruby 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,174 @@
1
+ =begin
2
+ #Pesaway Pesamoni API Documentation
3
+
4
+ OpenAPI spec version: 1.0.3
5
+
6
+
7
+ =end
8
+
9
+ require 'uri'
10
+
11
+ module Pesamoni
12
+ class DefaultApi
13
+ attr_accessor :api_client
14
+
15
+ def initialize(api_client = ApiClient.default)
16
+ @api_client = api_client
17
+ end
18
+ # Below are parameters and their respective expected responses. In order to try out the service, simply click Try it out.
19
+ # @param method Enter a request method. To check for request methods <a href=''>click here</a>
20
+ # @param amount Enter the amount you would like to request for. <p style=\"color:red\">This method applies for request methods <b>acreceive, acreceivekeac, acsend, acsendkeac, acsendbank, pesab2c, sendairtime, cardaccept</b></p>
21
+ # @param [Hash] opts the optional parameters
22
+ # @option opts [String] :mobile Enter the mobile number you would like to execute the above method in format 256.... or 254...<p style=\"color:red\">This method applies for request methods <b>acreceive, acreceivekeac, acsend, acsendkeac, senderid, sendsms, sendairtime</b></p>
23
+ # @option opts [String] :holdername Enter name of payer for Visa/MasterCard transactions<p style=\"color:red\">This method applies for request method <b>cardaccept</b></p>
24
+ # @option opts [String] :cardnumber Enter the Visa/MasterCard cardnumber<p style=\"color:red\">This method applies for request method <b>cardaccept</b></p>
25
+ # @option opts [String] :cvv Enter the Visa/MasterCard cvv<p style=\"color:red\">This method applies for request method <b>cardaccept</b></p>
26
+ # @option opts [String] :exp Enter the Visa/MasterCard expiry date in the format MM/YYYY e.g 07/2030<p style=\"color:red\">This method applies for request method <b>cardaccept</b></p>
27
+ # @option opts [String] :currency Enter the currency you intend to make the transaction for Visa/MasterCard based transactions<p style=\"color:red\">This method applies for request method <b>cardaccept</b></p>
28
+ # @option opts [String] :account Enter the Pesamoni account you would like to use for this transaction<p style=\"color:red\">This method applies for request method <b>paybills</b></p>
29
+ # @option opts [String] :reference Enter your user generated transaction reference<p style=\"color:red\">This method applies for request methods <b>acreceive, acreceivekeac, acsend, acsendkeac, sendsms, transactionstatus, sendairtime, pesab2c, sendsms, cardaccept</b></p>
30
+ # @option opts [String] :genericmsg Enter your user generated generic message for the requested transaction<p style=\"color:red\">This method applies for request methods <b>acreceive, acreceivekeac, acsend, acsendkeac, sendsms, sendairtime, pesab2c, sendsms, cardaccept</b></p>
31
+ # @option opts [String] :token Enter your user generated token for the above mentioned method<p style=\"color:red\">This method applies for request methods <b>acreceive, acreceivekeac, acsend, acsendkeac, sendsms, sendairtime, pesab2c, sendsms, cardaccept</b></p>
32
+ # @option opts [String] :bouquet Enter the bouquet or package you would like to pay for<p style=\"color:red\">This method applies for request methods <b>paybills</b></p>
33
+ # @option opts [String] :payoption Enter your prefered payment option<p style=\"color:red\">This method applies for request methods <b>paybills</b></p>
34
+ # @option opts [String] :meternumber Enter the meter number for the intended payment<p style=\"color:red\">This method applies for request methods <b>paybills</b></p>
35
+ # @return [InlineResponse200]
36
+ def transactions_post(method, amount, opts = {})
37
+ data, _status_code, _headers = transactions_post_with_http_info(method, amount, opts)
38
+ data
39
+ end
40
+
41
+ # Below are parameters and their respective expected responses. In order to try out the service, simply click Try it out.
42
+ # @param method Enter a request method. To check for request methods <a href=''>click here</a>
43
+ # @param amount Enter the amount you would like to request for. <p style=\"color:red\">This method applies for request methods <b>acreceive, acreceivekeac, acsend, acsendkeac, acsendbank, pesab2c, sendairtime, cardaccept</b></p>
44
+ # @param [Hash] opts the optional parameters
45
+ # @option opts [String] :mobile Enter the mobile number you would like to execute the above method in format 256.... or 254...<p style=\"color:red\">This method applies for request methods <b>acreceive, acreceivekeac, acsend, acsendkeac, senderid, sendsms, sendairtime</b></p>
46
+ # @option opts [String] :holdername Enter name of payer for Visa/MasterCard transactions<p style=\"color:red\">This method applies for request method <b>cardaccept</b></p>
47
+ # @option opts [String] :cardnumber Enter the Visa/MasterCard cardnumber<p style=\"color:red\">This method applies for request method <b>cardaccept</b></p>
48
+ # @option opts [String] :cvv Enter the Visa/MasterCard cvv<p style=\"color:red\">This method applies for request method <b>cardaccept</b></p>
49
+ # @option opts [String] :exp Enter the Visa/MasterCard expiry date in the format MM/YYYY e.g 07/2030<p style=\"color:red\">This method applies for request method <b>cardaccept</b></p>
50
+ # @option opts [String] :currency Enter the currency you intend to make the transaction for Visa/MasterCard based transactions<p style=\"color:red\">This method applies for request method <b>cardaccept</b></p>
51
+ # @option opts [String] :account Enter the Pesamoni account you would like to use for this transaction<p style=\"color:red\">This method applies for request method <b>paybills</b></p>
52
+ # @option opts [String] :reference Enter your user generated transaction reference<p style=\"color:red\">This method applies for request methods <b>acreceive, acreceivekeac, acsend, acsendkeac, sendsms, transactionstatus, sendairtime, pesab2c, sendsms, cardaccept</b></p>
53
+ # @option opts [String] :genericmsg Enter your user generated generic message for the requested transaction<p style=\"color:red\">This method applies for request methods <b>acreceive, acreceivekeac, acsend, acsendkeac, sendsms, sendairtime, pesab2c, sendsms, cardaccept</b></p>
54
+ # @option opts [String] :token Enter your user generated token for the above mentioned method<p style=\"color:red\">This method applies for request methods <b>acreceive, acreceivekeac, acsend, acsendkeac, sendsms, sendairtime, pesab2c, sendsms, cardaccept</b></p>
55
+ # @option opts [String] :bouquet Enter the bouquet or package you would like to pay for<p style=\"color:red\">This method applies for request methods <b>paybills</b></p>
56
+ # @option opts [String] :payoption Enter your prefered payment option<p style=\"color:red\">This method applies for request methods <b>paybills</b></p>
57
+ # @option opts [String] :meternumber Enter the meter number for the intended payment<p style=\"color:red\">This method applies for request methods <b>paybills</b></p>
58
+ # @return [Array<(InlineResponse200, Fixnum, Hash)>] InlineResponse200 data, response status code and response headers
59
+ def transactions_post_with_http_info(method, amount, opts = {})
60
+ if @api_client.config.debugging
61
+ @api_client.config.logger.debug 'Calling API: DefaultApi.transactions_post ...'
62
+ end
63
+ # verify the required parameter 'method' is set
64
+ if @api_client.config.client_side_validation && method.nil?
65
+ fail ArgumentError, "Missing the required parameter 'method' when calling DefaultApi.transactions_post"
66
+ end
67
+ if @api_client.config.client_side_validation && method < 1
68
+ fail ArgumentError, 'invalid value for "method" when calling DefaultApi.transactions_post, must be greater than or equal to 1.'
69
+ end
70
+
71
+ # verify the required parameter 'amount' is set
72
+ if @api_client.config.client_side_validation && amount.nil?
73
+ fail ArgumentError, "Missing the required parameter 'amount' when calling DefaultApi.transactions_post"
74
+ end
75
+ if @api_client.config.client_side_validation && amount < 1
76
+ fail ArgumentError, 'invalid value for "amount" when calling DefaultApi.transactions_post, must be greater than or equal to 1.'
77
+ end
78
+
79
+ if @api_client.config.client_side_validation && !opts[:'mobile'].nil? && opts[:'mobile'] < 1
80
+ fail ArgumentError, 'invalid value for "opts[:"mobile"]" when calling DefaultApi.transactions_post, must be greater than or equal to 1.'
81
+ end
82
+
83
+ if @api_client.config.client_side_validation && !opts[:'holdername'].nil? && opts[:'holdername'] < 1
84
+ fail ArgumentError, 'invalid value for "opts[:"holdername"]" when calling DefaultApi.transactions_post, must be greater than or equal to 1.'
85
+ end
86
+
87
+ if @api_client.config.client_side_validation && !opts[:'cardnumber'].nil? && opts[:'cardnumber'] < 1
88
+ fail ArgumentError, 'invalid value for "opts[:"cardnumber"]" when calling DefaultApi.transactions_post, must be greater than or equal to 1.'
89
+ end
90
+
91
+ if @api_client.config.client_side_validation && !opts[:'cvv'].nil? && opts[:'cvv'] < 1
92
+ fail ArgumentError, 'invalid value for "opts[:"cvv"]" when calling DefaultApi.transactions_post, must be greater than or equal to 1.'
93
+ end
94
+
95
+ if @api_client.config.client_side_validation && !opts[:'exp'].nil? && opts[:'exp'] < 1
96
+ fail ArgumentError, 'invalid value for "opts[:"exp"]" when calling DefaultApi.transactions_post, must be greater than or equal to 1.'
97
+ end
98
+
99
+ if @api_client.config.client_side_validation && !opts[:'currency'].nil? && opts[:'currency'] < 1
100
+ fail ArgumentError, 'invalid value for "opts[:"currency"]" when calling DefaultApi.transactions_post, must be greater than or equal to 1.'
101
+ end
102
+
103
+ if @api_client.config.client_side_validation && !opts[:'account'].nil? && opts[:'account'] < 1
104
+ fail ArgumentError, 'invalid value for "opts[:"account"]" when calling DefaultApi.transactions_post, must be greater than or equal to 1.'
105
+ end
106
+
107
+ if @api_client.config.client_side_validation && !opts[:'reference'].nil? && opts[:'reference'] < 1
108
+ fail ArgumentError, 'invalid value for "opts[:"reference"]" when calling DefaultApi.transactions_post, must be greater than or equal to 1.'
109
+ end
110
+
111
+ if @api_client.config.client_side_validation && !opts[:'genericmsg'].nil? && opts[:'genericmsg'] < 1
112
+ fail ArgumentError, 'invalid value for "opts[:"genericmsg"]" when calling DefaultApi.transactions_post, must be greater than or equal to 1.'
113
+ end
114
+
115
+ if @api_client.config.client_side_validation && !opts[:'token'].nil? && opts[:'token'] < 1
116
+ fail ArgumentError, 'invalid value for "opts[:"token"]" when calling DefaultApi.transactions_post, must be greater than or equal to 1.'
117
+ end
118
+
119
+ if @api_client.config.client_side_validation && !opts[:'bouquet'].nil? && opts[:'bouquet'] < 1
120
+ fail ArgumentError, 'invalid value for "opts[:"bouquet"]" when calling DefaultApi.transactions_post, must be greater than or equal to 1.'
121
+ end
122
+
123
+ if @api_client.config.client_side_validation && !opts[:'payoption'].nil? && opts[:'payoption'] < 1
124
+ fail ArgumentError, 'invalid value for "opts[:"payoption"]" when calling DefaultApi.transactions_post, must be greater than or equal to 1.'
125
+ end
126
+
127
+ if @api_client.config.client_side_validation && !opts[:'meternumber'].nil? && opts[:'meternumber'] < 1
128
+ fail ArgumentError, 'invalid value for "opts[:"meternumber"]" when calling DefaultApi.transactions_post, must be greater than or equal to 1.'
129
+ end
130
+
131
+ # resource path
132
+ local_var_path = '/transactions'
133
+
134
+ # query parameters
135
+ query_params = {}
136
+ query_params[:'method'] = method
137
+ query_params[:'amount'] = amount
138
+ query_params[:'mobile'] = opts[:'mobile'] if !opts[:'mobile'].nil?
139
+ query_params[:'holdername'] = opts[:'holdername'] if !opts[:'holdername'].nil?
140
+ query_params[:'cardnumber'] = opts[:'cardnumber'] if !opts[:'cardnumber'].nil?
141
+ query_params[:'cvv'] = opts[:'cvv'] if !opts[:'cvv'].nil?
142
+ query_params[:'exp'] = opts[:'exp'] if !opts[:'exp'].nil?
143
+ query_params[:'currency'] = opts[:'currency'] if !opts[:'currency'].nil?
144
+ query_params[:'account'] = opts[:'account'] if !opts[:'account'].nil?
145
+ query_params[:'reference'] = opts[:'reference'] if !opts[:'reference'].nil?
146
+ query_params[:'genericmsg'] = opts[:'genericmsg'] if !opts[:'genericmsg'].nil?
147
+ query_params[:'token'] = opts[:'token'] if !opts[:'token'].nil?
148
+ query_params[:'bouquet'] = opts[:'bouquet'] if !opts[:'bouquet'].nil?
149
+ query_params[:'payoption'] = opts[:'payoption'] if !opts[:'payoption'].nil?
150
+ query_params[:'meternumber'] = opts[:'meternumber'] if !opts[:'meternumber'].nil?
151
+
152
+ # header parameters
153
+ header_params = {}
154
+
155
+ # form parameters
156
+ form_params = {}
157
+
158
+ # http body (model)
159
+ post_body = nil
160
+ auth_names = ['apipassword', 'apiusername']
161
+ data, status_code, headers = @api_client.call_api(:POST, local_var_path,
162
+ :header_params => header_params,
163
+ :query_params => query_params,
164
+ :form_params => form_params,
165
+ :body => post_body,
166
+ :auth_names => auth_names,
167
+ :return_type => 'InlineResponse200')
168
+ if @api_client.config.debugging
169
+ @api_client.config.logger.debug "API called: DefaultApi#transactions_post\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
170
+ end
171
+ return data, status_code, headers
172
+ end
173
+ end
174
+ end
@@ -0,0 +1,387 @@
1
+ =begin
2
+ #Pesaway Pesamoni API Documentation
3
+
4
+ #Automate mobile money payments, bank transfers and more..
5
+
6
+ OpenAPI spec version: 1.0.3
7
+
8
+
9
+ =end
10
+
11
+ require 'date'
12
+ require 'json'
13
+ require 'logger'
14
+ require 'tempfile'
15
+ require 'typhoeus'
16
+ require 'uri'
17
+
18
+ module Pesamoni
19
+ class ApiClient
20
+ # The Configuration object holding settings to be used in the API client.
21
+ attr_accessor :config
22
+
23
+ # Defines the headers to be used in HTTP requests of all API calls by default.
24
+ #
25
+ # @return [Hash]
26
+ attr_accessor :default_headers
27
+
28
+ # Initializes the ApiClient
29
+ # @option config [Configuration] Configuration for initializing the object, default to Configuration.default
30
+ def initialize(config = Configuration.default)
31
+ @config = config
32
+ @user_agent = "Swagger-Codegen/#{VERSION}/ruby"
33
+ @default_headers = {
34
+ 'Content-Type' => 'application/json',
35
+ 'User-Agent' => @user_agent
36
+ }
37
+ end
38
+
39
+ def self.default
40
+ @@default ||= ApiClient.new
41
+ end
42
+
43
+ # Call an API with given options.
44
+ #
45
+ # @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
46
+ # the data deserialized from response body (could be nil), response status code and response headers.
47
+ def call_api(http_method, path, opts = {})
48
+ request = build_request(http_method, path, opts)
49
+ response = request.run
50
+
51
+ if @config.debugging
52
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
53
+ end
54
+
55
+ unless response.success?
56
+ if response.timed_out?
57
+ fail ApiError.new('Connection timed out')
58
+ elsif response.code == 0
59
+ # Errors from libcurl will be made visible here
60
+ fail ApiError.new(:code => 0,
61
+ :message => response.return_message)
62
+ else
63
+ fail ApiError.new(:code => response.code,
64
+ :response_headers => response.headers,
65
+ :response_body => response.body),
66
+ response.status_message
67
+ end
68
+ end
69
+
70
+ if opts[:return_type]
71
+ data = deserialize(response, opts[:return_type])
72
+ else
73
+ data = nil
74
+ end
75
+ return data, response.code, response.headers
76
+ end
77
+
78
+ # Builds the HTTP request
79
+ #
80
+ # @param [String] http_method HTTP method/verb (e.g. POST)
81
+ # @param [String] path URL path (e.g. /account/new)
82
+ # @option opts [Hash] :header_params Header parameters
83
+ # @option opts [Hash] :query_params Query parameters
84
+ # @option opts [Hash] :form_params Query parameters
85
+ # @option opts [Object] :body HTTP body (JSON/XML)
86
+ # @return [Typhoeus::Request] A Typhoeus Request
87
+ def build_request(http_method, path, opts = {})
88
+ url = build_request_url(path)
89
+ http_method = http_method.to_sym.downcase
90
+
91
+ header_params = @default_headers.merge(opts[:header_params] || {})
92
+ query_params = opts[:query_params] || {}
93
+ form_params = opts[:form_params] || {}
94
+
95
+ update_params_for_auth! header_params, query_params, opts[:auth_names]
96
+
97
+ # set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
98
+ _verify_ssl_host = @config.verify_ssl_host ? 2 : 0
99
+
100
+ req_opts = {
101
+ :method => http_method,
102
+ :headers => header_params,
103
+ :params => query_params,
104
+ :params_encoding => @config.params_encoding,
105
+ :timeout => @config.timeout,
106
+ :ssl_verifypeer => @config.verify_ssl,
107
+ :ssl_verifyhost => _verify_ssl_host,
108
+ :sslcert => @config.cert_file,
109
+ :sslkey => @config.key_file,
110
+ :verbose => @config.debugging
111
+ }
112
+
113
+ # set custom cert, if provided
114
+ req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
115
+
116
+ if [:post, :patch, :put, :delete].include?(http_method)
117
+ req_body = build_request_body(header_params, form_params, opts[:body])
118
+ req_opts.update :body => req_body
119
+ if @config.debugging
120
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
121
+ end
122
+ end
123
+
124
+ request = Typhoeus::Request.new(url, req_opts)
125
+ download_file(request) if opts[:return_type] == 'File'
126
+ request
127
+ end
128
+
129
+ # Check if the given MIME is a JSON MIME.
130
+ # JSON MIME examples:
131
+ # application/json
132
+ # application/json; charset=UTF8
133
+ # APPLICATION/JSON
134
+ # */*
135
+ # @param [String] mime MIME
136
+ # @return [Boolean] True if the MIME is application/json
137
+ def json_mime?(mime)
138
+ (mime == '*/*') || !(mime =~ /Application\/.*json(?!p)(;.*)?/i).nil?
139
+ end
140
+
141
+ # Deserialize the response to the given return type.
142
+ #
143
+ # @param [Response] response HTTP response
144
+ # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
145
+ def deserialize(response, return_type)
146
+ body = response.body
147
+
148
+ # handle file downloading - return the File instance processed in request callbacks
149
+ # note that response body is empty when the file is written in chunks in request on_body callback
150
+ return @tempfile if return_type == 'File'
151
+
152
+ return nil if body.nil? || body.empty?
153
+
154
+ # return response body directly for String return type
155
+ return body if return_type == 'String'
156
+
157
+ # ensuring a default content type
158
+ content_type = response.headers['Content-Type'] || 'application/json'
159
+
160
+ fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
161
+
162
+ begin
163
+ data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
164
+ rescue JSON::ParserError => e
165
+ if %w(String Date DateTime).include?(return_type)
166
+ data = body
167
+ else
168
+ raise e
169
+ end
170
+ end
171
+
172
+ convert_to_type data, return_type
173
+ end
174
+
175
+ # Convert data to the given return type.
176
+ # @param [Object] data Data to be converted
177
+ # @param [String] return_type Return type
178
+ # @return [Mixed] Data in a particular type
179
+ def convert_to_type(data, return_type)
180
+ return nil if data.nil?
181
+ case return_type
182
+ when 'String'
183
+ data.to_s
184
+ when 'Integer'
185
+ data.to_i
186
+ when 'Float'
187
+ data.to_f
188
+ when 'BOOLEAN'
189
+ data == true
190
+ when 'DateTime'
191
+ # parse date time (expecting ISO 8601 format)
192
+ DateTime.parse data
193
+ when 'Date'
194
+ # parse date time (expecting ISO 8601 format)
195
+ Date.parse data
196
+ when 'Object'
197
+ # generic object (usually a Hash), return directly
198
+ data
199
+ when /\AArray<(.+)>\z/
200
+ # e.g. Array<Pet>
201
+ sub_type = $1
202
+ data.map { |item| convert_to_type(item, sub_type) }
203
+ when /\AHash\<String, (.+)\>\z/
204
+ # e.g. Hash<String, Integer>
205
+ sub_type = $1
206
+ {}.tap do |hash|
207
+ data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
208
+ end
209
+ else
210
+ # models, e.g. Pet
211
+ Pesamoni.const_get(return_type).new.tap do |model|
212
+ model.build_from_hash data
213
+ end
214
+ end
215
+ end
216
+
217
+ # Save response body into a file in (the defined) temporary folder, using the filename
218
+ # from the "Content-Disposition" header if provided, otherwise a random filename.
219
+ # The response body is written to the file in chunks in order to handle files which
220
+ # size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
221
+ # process can use.
222
+ #
223
+ # @see Configuration#temp_folder_path
224
+ def download_file(request)
225
+ tempfile = nil
226
+ encoding = nil
227
+ request.on_headers do |response|
228
+ content_disposition = response.headers['Content-Disposition']
229
+ if content_disposition && content_disposition =~ /filename=/i
230
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
231
+ prefix = sanitize_filename(filename)
232
+ else
233
+ prefix = 'download-'
234
+ end
235
+ prefix = prefix + '-' unless prefix.end_with?('-')
236
+ encoding = response.body.encoding
237
+ tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
238
+ @tempfile = tempfile
239
+ end
240
+ request.on_body do |chunk|
241
+ chunk.force_encoding(encoding)
242
+ tempfile.write(chunk)
243
+ end
244
+ request.on_complete do |response|
245
+ tempfile.close
246
+ @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
247
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
248
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
249
+ "explicitly with `tempfile.delete`"
250
+ end
251
+ end
252
+
253
+ # Sanitize filename by removing path.
254
+ # e.g. ../../sun.gif becomes sun.gif
255
+ #
256
+ # @param [String] filename the filename to be sanitized
257
+ # @return [String] the sanitized filename
258
+ def sanitize_filename(filename)
259
+ filename.gsub(/.*[\/\\]/, '')
260
+ end
261
+
262
+ def build_request_url(path)
263
+ # Add leading and trailing slashes to path
264
+ path = "/#{path}".gsub(/\/+/, '/')
265
+ URI.encode(@config.base_url + path)
266
+ end
267
+
268
+ # Builds the HTTP request body
269
+ #
270
+ # @param [Hash] header_params Header parameters
271
+ # @param [Hash] form_params Query parameters
272
+ # @param [Object] body HTTP body (JSON/XML)
273
+ # @return [String] HTTP body data in the form of string
274
+ def build_request_body(header_params, form_params, body)
275
+ # http form
276
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
277
+ header_params['Content-Type'] == 'multipart/form-data'
278
+ data = {}
279
+ form_params.each do |key, value|
280
+ case value
281
+ when ::File, ::Array, nil
282
+ # let typhoeus handle File, Array and nil parameters
283
+ data[key] = value
284
+ else
285
+ data[key] = value.to_s
286
+ end
287
+ end
288
+ elsif body
289
+ data = body.is_a?(String) ? body : body.to_json
290
+ else
291
+ data = nil
292
+ end
293
+ data
294
+ end
295
+
296
+ # Update hearder and query params based on authentication settings.
297
+ #
298
+ # @param [Hash] header_params Header parameters
299
+ # @param [Hash] query_params Query parameters
300
+ # @param [String] auth_names Authentication scheme name
301
+ def update_params_for_auth!(header_params, query_params, auth_names)
302
+ Array(auth_names).each do |auth_name|
303
+ auth_setting = @config.auth_settings[auth_name]
304
+ next unless auth_setting
305
+ case auth_setting[:in]
306
+ when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
307
+ when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
308
+ else fail ArgumentError, 'Authentication token must be in `query` of `header`'
309
+ end
310
+ end
311
+ end
312
+
313
+ # Sets user agent in HTTP header
314
+ #
315
+ # @param [String] user_agent User agent (e.g. swagger-codegen/ruby/1.0.0)
316
+ def user_agent=(user_agent)
317
+ @user_agent = user_agent
318
+ @default_headers['User-Agent'] = @user_agent
319
+ end
320
+
321
+ # Return Accept header based on an array of accepts provided.
322
+ # @param [Array] accepts array for Accept
323
+ # @return [String] the Accept header (e.g. application/json)
324
+ def select_header_accept(accepts)
325
+ return nil if accepts.nil? || accepts.empty?
326
+ # use JSON when present, otherwise use all of the provided
327
+ json_accept = accepts.find { |s| json_mime?(s) }
328
+ json_accept || accepts.join(',')
329
+ end
330
+
331
+ # Return Content-Type header based on an array of content types provided.
332
+ # @param [Array] content_types array for Content-Type
333
+ # @return [String] the Content-Type header (e.g. application/json)
334
+ def select_header_content_type(content_types)
335
+ # use application/json by default
336
+ return 'application/json' if content_types.nil? || content_types.empty?
337
+ # use JSON when present, otherwise use the first one
338
+ json_content_type = content_types.find { |s| json_mime?(s) }
339
+ json_content_type || content_types.first
340
+ end
341
+
342
+ # Convert object (array, hash, object, etc) to JSON string.
343
+ # @param [Object] model object to be converted into JSON string
344
+ # @return [String] JSON string representation of the object
345
+ def object_to_http_body(model)
346
+ return model if model.nil? || model.is_a?(String)
347
+ local_body = nil
348
+ if model.is_a?(Array)
349
+ local_body = model.map { |m| object_to_hash(m) }
350
+ else
351
+ local_body = object_to_hash(model)
352
+ end
353
+ local_body.to_json
354
+ end
355
+
356
+ # Convert object(non-array) to hash.
357
+ # @param [Object] obj object to be converted into JSON string
358
+ # @return [String] JSON string representation of the object
359
+ def object_to_hash(obj)
360
+ if obj.respond_to?(:to_hash)
361
+ obj.to_hash
362
+ else
363
+ obj
364
+ end
365
+ end
366
+
367
+ # Build parameter value according to the given collection format.
368
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
369
+ def build_collection_param(param, collection_format)
370
+ case collection_format
371
+ when :csv
372
+ param.join(',')
373
+ when :ssv
374
+ param.join(' ')
375
+ when :tsv
376
+ param.join("\t")
377
+ when :pipes
378
+ param.join('|')
379
+ when :multi
380
+ # return the array directly as typhoeus will handle it as expected
381
+ param
382
+ else
383
+ fail "unknown collection format: #{collection_format.inspect}"
384
+ end
385
+ end
386
+ end
387
+ end
@@ -0,0 +1,36 @@
1
+ =begin
2
+ #Pesaway Pesamoni API Documentation
3
+
4
+ Automate mobile money payments, bank transfers and more..
5
+
6
+ OpenAPI spec version: 1.0.3
7
+
8
+
9
+ =end
10
+
11
+ module Pesamoni
12
+ class ApiError < StandardError
13
+ attr_reader :code, :response_headers, :response_body
14
+
15
+ # Usage examples:
16
+ # ApiError.new
17
+ # ApiError.new("message")
18
+ # ApiError.new(:code => 500, :response_headers => {}, :response_body => "")
19
+ # ApiError.new(:code => 404, :message => "Not Found")
20
+ def initialize(arg = nil)
21
+ if arg.is_a? Hash
22
+ if arg.key?(:message) || arg.key?('message')
23
+ super(arg[:message] || arg['message'])
24
+ else
25
+ super arg
26
+ end
27
+
28
+ arg.each do |k, v|
29
+ instance_variable_set "@#{k}", v
30
+ end
31
+ else
32
+ super arg
33
+ end
34
+ end
35
+ end
36
+ end