tpaga 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/tpaga.rb +28 -21
- data/lib/tpaga/api/charge_api.rb +133 -100
- data/lib/tpaga/api/chargeback_api.rb +88 -0
- data/lib/tpaga/api/credit_card_api.rb +220 -0
- data/lib/tpaga/api/customer_api.rb +84 -326
- data/lib/tpaga/api/davi_plata_api.rb +340 -0
- data/lib/tpaga/models/address.rb +47 -54
- data/lib/tpaga/models/api_error.rb +38 -0
- data/lib/tpaga/models/api_errors_item.rb +60 -0
- data/lib/tpaga/models/base_object.rb +85 -0
- data/lib/tpaga/models/billing_address.rb +76 -0
- data/lib/tpaga/models/city.rb +40 -51
- data/lib/tpaga/models/credit_card.rb +148 -0
- data/lib/tpaga/models/credit_card_charge.rb +116 -0
- data/lib/tpaga/models/credit_card_create.rb +76 -0
- data/lib/tpaga/models/customer.rb +68 -63
- data/lib/tpaga/models/davi_plata.rb +92 -0
- data/lib/tpaga/models/davi_plata_charge.rb +108 -0
- data/lib/tpaga/models/davi_plata_chargeback.rb +60 -0
- data/lib/tpaga/models/davi_plata_verification.rb +44 -0
- data/lib/tpaga/monkey.rb +1 -1
- data/lib/tpaga/swagger.rb +64 -87
- data/lib/tpaga/swagger/configuration.rb +24 -27
- data/lib/tpaga/swagger/request.rb +247 -192
- data/lib/tpaga/swagger/response.rb +52 -53
- data/lib/tpaga/swagger/version.rb +3 -4
- data/tpaga.gemspec +8 -9
- metadata +20 -12
- data/lib/tpaga/models/billingaddress.rb +0 -75
- data/lib/tpaga/models/charge.rb +0 -91
- data/lib/tpaga/models/creditcard.rb +0 -107
- data/lib/tpaga/models/creditcardcreate.rb +0 -75
@@ -0,0 +1,60 @@
|
|
1
|
+
module Tpaga
|
2
|
+
#
|
3
|
+
class DaviPlataChargeback < BaseObject
|
4
|
+
attr_accessor :id, :davi_plata_charge, :davi_plata, :reimbursed
|
5
|
+
# attribute mapping from ruby-style variable name to JSON key
|
6
|
+
def self.attribute_map
|
7
|
+
{
|
8
|
+
|
9
|
+
#
|
10
|
+
:'id' => :'id',
|
11
|
+
|
12
|
+
#
|
13
|
+
:'davi_plata_charge' => :'daviPlataCharge',
|
14
|
+
|
15
|
+
#
|
16
|
+
:'davi_plata' => :'daviPlata',
|
17
|
+
|
18
|
+
#
|
19
|
+
:'reimbursed' => :'reimbursed'
|
20
|
+
|
21
|
+
}
|
22
|
+
end
|
23
|
+
|
24
|
+
# attribute type
|
25
|
+
def self.swagger_types
|
26
|
+
{
|
27
|
+
:'id' => :'string',
|
28
|
+
:'davi_plata_charge' => :'string',
|
29
|
+
:'davi_plata' => :'string',
|
30
|
+
:'reimbursed' => :'boolean'
|
31
|
+
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
def initialize(attributes = {})
|
36
|
+
return if !attributes.is_a?(Hash) || attributes.empty?
|
37
|
+
|
38
|
+
# convert string to symbol for hash key
|
39
|
+
attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
|
40
|
+
|
41
|
+
|
42
|
+
if attributes[:'id']
|
43
|
+
@id = attributes[:'id']
|
44
|
+
end
|
45
|
+
|
46
|
+
if attributes[:'daviPlataCharge']
|
47
|
+
@davi_plata_charge = attributes[:'daviPlataCharge']
|
48
|
+
end
|
49
|
+
|
50
|
+
if attributes[:'daviPlata']
|
51
|
+
@davi_plata = attributes[:'daviPlata']
|
52
|
+
end
|
53
|
+
|
54
|
+
if attributes[:'reimbursed']
|
55
|
+
@reimbursed = attributes[:'reimbursed']
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Tpaga
|
2
|
+
#
|
3
|
+
class DaviPlataVerification < BaseObject
|
4
|
+
attr_accessor :davi_plata, :pin_number
|
5
|
+
# attribute mapping from ruby-style variable name to JSON key
|
6
|
+
def self.attribute_map
|
7
|
+
{
|
8
|
+
|
9
|
+
#
|
10
|
+
:'davi_plata' => :'daviPlata',
|
11
|
+
|
12
|
+
#
|
13
|
+
:'pin_number' => :'pinNumber'
|
14
|
+
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
# attribute type
|
19
|
+
def self.swagger_types
|
20
|
+
{
|
21
|
+
:'davi_plata' => :'string',
|
22
|
+
:'pin_number' => :'string'
|
23
|
+
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
def initialize(attributes = {})
|
28
|
+
return if !attributes.is_a?(Hash) || attributes.empty?
|
29
|
+
|
30
|
+
# convert string to symbol for hash key
|
31
|
+
attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
|
32
|
+
|
33
|
+
|
34
|
+
if attributes[:'daviPlata']
|
35
|
+
@davi_plata = attributes[:'daviPlata']
|
36
|
+
end
|
37
|
+
|
38
|
+
if attributes[:'pinNumber']
|
39
|
+
@pin_number = attributes[:'pinNumber']
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/tpaga/monkey.rb
CHANGED
data/lib/tpaga/swagger.rb
CHANGED
@@ -1,99 +1,76 @@
|
|
1
1
|
require 'logger'
|
2
|
+
require 'json'
|
2
3
|
|
3
4
|
module Tpaga
|
4
|
-
module Swagger
|
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
|
-
# Add leading and trailing slashes to base_path
|
40
|
-
configuration.base_path = "/#{configuration.base_path}".gsub(/\/+/, '/')
|
41
|
-
configuration.base_path = "" if configuration.base_path == "/"
|
42
|
-
end
|
43
|
-
|
44
|
-
def authenticated?
|
45
|
-
Swagger.configuration.auth_token.present?
|
46
|
-
end
|
47
|
-
|
48
|
-
def de_authenticate
|
49
|
-
Swagger.configuration.auth_token = nil
|
50
|
-
end
|
51
|
-
|
52
|
-
def authenticate
|
53
|
-
return if Swagger.authenticated?
|
54
|
-
|
55
|
-
if Swagger.configuration.username.blank? || Swagger.configuration.password.blank?
|
56
|
-
raise ClientError, "Username and password are required to authenticate."
|
5
|
+
module Swagger
|
6
|
+
class << self
|
7
|
+
attr_accessor :logger
|
8
|
+
|
9
|
+
# A Swagger configuration object. Must act like a hash and return sensible
|
10
|
+
# values for all Swagger configuration options. See Swagger::Configuration.
|
11
|
+
attr_accessor :configuration
|
12
|
+
|
13
|
+
attr_accessor :resources
|
14
|
+
|
15
|
+
# Call this method to modify defaults in your initializers.
|
16
|
+
#
|
17
|
+
# @example
|
18
|
+
# Swagger.configure do |config|
|
19
|
+
# config.api_key = '1234567890abcdef' # api key authentication
|
20
|
+
# config.format = 'json' # optional, defaults to 'json'
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
def configure
|
24
|
+
yield(configuration) if block_given?
|
25
|
+
|
26
|
+
# Configure logger. Default to use Rails
|
27
|
+
self.logger ||= configuration.logger || (defined?(Rails) ? Rails.logger : Logger.new(STDOUT))
|
28
|
+
|
29
|
+
# remove :// from scheme
|
30
|
+
configuration.scheme.sub!(/:\/\//, '')
|
31
|
+
|
32
|
+
# remove http(s):// and anything after a slash
|
33
|
+
configuration.host.sub!(/https?:\/\//, '')
|
34
|
+
configuration.host = configuration.host.split('/').first
|
35
|
+
|
36
|
+
# Add leading and trailing slashes to base_path
|
37
|
+
configuration.base_path = "/#{configuration.base_path}".gsub(/\/+/, '/')
|
38
|
+
configuration.base_path = "" if configuration.base_path == "/"
|
57
39
|
end
|
58
|
-
|
59
|
-
request = Swagger::Request.new(
|
60
|
-
:get,
|
61
|
-
"account/authenticate/{username}",
|
62
|
-
:params => {
|
63
|
-
:username => Swagger.configuration.username,
|
64
|
-
:password => Swagger.configuration.password
|
65
|
-
}
|
66
|
-
)
|
67
|
-
|
68
|
-
response_body = request.response.body
|
69
|
-
Swagger.configuration.auth_token = response_body['token']
|
70
|
-
end
|
71
40
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
return obj.map{|x| to_body(x) } if obj.is_a? Array
|
76
|
-
return obj
|
77
|
-
end
|
41
|
+
def authenticated?
|
42
|
+
Swagger.configuration.auth_token.present?
|
43
|
+
end
|
78
44
|
|
79
|
-
|
80
|
-
|
81
|
-
end
|
45
|
+
def de_authenticate
|
46
|
+
Swagger.configuration.auth_token = nil
|
47
|
+
end
|
82
48
|
|
83
|
-
|
84
|
-
|
49
|
+
def authenticate
|
50
|
+
return if Swagger.authenticated?
|
85
51
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
end
|
90
|
-
end
|
52
|
+
if Swagger.configuration.username.blank? || Swagger.configuration.password.blank?
|
53
|
+
raise ClientError, "Username and password are required to authenticate."
|
54
|
+
end
|
91
55
|
|
92
|
-
|
93
|
-
|
56
|
+
request = Swagger::Request.new(
|
57
|
+
:get,
|
58
|
+
"account/authenticate/{username}",
|
59
|
+
:params => {
|
60
|
+
:username => Swagger.configuration.username,
|
61
|
+
:password => Swagger.configuration.password
|
62
|
+
}
|
63
|
+
)
|
94
64
|
|
95
|
-
|
96
|
-
|
65
|
+
response_body = request.response.body
|
66
|
+
Swagger.configuration.auth_token = response_body['token']
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
97
70
|
|
98
|
-
|
71
|
+
class ServerError < StandardError
|
72
|
+
end
|
99
73
|
|
74
|
+
class ClientError < StandardError
|
75
|
+
end
|
76
|
+
end
|
@@ -1,32 +1,29 @@
|
|
1
1
|
module Tpaga
|
2
|
-
module Swagger
|
2
|
+
module Swagger
|
3
|
+
class Configuration
|
4
|
+
attr_accessor :format, :api_key, :api_key_prefix, :username, :password, :auth_token, :scheme, :host, :base_path, :user_agent, :logger, :inject_format, :force_ending_format, :camelize_params, :user_agent, :verify_ssl
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
@user_agent = 'Swagger/Ruby/0.0.2/dev'
|
15
|
-
@inject_format = false
|
16
|
-
@force_ending_format = false
|
17
|
-
@camelize_params = false
|
18
|
-
# for API key/token authentication
|
19
|
-
@api_key = '';
|
20
|
-
@api_key_type = '';
|
21
|
-
@api_key_name = '';
|
22
|
-
@api_key_prefix = '';
|
23
|
-
# for http basic authentication
|
24
|
-
@username = ''
|
25
|
-
@password = ''
|
26
|
-
end
|
6
|
+
# Defaults go in here..
|
7
|
+
def initialize
|
8
|
+
@format = 'json'
|
9
|
+
@scheme = 'https'
|
10
|
+
@host = 'sandbox.tpaga.co'
|
11
|
+
@base_path = '/api'
|
12
|
+
@user_agent = "ruby-swagger-#{Swagger::VERSION}"
|
13
|
+
@inject_format = false
|
14
|
+
@force_ending_format = false
|
15
|
+
@camelize_params = true
|
27
16
|
|
28
|
-
|
17
|
+
# keys for API key authentication (param-name => api-key)
|
18
|
+
@api_key = {}
|
19
|
+
# api-key prefix for API key authentication, e.g. "Bearer" (param-name => api-key-prefix)
|
20
|
+
@api_key_prefix = {}
|
29
21
|
|
22
|
+
# whether to verify SSL certificate, default to true
|
23
|
+
# Note: do NOT set it to false in production code, otherwise you would
|
24
|
+
# face multiple types of cryptographic attacks
|
25
|
+
@verify_ssl = true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
30
29
|
end
|
31
|
-
end
|
32
|
-
|
@@ -1,215 +1,270 @@
|
|
1
1
|
module Tpaga
|
2
|
-
module Swagger
|
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
|
-
|
2
|
+
module Swagger
|
3
|
+
class Request
|
4
|
+
require 'uri'
|
5
|
+
require 'addressable/uri'
|
6
|
+
require 'typhoeus'
|
7
|
+
|
8
|
+
attr_accessor :host, :path, :format, :params, :body, :http_method, :headers, :form_params, :auth_names
|
9
|
+
|
10
|
+
# All requests must have an HTTP method and a path
|
11
|
+
# Optionals parameters are :params, :headers, :body, :format, :host
|
12
|
+
def initialize(http_method, path, attributes={})
|
13
|
+
attributes[:format] ||= Swagger.configuration.format
|
14
|
+
attributes[:params] ||= {}
|
15
|
+
|
16
|
+
# Set default headers
|
17
|
+
default_headers = {
|
18
|
+
'Content-Type' => "application/#{attributes[:format].downcase}",
|
19
|
+
'User-Agent' => Swagger.configuration.user_agent
|
20
|
+
}
|
21
|
+
|
22
|
+
# Merge argument headers into defaults
|
23
|
+
attributes[:headers] = default_headers.merge(attributes[:headers] || {})
|
24
|
+
|
25
|
+
# Stick in the auth token if there is one
|
26
|
+
if Swagger.authenticated?
|
27
|
+
attributes[:headers].merge!({:auth_token => Swagger.configuration.auth_token})
|
28
|
+
end
|
29
|
+
|
30
|
+
self.http_method = http_method.to_sym
|
31
|
+
self.path = path
|
32
|
+
attributes.each do |name, value|
|
33
|
+
send("#{name.to_s.underscore.to_sym}=", value)
|
34
|
+
end
|
35
|
+
|
36
|
+
update_params_for_auth!
|
37
|
+
end
|
38
|
+
|
39
|
+
# Update hearder and query params based on authentication settings.
|
40
|
+
def update_params_for_auth!
|
41
|
+
(@auth_names || []).each do |auth_name|
|
42
|
+
case auth_name
|
43
|
+
when 'api_key'
|
44
|
+
@headers ||= {}
|
45
|
+
http_auth_header = 'Basic ' + ["#{Swagger.configuration.api_key}:"].pack('m').delete("\r\n")
|
46
|
+
@headers['Authorization'] = http_auth_header
|
47
|
+
|
31
48
|
end
|
32
|
-
|
33
|
-
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# Get API key (with prefix if set).
|
53
|
+
# @param [String] param_name the parameter name of API key auth
|
54
|
+
def get_api_key_with_prefix(param_name)
|
55
|
+
if Swagger.configuration.api_key_prefix[param_name].present?
|
56
|
+
"#{Swagger.configuration.api_key_prefix[param_name]} #{Swagger.configuration.api_key[param_name]}"
|
34
57
|
else
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
58
|
+
Swagger.configuration.api_key[param_name]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Construct a base URL
|
63
|
+
def url(options = {})
|
64
|
+
u = Addressable::URI.new(
|
65
|
+
:scheme => Swagger.configuration.scheme,
|
66
|
+
:host => Swagger.configuration.host,
|
67
|
+
:path => self.interpreted_path,
|
68
|
+
:query => self.query_string.sub(/\?/, '')
|
69
|
+
).to_s
|
70
|
+
|
71
|
+
# Drop trailing question mark, if present
|
72
|
+
u.sub! /\?$/, ''
|
73
|
+
|
74
|
+
u
|
75
|
+
end
|
76
|
+
|
77
|
+
# Iterate over the params hash, injecting any path values into the path string
|
78
|
+
# e.g. /word.{format}/{word}/entries => /word.json/cat/entries
|
79
|
+
def interpreted_path
|
80
|
+
p = self.path.dup
|
81
|
+
|
82
|
+
# Stick a .{format} placeholder into the path if there isn't
|
83
|
+
# one already or an actual format like json or xml
|
84
|
+
# e.g. /words/blah => /words.{format}/blah
|
85
|
+
if Swagger.configuration.inject_format
|
86
|
+
unless ['.json', '.xml', '{format}'].any? {|s| p.downcase.include? s }
|
87
|
+
p = p.sub(/^(\/?\w+)/, "\\1.#{format}")
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
# Stick a .{format} placeholder on the end of the path if there isn't
|
92
|
+
# one already or an actual format like json or xml
|
93
|
+
# e.g. /words/blah => /words/blah.{format}
|
94
|
+
if Swagger.configuration.force_ending_format
|
95
|
+
unless ['.json', '.xml', '{format}'].any? {|s| p.downcase.include? s }
|
96
|
+
p = "#{p}.#{format}"
|
41
97
|
end
|
42
98
|
end
|
99
|
+
|
100
|
+
p = p.sub("{format}", self.format.to_s)
|
101
|
+
|
102
|
+
URI.encode [Swagger.configuration.base_path, p].join("/").gsub(/\/+/, '/')
|
43
103
|
end
|
44
104
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
105
|
+
# Massage the request body into a state of readiness
|
106
|
+
# If body is a hash, camelize all keys then convert to a json string
|
107
|
+
def body=(value)
|
108
|
+
if value.is_a?(Hash)
|
109
|
+
value = value.inject({}) do |memo, (k,v)|
|
110
|
+
memo[k.to_s.camelize(:lower).to_sym] = v
|
111
|
+
memo
|
112
|
+
end
|
113
|
+
end
|
114
|
+
@body = value
|
51
115
|
end
|
52
|
-
end
|
53
116
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
u
|
72
|
-
end
|
117
|
+
# If body is an object, JSONify it before making the actual request.
|
118
|
+
# For form parameters, remove empty value
|
119
|
+
def outgoing_body
|
120
|
+
# http form
|
121
|
+
if headers['Content-Type'] == 'application/x-www-form-urlencoded'
|
122
|
+
data = form_params.dup
|
123
|
+
data.each do |key, value|
|
124
|
+
data[key] = value.to_s if value && !value.is_a?(File) # remove emtpy form parameter
|
125
|
+
end
|
126
|
+
data
|
127
|
+
elsif @body # http body is JSON
|
128
|
+
@body.is_a?(String) ? @body : @body.to_json
|
129
|
+
else
|
130
|
+
nil
|
131
|
+
end
|
132
|
+
end
|
73
133
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
134
|
+
# Construct a query string from the query-string-type params
|
135
|
+
def query_string
|
136
|
+
# Iterate over all params,
|
137
|
+
# .. removing the ones that are part of the path itself.
|
138
|
+
# .. stringifying values so Addressable doesn't blow up.
|
139
|
+
query_values = {}
|
140
|
+
self.params.each_pair do |key, value|
|
141
|
+
next if self.path.include? "{#{key}}" # skip path params
|
142
|
+
next if value.blank? && value.class != FalseClass # skip empties
|
143
|
+
if Swagger.configuration.camelize_params
|
144
|
+
key = key.to_s.camelize(:lower).to_sym
|
145
|
+
end
|
146
|
+
query_values[key] = value.to_s
|
147
|
+
end
|
79
148
|
|
80
|
-
|
81
|
-
|
82
|
-
|
149
|
+
# We don't want to end up with '?' as our query string
|
150
|
+
# if there aren't really any params
|
151
|
+
return "" if query_values.blank?
|
152
|
+
|
153
|
+
# Addressable requires query_values to be set after initialization..
|
154
|
+
qs = Addressable::URI.new
|
155
|
+
qs.query_values = query_values
|
156
|
+
qs.to_s
|
83
157
|
end
|
84
158
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
159
|
+
def make
|
160
|
+
#TODO use configuration setting to determine if debugging
|
161
|
+
#logger = Logger.new STDOUT
|
162
|
+
#logger.debug self.url
|
163
|
+
|
164
|
+
request_options = {
|
165
|
+
:ssl_verifypeer => Swagger.configuration.verify_ssl,
|
166
|
+
:headers => self.headers.stringify_keys
|
167
|
+
}
|
168
|
+
response = case self.http_method.to_sym
|
169
|
+
when :get,:GET
|
170
|
+
Typhoeus::Request.get(
|
171
|
+
self.url,
|
172
|
+
request_options
|
173
|
+
)
|
174
|
+
|
175
|
+
when :post,:POST
|
176
|
+
Typhoeus::Request.post(
|
177
|
+
self.url,
|
178
|
+
request_options.merge(:body => self.outgoing_body)
|
179
|
+
)
|
180
|
+
|
181
|
+
when :patch,:PATCH
|
182
|
+
Typhoeus::Request.patch(
|
183
|
+
self.url,
|
184
|
+
request_options.merge(:body => self.outgoing_body)
|
185
|
+
)
|
186
|
+
|
187
|
+
when :put,:PUT
|
188
|
+
Typhoeus::Request.put(
|
189
|
+
self.url,
|
190
|
+
request_options.merge(:body => self.outgoing_body)
|
191
|
+
)
|
192
|
+
|
193
|
+
when :delete,:DELETE
|
194
|
+
Typhoeus::Request.delete(
|
195
|
+
self.url,
|
196
|
+
request_options.merge(:body => self.outgoing_body)
|
197
|
+
)
|
91
198
|
end
|
199
|
+
Response.new(response)
|
92
200
|
end
|
93
201
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
202
|
+
def response
|
203
|
+
self.make
|
204
|
+
end
|
205
|
+
|
206
|
+
def response_code_pretty
|
207
|
+
return unless @response.present?
|
208
|
+
@response.code.to_s
|
209
|
+
end
|
210
|
+
|
211
|
+
def response_headers_pretty
|
212
|
+
return unless @response.present?
|
213
|
+
# JSON.pretty_generate(@response.headers).gsub(/\n/, '<br/>') # <- This was for RestClient
|
214
|
+
@response.headers.gsub(/\n/, '<br/>') # <- This is for Typhoeus
|
215
|
+
end
|
216
|
+
|
217
|
+
# return 'Accept' based on an array of accept provided
|
218
|
+
# @param [Array] header_accept_array Array fo 'Accept'
|
219
|
+
# @return String Accept (e.g. application/json)
|
220
|
+
def self.select_header_accept header_accept_array
|
221
|
+
if header_accept_array.empty?
|
222
|
+
return
|
223
|
+
elsif header_accept_array.any?{ |s| s.casecmp('application/json')==0 }
|
224
|
+
'application/json' # look for json data by default
|
225
|
+
else
|
226
|
+
header_accept_array.join(',')
|
100
227
|
end
|
101
228
|
end
|
102
229
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
#if value.is_a?(Hash)
|
114
|
-
# value = value.inject({}) do |memo, (k,v)|
|
115
|
-
# memo[k.to_s.camelize(:lower).to_sym] = v
|
116
|
-
# memo
|
117
|
-
# end
|
118
|
-
#end
|
119
|
-
@body = value
|
120
|
-
end
|
121
|
-
|
122
|
-
# If body is an object, JSONify it before making the actual request.
|
123
|
-
#
|
124
|
-
def outgoing_body
|
125
|
-
if @body.nil? && @form_params && !@form_params.empty?
|
126
|
-
data = form_params.dup
|
127
|
-
data.each do |key, value|
|
128
|
-
data[key] = value.to_s if value && !value.is_a?(File)
|
230
|
+
# return the content type based on an array of content-type provided
|
231
|
+
# @param [Array] content_type_array Array fo content-type
|
232
|
+
# @return String Content-Type (e.g. application/json)
|
233
|
+
def self.select_header_content_type content_type_array
|
234
|
+
if content_type_array.empty?
|
235
|
+
'application/json' # use application/json by default
|
236
|
+
elsif content_type_array.any?{ |s| s.casecmp('application/json')==0 }
|
237
|
+
'application/json' # use application/json if it's included
|
238
|
+
else
|
239
|
+
content_type_array[0]; # otherwise, use the first one
|
129
240
|
end
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
next if value.blank? && value.class != FalseClass # skip empties
|
146
|
-
|
147
|
-
# no longer support Swagger.configuration.camelize_params
|
148
|
-
#if Swagger.configuration.camelize_params
|
149
|
-
# key = key.to_s.camelize(:lower).to_sym unless key.to_sym == :api_key # api_key is not a camelCased param
|
150
|
-
#end
|
151
|
-
|
152
|
-
query_values[key] = value.to_s
|
153
|
-
end
|
154
|
-
|
155
|
-
# We don't want to end up with '?' as our query string
|
156
|
-
# if there aren't really any params
|
157
|
-
return "" if query_values.blank?
|
158
|
-
|
159
|
-
# Addressable requires query_values to be set after initialization..
|
160
|
-
qs = Addressable::URI.new
|
161
|
-
qs.query_values = query_values
|
162
|
-
qs.to_s
|
163
|
-
end
|
164
|
-
|
165
|
-
def make
|
166
|
-
response = case self.http_method.to_sym
|
167
|
-
when :get,:GET
|
168
|
-
Typhoeus::Request.get(
|
169
|
-
self.url,
|
170
|
-
:headers => self.headers.stringify_keys,
|
171
|
-
)
|
172
|
-
|
173
|
-
when :post,:POST
|
174
|
-
Typhoeus::Request.post(
|
175
|
-
self.url,
|
176
|
-
:body => self.outgoing_body,
|
177
|
-
:headers => self.headers.stringify_keys,
|
178
|
-
)
|
179
|
-
|
180
|
-
when :put,:PUT
|
181
|
-
Typhoeus::Request.put(
|
182
|
-
self.url,
|
183
|
-
:body => self.outgoing_body,
|
184
|
-
:headers => self.headers.stringify_keys,
|
185
|
-
)
|
186
|
-
|
187
|
-
when :delete,:DELETE
|
188
|
-
Typhoeus::Request.delete(
|
189
|
-
self.url,
|
190
|
-
:body => self.outgoing_body,
|
191
|
-
:headers => self.headers.stringify_keys,
|
192
|
-
)
|
193
|
-
end
|
194
|
-
Response.new(response)
|
195
|
-
end
|
196
|
-
|
197
|
-
def response
|
198
|
-
self.make
|
199
|
-
end
|
200
|
-
|
201
|
-
def response_code_pretty
|
202
|
-
return unless @response.present?
|
203
|
-
@response.code.to_s
|
204
|
-
end
|
205
|
-
|
206
|
-
def response_headers_pretty
|
207
|
-
return unless @response.present?
|
208
|
-
# JSON.pretty_generate(@response.headers).gsub(/\n/, '<br/>') # <- This was for RestClient
|
209
|
-
@response.headers.gsub(/\n/, '<br/>') # <- This is for Typhoeus
|
210
|
-
end
|
241
|
+
end
|
242
|
+
|
243
|
+
# static method to convert object (array, hash, object, etc) to JSON string
|
244
|
+
# @param model object to be converted into JSON string
|
245
|
+
# @return string JSON string representation of the object
|
246
|
+
def self.object_to_http_body model
|
247
|
+
return if model.nil?
|
248
|
+
_body = nil
|
249
|
+
if model.is_a?(Array)
|
250
|
+
_body = model.map{|m| object_to_hash(m) }
|
251
|
+
else
|
252
|
+
_body = object_to_hash(model)
|
253
|
+
end
|
254
|
+
_body.to_json
|
255
|
+
end
|
211
256
|
|
257
|
+
# static method to convert object(non-array) to hash
|
258
|
+
# @param obj object to be converted into JSON string
|
259
|
+
# @return string JSON string representation of the object
|
260
|
+
def self.object_to_hash obj
|
261
|
+
if obj.respond_to?(:to_hash)
|
262
|
+
obj.to_hash
|
263
|
+
else
|
264
|
+
obj
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
end
|
212
269
|
end
|
213
270
|
end
|
214
|
-
end
|
215
|
-
|