taxamo 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +7 -0
- data/LICENSE +11 -0
- data/README.md +120 -0
- data/Rakefile +7 -0
- data/lib/monkey.rb +90 -0
- data/lib/swagger.rb +93 -0
- data/lib/swagger/configuration.rb +21 -0
- data/lib/swagger/request.rb +198 -0
- data/lib/swagger/response.rb +87 -0
- data/lib/taxamo.rb +999 -0
- data/lib/taxamo/models/additionalcurrencies.rb +62 -0
- data/lib/taxamo/models/additionalcurrency.rb +82 -0
- data/lib/taxamo/models/bycountry.rb +77 -0
- data/lib/taxamo/models/bystatus.rb +69 -0
- data/lib/taxamo/models/bytaxationtype.rb +82 -0
- data/lib/taxamo/models/c.rb +77 -0
- data/lib/taxamo/models/calculatesimpletaxout.rb +62 -0
- data/lib/taxamo/models/calculatetaxin.rb +62 -0
- data/lib/taxamo/models/calculatetaxlocationout.rb +97 -0
- data/lib/taxamo/models/calculatetaxout.rb +62 -0
- data/lib/taxamo/models/canceltransactionout.rb +62 -0
- data/lib/taxamo/models/capturepaymentout.rb +62 -0
- data/lib/taxamo/models/confirmtransactionin.rb +62 -0
- data/lib/taxamo/models/confirmtransactionout.rb +62 -0
- data/lib/taxamo/models/countries.rb +112 -0
- data/lib/taxamo/models/country.rb +114 -0
- data/lib/taxamo/models/countryschema.rb +114 -0
- data/lib/taxamo/models/createpaymentin.rb +72 -0
- data/lib/taxamo/models/createpaymentout.rb +62 -0
- data/lib/taxamo/models/createrefundin.rb +77 -0
- data/lib/taxamo/models/createrefundout.rb +77 -0
- data/lib/taxamo/models/createsmstokenin.rb +67 -0
- data/lib/taxamo/models/createsmstokenout.rb +62 -0
- data/lib/taxamo/models/createtransactionin.rb +67 -0
- data/lib/taxamo/models/createtransactionout.rb +62 -0
- data/lib/taxamo/models/currencyschema.rb +82 -0
- data/lib/taxamo/models/customfields.rb +67 -0
- data/lib/taxamo/models/emailinvoicein.rb +62 -0
- data/lib/taxamo/models/emailinvoiceout.rb +62 -0
- data/lib/taxamo/models/evidence.rb +112 -0
- data/lib/taxamo/models/evidenceschema.rb +77 -0
- data/lib/taxamo/models/getcountriesdictout.rb +63 -0
- data/lib/taxamo/models/getcurrenciesdictout.rb +63 -0
- data/lib/taxamo/models/getdailysettlementstatsout.rb +63 -0
- data/lib/taxamo/models/getproducttypesdictout.rb +63 -0
- data/lib/taxamo/models/getrefundsout.rb +63 -0
- data/lib/taxamo/models/getsettlementout.rb +83 -0
- data/lib/taxamo/models/getsettlementstatsbycountryout.rb +63 -0
- data/lib/taxamo/models/getsettlementstatsbytaxationtypeout.rb +62 -0
- data/lib/taxamo/models/getsettlementsummaryout.rb +62 -0
- data/lib/taxamo/models/gettransactionout.rb +62 -0
- data/lib/taxamo/models/gettransactionsstatsout.rb +62 -0
- data/lib/taxamo/models/inputtransaction.rb +184 -0
- data/lib/taxamo/models/inputtransactionline.rb +133 -0
- data/lib/taxamo/models/inputtransactionupdate.rb +184 -0
- data/lib/taxamo/models/invoiceaddress.rb +97 -0
- data/lib/taxamo/models/listpaymentsout.rb +63 -0
- data/lib/taxamo/models/listtransactionsout.rb +63 -0
- data/lib/taxamo/models/locategivenipout.rb +72 -0
- data/lib/taxamo/models/locatemyipout.rb +72 -0
- data/lib/taxamo/models/n.rb +77 -0
- data/lib/taxamo/models/payments.rb +72 -0
- data/lib/taxamo/models/producttypeschema.rb +62 -0
- data/lib/taxamo/models/report.rb +92 -0
- data/lib/taxamo/models/settlementdailystatsschema.rb +102 -0
- data/lib/taxamo/models/summary.rb +87 -0
- data/lib/taxamo/models/transaction.rb +279 -0
- data/lib/taxamo/models/transactionlines.rb +163 -0
- data/lib/taxamo/models/transactions.rb +279 -0
- data/lib/taxamo/models/unconfirmtransactionin.rb +62 -0
- data/lib/taxamo/models/unconfirmtransactionout.rb +62 -0
- data/lib/taxamo/models/updatetransactionin.rb +62 -0
- data/lib/taxamo/models/updatetransactionout.rb +62 -0
- data/lib/taxamo/models/validatetaxnumberout.rb +77 -0
- data/lib/taxamo/models/verifysmstokenout.rb +62 -0
- data/lib/taxamo/version.rb +17 -0
- data/taxamo.gemspec +31 -0
- data/test/taxamo/connectivity_test.rb +55 -0
- data/test/taxamo/tax_test.rb +147 -0
- data/test/taxamo/transactions_api_test.rb +308 -0
- metadata +226 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8cf5f76565fff95a1fbae895d217fa71bb049be4
|
4
|
+
data.tar.gz: 23aaffe3c9121060f3491dcf3ece72fce0d645af
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: caa975d864fc42417e2d14109f6d2bfba405757f32590139d0b6efc00ffce06d416cce563c7a1922f6d99c256e574ba507ec652c232d077748fee1e527c45c2e
|
7
|
+
data.tar.gz: 3cfc7cd62e1e281a74df0450a48852ed631c470520863a9f1746660bf4c196df94ed3700ad59e926d1a476472a28f23f487718f60f81b269f2aab605a536d63e
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
Copyright 2014-2015 Taxamo, Ltd.
|
2
|
+
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
you may not use this file except in compliance with the License.
|
5
|
+
You may obtain a copy of the License at [apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)
|
6
|
+
|
7
|
+
Unless required by applicable law or agreed to in writing, software
|
8
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
9
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
10
|
+
See the License for the specific language governing permissions and
|
11
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
# Taxamo API Ruby client
|
2
|
+
|
3
|
+
This package provides [Taxamo](http://www.taxamo.com) API bindings for Ruby and was generated using Swagger-codegen.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
To install it, just fetch this repository and issue the following command:
|
8
|
+
|
9
|
+
```shell
|
10
|
+
$ gem build taxamo.gemspec
|
11
|
+
$ gem install taxamo-1.0.4.gem
|
12
|
+
```
|
13
|
+
|
14
|
+
Alternatively, add the following line to your project's `Gemfile` file:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem "taxamo", github: "taxamo/taxamo-ruby"
|
18
|
+
```
|
19
|
+
|
20
|
+
## Changes
|
21
|
+
|
22
|
+
1.0.5 (2015-07-02):
|
23
|
+
* sync with the newest API model, including addition of getsettlementdailystats operation
|
24
|
+
|
25
|
+
1.0.4 (2015-04-15):
|
26
|
+
* sync with the newest API data model, including unconfirm transaction and email invoice operations and a default currency code for each country
|
27
|
+
* please note, that the list_transactions operation has new argument: invoice_number, changing the argument count to 12
|
28
|
+
|
29
|
+
1.0.3 (2015-04-15):
|
30
|
+
* use require_rel to make the require work properly when using bundler to manage the dependencies
|
31
|
+
|
32
|
+
1.0.2 (2015-03-16):
|
33
|
+
* Refresh CreateRefundOut with new field and updated documentation
|
34
|
+
|
35
|
+
1.0.1:
|
36
|
+
|
37
|
+
* Added buyer_tax_number_valid field to tax calculation, transaction storage and VAT number validation operations
|
38
|
+
* Added key_or_custom_id to list_transactions operation - please note, that the number of arguments has changed!
|
39
|
+
|
40
|
+
## Usage
|
41
|
+
|
42
|
+
First of all, you need to set your private test or live token:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
require 'taxamo'
|
46
|
+
|
47
|
+
Swagger.configure do |config|
|
48
|
+
config.api_key = 'YOUR_TOKEN_HERE'
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
Next, you can invoke the API as functions in Taxamo module:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
resp = Taxamo.get_transaction('SOME_TRANSACTION_ID')
|
56
|
+
```
|
57
|
+
|
58
|
+
It is possible to use hashes and arrays as input:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
resp = Taxamo.calculate_tax(
|
62
|
+
{'transaction' => {
|
63
|
+
'currency_code' => 'USD',
|
64
|
+
'buyer_ip' => '127.0.0.1',
|
65
|
+
'billing_country_code' => 'IE',
|
66
|
+
'force_country_code' => 'FR',
|
67
|
+
'transaction_lines' => [{'amount' => 200,
|
68
|
+
'custom_id' => 'line1'},
|
69
|
+
{'amount' => 100,
|
70
|
+
'product_type' => 'e-book',
|
71
|
+
'custom_id' => 'line2'}]}})
|
72
|
+
```
|
73
|
+
|
74
|
+
But also regular classes:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
taxIn = CalculateTaxIn.new
|
78
|
+
taxIn.transaction = InputTransaction.new
|
79
|
+
taxIn.transaction.currency_code = 'USD'
|
80
|
+
taxIn.transaction.buyer_ip = '127.0.0.1'
|
81
|
+
taxIn.transaction.billing_country_code = 'IE'
|
82
|
+
taxIn.transaction.force_country_code = 'FR'
|
83
|
+
taxIn.transaction.transaction_lines = [InputTransactionLine.new, InputTransactionLine.new]
|
84
|
+
taxIn.transaction.transaction_lines[0].amount = 200
|
85
|
+
taxIn.transaction.transaction_lines[0].custom_id = 'line1'
|
86
|
+
taxIn.transaction.transaction_lines[1].amount = 100
|
87
|
+
taxIn.transaction.transaction_lines[1].custom_id = 'line2'
|
88
|
+
taxIn.transaction.transaction_lines[1].product_type = 'e-book'
|
89
|
+
|
90
|
+
resp = Taxamo.calculate_tax(taxIn)
|
91
|
+
```
|
92
|
+
|
93
|
+
The output is always coerced to appropriate classes:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
assert_equal resp.transaction.countries.detected.code, 'IE'
|
97
|
+
assert_equal resp.transaction.amount, 300
|
98
|
+
assert_equal resp.transaction.tax_amount, 45.5
|
99
|
+
assert_equal resp.transaction.total_amount, 345.5
|
100
|
+
```
|
101
|
+
|
102
|
+
See the regression tests for more examples.
|
103
|
+
|
104
|
+
## Documentation
|
105
|
+
|
106
|
+
Please see http://www.taxamo.com/documentation/ for the most up-to-date documentation.
|
107
|
+
|
108
|
+
## License
|
109
|
+
|
110
|
+
Copyright 2014-2015 Taxamo, Ltd.
|
111
|
+
|
112
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
113
|
+
you may not use this file except in compliance with the License.
|
114
|
+
You may obtain a copy of the License at [apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)
|
115
|
+
|
116
|
+
Unless required by applicable law or agreed to in writing, software
|
117
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
118
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
119
|
+
See the License for the specific language governing permissions and
|
120
|
+
limitations under the License.
|
data/Rakefile
ADDED
data/lib/monkey.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# module Swagger
|
2
|
+
class Object
|
3
|
+
|
4
|
+
unless Object.method_defined? :blank?
|
5
|
+
def blank?
|
6
|
+
respond_to?(:empty?) ? empty? : !self
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
unless Object.method_defined? :present?
|
11
|
+
def present?
|
12
|
+
!blank?
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
class String
|
19
|
+
|
20
|
+
unless String.method_defined? :underscore
|
21
|
+
def underscore
|
22
|
+
self.gsub(/::/, '/').
|
23
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
24
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
25
|
+
tr("-", "_").
|
26
|
+
downcase
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
unless String.method_defined? :camelize
|
31
|
+
def camelize(first_letter_in_uppercase = true)
|
32
|
+
if first_letter_in_uppercase != :lower
|
33
|
+
self.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
34
|
+
else
|
35
|
+
self.to_s[0].chr.downcase + camelize(self)[1..-1]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
class Hash
|
43
|
+
|
44
|
+
unless Hash.method_defined? :stringify_keys
|
45
|
+
def stringify_keys
|
46
|
+
inject({}) do |options, (key, value)|
|
47
|
+
options[key.to_s] = value
|
48
|
+
options
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
unless Hash.method_defined? :stringify_keys!
|
54
|
+
def stringify_keys!
|
55
|
+
self.replace(self.stringify_keys)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
unless Hash.method_defined? :symbolize_keys
|
60
|
+
def symbolize_keys
|
61
|
+
inject({}) do |options, (key, value)|
|
62
|
+
options[(key.to_sym rescue key) || key] = value
|
63
|
+
options
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
unless Hash.method_defined? :symbolize_keys!
|
69
|
+
def symbolize_keys!
|
70
|
+
self.replace(self.symbolize_keys)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
unless Hash.method_defined? :symbolize_and_underscore_keys
|
75
|
+
def symbolize_and_underscore_keys
|
76
|
+
inject({}) do |options, (key, value)|
|
77
|
+
options[(key.to_s.underscore.to_sym rescue key) || key] = value
|
78
|
+
options
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
unless Hash.method_defined? :symbolize_and_underscore_keys!
|
84
|
+
def symbolize_and_underscore_keys!
|
85
|
+
self.replace(self.symbolize_and_underscore_keys)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
# end
|
data/lib/swagger.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'monkey'
|
2
|
+
require 'swagger/configuration'
|
3
|
+
require 'swagger/request'
|
4
|
+
require 'swagger/response'
|
5
|
+
require 'logger'
|
6
|
+
|
7
|
+
module Swagger
|
8
|
+
|
9
|
+
class << self
|
10
|
+
attr_accessor :logger
|
11
|
+
|
12
|
+
# A Swagger configuration object. Must act like a hash and return sensible
|
13
|
+
# values for all Swagger configuration options. See Swagger::Configuration.
|
14
|
+
attr_accessor :configuration
|
15
|
+
|
16
|
+
attr_accessor :resources
|
17
|
+
|
18
|
+
# Call this method to modify defaults in your initializers.
|
19
|
+
#
|
20
|
+
# @example
|
21
|
+
# Swagger.configure do |config|
|
22
|
+
# config.api_key = '1234567890abcdef' # required
|
23
|
+
# config.username = 'wordlover' # optional, but needed for user-related functions
|
24
|
+
# config.password = 'i<3words' # optional, but needed for user-related functions
|
25
|
+
# config.format = 'json' # optional, defaults to 'json'
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
def configure
|
29
|
+
self.configuration ||= Configuration.new
|
30
|
+
yield(configuration) if block_given?
|
31
|
+
|
32
|
+
# Configure logger. Default to use Rails
|
33
|
+
self.logger ||= configuration.logger || (defined?(Rails) ? Rails.logger : Logger.new(STDOUT))
|
34
|
+
|
35
|
+
# remove :// from scheme
|
36
|
+
configuration.scheme.sub!(/:\/\//, '')
|
37
|
+
|
38
|
+
# remove http(s):// and anything after a slash
|
39
|
+
configuration.host.sub!(/https?:\/\//, '')
|
40
|
+
configuration.host = configuration.host.split('/').first
|
41
|
+
|
42
|
+
# Add leading and trailing slashes to base_path
|
43
|
+
configuration.base_path = "/#{configuration.base_path}".gsub(/\/+/, '/')
|
44
|
+
configuration.base_path = "" if configuration.base_path == "/"
|
45
|
+
end
|
46
|
+
|
47
|
+
def authenticated?
|
48
|
+
Swagger.configuration.auth_token.present?
|
49
|
+
end
|
50
|
+
|
51
|
+
def de_authenticate
|
52
|
+
Swagger.configuration.auth_token = nil
|
53
|
+
end
|
54
|
+
|
55
|
+
def authenticate
|
56
|
+
return if Swagger.authenticated?
|
57
|
+
|
58
|
+
if Swagger.configuration.username.blank? || Swagger.configuration.password.blank?
|
59
|
+
raise ClientError, "Username and password are required to authenticate."
|
60
|
+
end
|
61
|
+
|
62
|
+
request = Swagger::Request.new(
|
63
|
+
:get,
|
64
|
+
"account/authenticate/{username}",
|
65
|
+
:params => {
|
66
|
+
:username => Swagger.configuration.username,
|
67
|
+
:password => Swagger.configuration.password
|
68
|
+
}
|
69
|
+
)
|
70
|
+
|
71
|
+
response_body = request.response.body
|
72
|
+
Swagger.configuration.auth_token = response_body['token']
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
class ServerError < StandardError
|
80
|
+
end
|
81
|
+
|
82
|
+
class ValidationError < StandardError
|
83
|
+
attr_accessor :validation_failures
|
84
|
+
end
|
85
|
+
|
86
|
+
class NotFoundError < StandardError
|
87
|
+
end
|
88
|
+
|
89
|
+
class AuthenticationError < StandardError
|
90
|
+
end
|
91
|
+
|
92
|
+
class ClientError < StandardError
|
93
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Swagger
|
2
|
+
|
3
|
+
class Configuration
|
4
|
+
|
5
|
+
attr_accessor :format, :api_key, :username, :password, :auth_token, :scheme, :host, :base_path, :user_agent, :logger, :inject_format, :force_ending_format, :camelize_params
|
6
|
+
|
7
|
+
# Defaults go in here..
|
8
|
+
def initialize
|
9
|
+
@format = 'json'
|
10
|
+
@scheme = 'https'
|
11
|
+
@host = 'api.taxamo.com'
|
12
|
+
@base_path = ''
|
13
|
+
@user_agent = "ruby-swagger"
|
14
|
+
@inject_format = true
|
15
|
+
@force_ending_format = false
|
16
|
+
@camelize_params = false
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,198 @@
|
|
1
|
+
module Swagger
|
2
|
+
|
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
|
9
|
+
|
10
|
+
|
11
|
+
# All requests must have an HTTP method and a path
|
12
|
+
# Optionals parameters are :params, :headers, :body, :format, :host
|
13
|
+
#
|
14
|
+
def initialize(http_method, path, attributes={})
|
15
|
+
attributes[:format] ||= Swagger.configuration.format
|
16
|
+
attributes[:params] ||= {}
|
17
|
+
|
18
|
+
# Set default headers
|
19
|
+
default_headers = {
|
20
|
+
'Content-Type' => "application/#{attributes[:format].downcase}",
|
21
|
+
'Token' => Swagger.configuration.api_key
|
22
|
+
}
|
23
|
+
|
24
|
+
# api_key from headers hash trumps the default, even if its value is blank
|
25
|
+
if attributes[:headers].present? && attributes[:headers].has_key?(:api_key)
|
26
|
+
default_headers.delete(:api_key)
|
27
|
+
end
|
28
|
+
|
29
|
+
# api_key from params hash trumps all others (headers and default_headers)
|
30
|
+
if attributes[:params].present? && attributes[:params].has_key?(:api_key)
|
31
|
+
default_headers.delete(:api_key)
|
32
|
+
attributes[:headers].delete(:api_key) if attributes[:headers].present?
|
33
|
+
end
|
34
|
+
|
35
|
+
# Merge argument headers into defaults
|
36
|
+
attributes[:headers] = default_headers.merge(attributes[:headers] || {})
|
37
|
+
|
38
|
+
# Stick in the auth token if there is one
|
39
|
+
if Swagger.authenticated?
|
40
|
+
attributes[:headers].merge!({:auth_token => Swagger.configuration.auth_token})
|
41
|
+
end
|
42
|
+
|
43
|
+
self.http_method = http_method.to_sym
|
44
|
+
self.path = path
|
45
|
+
attributes.each do |name, value|
|
46
|
+
send("#{name.to_s.underscore.to_sym}=", value)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Construct a base URL
|
51
|
+
#
|
52
|
+
def url(options = {})
|
53
|
+
u = Addressable::URI.new(
|
54
|
+
:scheme => Swagger.configuration.scheme,
|
55
|
+
:host => Swagger.configuration.host,
|
56
|
+
:path => self.interpreted_path,
|
57
|
+
:query => self.query_string.sub(/\?/, '')
|
58
|
+
).to_s
|
59
|
+
|
60
|
+
# Drop trailing question mark, if present
|
61
|
+
u.sub! /\?$/, ''
|
62
|
+
|
63
|
+
# Obfuscate API key?
|
64
|
+
u.sub! /api\_key=\w+/, 'api_key=YOUR_API_KEY' if options[:obfuscated]
|
65
|
+
|
66
|
+
u
|
67
|
+
end
|
68
|
+
|
69
|
+
# Iterate over the params hash, injecting any path values into the path string
|
70
|
+
#
|
71
|
+
# e.g. /word.{format}/{word}/entries => /word.json/cat/entries
|
72
|
+
def interpreted_path
|
73
|
+
p = self.path.dup
|
74
|
+
|
75
|
+
# Fill in the path params
|
76
|
+
self.params.each_pair do |key, value|
|
77
|
+
p = p.gsub("{#{key}}", value.to_s)
|
78
|
+
end
|
79
|
+
|
80
|
+
# # Stick a .{format} placeholder into the path if there isn't
|
81
|
+
# # one already or an actual format like json or xml
|
82
|
+
# # e.g. /words/blah => /words.{format}/blah
|
83
|
+
# if Swagger.configuration.inject_format
|
84
|
+
# unless ['.json', '.xml', '{format}'].any? {|s| p.downcase.include? s }
|
85
|
+
# p = p.sub(/^(\/?\w+)/, "\\1.#{format}")
|
86
|
+
# end
|
87
|
+
# end
|
88
|
+
#
|
89
|
+
# # Stick a .{format} placeholder on the end of the path if there isn't
|
90
|
+
# # one already or an actual format like json or xml
|
91
|
+
# # e.g. /words/blah => /words/blah.{format}
|
92
|
+
# if Swagger.configuration.force_ending_format
|
93
|
+
# unless ['.json', '.xml', '{format}'].any? {|s| p.downcase.include? s }
|
94
|
+
# p = "#{p}.#{format}"
|
95
|
+
# end
|
96
|
+
# end
|
97
|
+
#
|
98
|
+
# p = p.sub("{format}", self.format.to_s)
|
99
|
+
#
|
100
|
+
URI.encode [Swagger.configuration.base_path, p].join("/").gsub(/\/+/, '/')
|
101
|
+
end
|
102
|
+
|
103
|
+
# Massage the request body into a state of readiness
|
104
|
+
# If body is a hash, camelize all keys then convert to a json string
|
105
|
+
#
|
106
|
+
def body=(value)
|
107
|
+
if value.is_a?(Hash)
|
108
|
+
value = value.inject({}) do |memo, (k,v)|
|
109
|
+
memo[k.to_s.camelize(:lower).to_sym] = v
|
110
|
+
memo
|
111
|
+
end
|
112
|
+
end
|
113
|
+
@body = value
|
114
|
+
end
|
115
|
+
|
116
|
+
# If body is an object, JSONify it before making the actual request.
|
117
|
+
#
|
118
|
+
def outgoing_body
|
119
|
+
body.is_a?(String) ? body : body.to_json
|
120
|
+
end
|
121
|
+
|
122
|
+
# Construct a query string from the query-string-type params
|
123
|
+
def query_string
|
124
|
+
|
125
|
+
# Iterate over all params,
|
126
|
+
# .. removing the ones that are part of the path itself.
|
127
|
+
# .. stringifying values so Addressable doesn't blow up.
|
128
|
+
query_values = {}
|
129
|
+
self.params.each_pair do |key, value|
|
130
|
+
next if self.path.include? "{#{key}}" # skip path params
|
131
|
+
next if value.blank? && value.class != FalseClass # skip empties
|
132
|
+
if Swagger.configuration.camelize_params
|
133
|
+
key = key.to_s.camelize(:lower).to_sym unless key.to_sym == :api_key # api_key is not a camelCased param
|
134
|
+
end
|
135
|
+
query_values[key.to_s] = value.to_s
|
136
|
+
end
|
137
|
+
|
138
|
+
# We don't want to end up with '?' as our query string
|
139
|
+
# if there aren't really any params
|
140
|
+
return "" if query_values.blank?
|
141
|
+
|
142
|
+
# Addressable requires query_values to be set after initialization..
|
143
|
+
qs = Addressable::URI.new
|
144
|
+
qs.query_values = query_values
|
145
|
+
qs.to_s
|
146
|
+
end
|
147
|
+
|
148
|
+
def make
|
149
|
+
logger = Logger.new STDOUT
|
150
|
+
logger.debug self.url
|
151
|
+
response = case self.http_method.to_sym
|
152
|
+
when :get,:GET
|
153
|
+
Typhoeus::Request.get(
|
154
|
+
self.url,
|
155
|
+
:headers => self.headers.stringify_keys,
|
156
|
+
)
|
157
|
+
|
158
|
+
when :post,:POST
|
159
|
+
Typhoeus::Request.post(
|
160
|
+
self.url,
|
161
|
+
:body => self.outgoing_body,
|
162
|
+
:headers => self.headers.stringify_keys,
|
163
|
+
)
|
164
|
+
|
165
|
+
when :put,:PUT
|
166
|
+
Typhoeus::Request.put(
|
167
|
+
self.url,
|
168
|
+
:body => self.outgoing_body,
|
169
|
+
:headers => self.headers.stringify_keys,
|
170
|
+
)
|
171
|
+
|
172
|
+
when :delete,:DELETE
|
173
|
+
Typhoeus::Request.delete(
|
174
|
+
self.url,
|
175
|
+
:body => self.outgoing_body,
|
176
|
+
:headers => self.headers.stringify_keys,
|
177
|
+
)
|
178
|
+
end
|
179
|
+
Response.new(response)
|
180
|
+
end
|
181
|
+
|
182
|
+
def response
|
183
|
+
self.make
|
184
|
+
end
|
185
|
+
|
186
|
+
def response_code_pretty
|
187
|
+
return unless @response.present?
|
188
|
+
@response.code.to_s
|
189
|
+
end
|
190
|
+
|
191
|
+
def response_headers_pretty
|
192
|
+
return unless @response.present?
|
193
|
+
# JSON.pretty_generate(@response.headers).gsub(/\n/, '<br/>') # <- This was for RestClient
|
194
|
+
@response.headers.gsub(/\n/, '<br/>') # <- This is for Typhoeus
|
195
|
+
end
|
196
|
+
|
197
|
+
end
|
198
|
+
end
|