dear-inventory-ruby 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +9 -0
- data/LICENSE +21 -0
- data/README.md +128 -0
- data/Rakefile +10 -0
- data/dear-inventory-ruby.gemspec +39 -0
- data/docs/Address.md +33 -0
- data/docs/Contact.md +33 -0
- data/docs/CurrencyCode.md +16 -0
- data/docs/Customer.md +77 -0
- data/docs/CustomerApi.md +78 -0
- data/docs/Customers.md +21 -0
- data/docs/Error.md +19 -0
- data/git_push.sh +58 -0
- data/lib/dear-inventory-ruby.rb +46 -0
- data/lib/dear-inventory-ruby/api/customer_api.rb +94 -0
- data/lib/dear-inventory-ruby/api_client.rb +402 -0
- data/lib/dear-inventory-ruby/api_error.rb +57 -0
- data/lib/dear-inventory-ruby/configuration.rb +250 -0
- data/lib/dear-inventory-ruby/models/address.rb +398 -0
- data/lib/dear-inventory-ruby/models/contact.rb +381 -0
- data/lib/dear-inventory-ruby/models/currency_code.rb +196 -0
- data/lib/dear-inventory-ruby/models/customer.rb +574 -0
- data/lib/dear-inventory-ruby/models/customers.rb +229 -0
- data/lib/dear-inventory-ruby/models/error.rb +217 -0
- data/lib/dear-inventory-ruby/version.rb +15 -0
- data/spec/api/customer_api_spec.rb +51 -0
- data/spec/api_client_spec.rb +188 -0
- data/spec/configuration_spec.rb +42 -0
- data/spec/models/address_spec.rb +93 -0
- data/spec/models/contact_spec.rb +89 -0
- data/spec/models/currency_code_spec.rb +35 -0
- data/spec/models/customer_spec.rb +225 -0
- data/spec/models/customers_spec.rb +53 -0
- data/spec/models/error_spec.rb +47 -0
- data/spec/spec_helper.rb +111 -0
- metadata +142 -0
data/docs/Customers.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# DearInventoryRuby::Customers
|
2
|
+
|
3
|
+
## Properties
|
4
|
+
|
5
|
+
Name | Type | Description | Notes
|
6
|
+
------------ | ------------- | ------------- | -------------
|
7
|
+
**total** | **String** | Total | [optional]
|
8
|
+
**page** | **String** | Page | [optional]
|
9
|
+
**customer_list** | [**Array<Customer>**](Customer.md) | Array of Customers | [optional]
|
10
|
+
|
11
|
+
## Code Sample
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
require 'DearInventoryRuby'
|
15
|
+
|
16
|
+
instance = DearInventoryRuby::Customers.new(total: null,
|
17
|
+
page: null,
|
18
|
+
customer_list: null)
|
19
|
+
```
|
20
|
+
|
21
|
+
|
data/docs/Error.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# DearInventoryRuby::Error
|
2
|
+
|
3
|
+
## Properties
|
4
|
+
|
5
|
+
Name | Type | Description | Notes
|
6
|
+
------------ | ------------- | ------------- | -------------
|
7
|
+
**error_code** | **Integer** | Exception number | [optional]
|
8
|
+
**exception** | **String** | Exception message | [optional]
|
9
|
+
|
10
|
+
## Code Sample
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
require 'DearInventoryRuby'
|
14
|
+
|
15
|
+
instance = DearInventoryRuby::Error.new(error_code: null,
|
16
|
+
exception: null)
|
17
|
+
```
|
18
|
+
|
19
|
+
|
data/git_push.sh
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
#!/bin/sh
|
2
|
+
# ref: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/
|
3
|
+
#
|
4
|
+
# Usage example: /bin/sh ./git_push.sh wing328 openapi-pestore-perl "minor update" "gitlab.com"
|
5
|
+
|
6
|
+
git_user_id=$1
|
7
|
+
git_repo_id=$2
|
8
|
+
release_note=$3
|
9
|
+
git_host=$4
|
10
|
+
|
11
|
+
if [ "$git_host" = "" ]; then
|
12
|
+
git_host="github.com"
|
13
|
+
echo "[INFO] No command line input provided. Set \$git_host to $git_host"
|
14
|
+
fi
|
15
|
+
|
16
|
+
if [ "$git_user_id" = "" ]; then
|
17
|
+
git_user_id="GIT_USER_ID"
|
18
|
+
echo "[INFO] No command line input provided. Set \$git_user_id to $git_user_id"
|
19
|
+
fi
|
20
|
+
|
21
|
+
if [ "$git_repo_id" = "" ]; then
|
22
|
+
git_repo_id="GIT_REPO_ID"
|
23
|
+
echo "[INFO] No command line input provided. Set \$git_repo_id to $git_repo_id"
|
24
|
+
fi
|
25
|
+
|
26
|
+
if [ "$release_note" = "" ]; then
|
27
|
+
release_note="Minor update"
|
28
|
+
echo "[INFO] No command line input provided. Set \$release_note to $release_note"
|
29
|
+
fi
|
30
|
+
|
31
|
+
# Initialize the local directory as a Git repository
|
32
|
+
git init
|
33
|
+
|
34
|
+
# Adds the files in the local repository and stages them for commit.
|
35
|
+
git add .
|
36
|
+
|
37
|
+
# Commits the tracked changes and prepares them to be pushed to a remote repository.
|
38
|
+
git commit -m "$release_note"
|
39
|
+
|
40
|
+
# Sets the new remote
|
41
|
+
git_remote=`git remote`
|
42
|
+
if [ "$git_remote" = "" ]; then # git remote not defined
|
43
|
+
|
44
|
+
if [ "$GIT_TOKEN" = "" ]; then
|
45
|
+
echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git credential in your environment."
|
46
|
+
git remote add origin https://${git_host}/${git_user_id}/${git_repo_id}.git
|
47
|
+
else
|
48
|
+
git remote add origin https://${git_user_id}:${GIT_TOKEN}@${git_host}/${git_user_id}/${git_repo_id}.git
|
49
|
+
fi
|
50
|
+
|
51
|
+
fi
|
52
|
+
|
53
|
+
git pull origin master
|
54
|
+
|
55
|
+
# Pushes (Forces) the changes in the local repository up to the remote repository
|
56
|
+
echo "Git pushing to https://${git_host}/${git_user_id}/${git_repo_id}.git"
|
57
|
+
git push origin master 2>&1 | grep -v 'To https'
|
58
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
=begin
|
2
|
+
#DEAR Inventory API
|
3
|
+
|
4
|
+
#This specifing endpoints for DEAR Inventory API
|
5
|
+
|
6
|
+
The version of the OpenAPI document: 2.0.0
|
7
|
+
Contact: nnhansg@gmail.com
|
8
|
+
Generated by: https://openapi-generator.tech
|
9
|
+
OpenAPI Generator version: 4.2.3
|
10
|
+
|
11
|
+
=end
|
12
|
+
|
13
|
+
# Common files
|
14
|
+
require 'dear-inventory-ruby/api_client'
|
15
|
+
require 'dear-inventory-ruby/api_error'
|
16
|
+
require 'dear-inventory-ruby/version'
|
17
|
+
require 'dear-inventory-ruby/configuration'
|
18
|
+
|
19
|
+
# Models
|
20
|
+
require 'dear-inventory-ruby/models/address'
|
21
|
+
require 'dear-inventory-ruby/models/contact'
|
22
|
+
require 'dear-inventory-ruby/models/currency_code'
|
23
|
+
require 'dear-inventory-ruby/models/customer'
|
24
|
+
require 'dear-inventory-ruby/models/customers'
|
25
|
+
require 'dear-inventory-ruby/models/error'
|
26
|
+
|
27
|
+
# APIs
|
28
|
+
require 'dear-inventory-ruby/api/customer_api'
|
29
|
+
|
30
|
+
module DearInventoryRuby
|
31
|
+
class << self
|
32
|
+
# Customize default settings for the SDK using block.
|
33
|
+
# DearInventoryRuby.configure do |config|
|
34
|
+
# config.username = "xxx"
|
35
|
+
# config.password = "xxx"
|
36
|
+
# end
|
37
|
+
# If no block given, return the default Configuration object.
|
38
|
+
def configure
|
39
|
+
if block_given?
|
40
|
+
yield(Configuration.default)
|
41
|
+
else
|
42
|
+
Configuration.default
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
=begin
|
2
|
+
#DEAR Inventory API
|
3
|
+
|
4
|
+
#This specifing endpoints for DEAR Inventory API
|
5
|
+
|
6
|
+
The version of the OpenAPI document: 2.0.0
|
7
|
+
Contact: nnhansg@gmail.com
|
8
|
+
Generated by: https://openapi-generator.tech
|
9
|
+
OpenAPI Generator version: 4.2.3
|
10
|
+
|
11
|
+
=end
|
12
|
+
|
13
|
+
require 'cgi'
|
14
|
+
|
15
|
+
module DearInventoryRuby
|
16
|
+
class CustomerApi
|
17
|
+
attr_accessor :api_client
|
18
|
+
|
19
|
+
def initialize(api_client = ApiClient.default)
|
20
|
+
@api_client = api_client
|
21
|
+
end
|
22
|
+
# Allows you to retrieve the customers
|
23
|
+
# @param [Hash] opts the optional parameters
|
24
|
+
# @option opts [String] :page Default is 1 (default to '1')
|
25
|
+
# @option opts [String] :limit Default is 100 (default to '100')
|
26
|
+
# @option opts [String] :id Default is null
|
27
|
+
# @option opts [String] :name Default is null
|
28
|
+
# @option opts [String] :modified_since Default is null
|
29
|
+
# @option opts [String] :include_deprecated Default is false (default to 'false')
|
30
|
+
# @return [Customers]
|
31
|
+
def get_customers(opts = {})
|
32
|
+
data, _status_code, _headers = get_customers_with_http_info(opts)
|
33
|
+
data
|
34
|
+
end
|
35
|
+
|
36
|
+
# Allows you to retrieve the customers
|
37
|
+
# @param [Hash] opts the optional parameters
|
38
|
+
# @option opts [String] :page Default is 1
|
39
|
+
# @option opts [String] :limit Default is 100
|
40
|
+
# @option opts [String] :id Default is null
|
41
|
+
# @option opts [String] :name Default is null
|
42
|
+
# @option opts [String] :modified_since Default is null
|
43
|
+
# @option opts [String] :include_deprecated Default is false
|
44
|
+
# @return [Array<(Customers, Integer, Hash)>] Customers data, response status code and response headers
|
45
|
+
def get_customers_with_http_info(opts = {})
|
46
|
+
if @api_client.config.debugging
|
47
|
+
@api_client.config.logger.debug 'Calling API: CustomerApi.get_customers ...'
|
48
|
+
end
|
49
|
+
# resource path
|
50
|
+
local_var_path = '/customer'
|
51
|
+
|
52
|
+
# query parameters
|
53
|
+
query_params = opts[:query_params] || {}
|
54
|
+
query_params[:'Page'] = opts[:'page'] if !opts[:'page'].nil?
|
55
|
+
query_params[:'Limit'] = opts[:'limit'] if !opts[:'limit'].nil?
|
56
|
+
query_params[:'ID'] = opts[:'id'] if !opts[:'id'].nil?
|
57
|
+
query_params[:'Name'] = opts[:'name'] if !opts[:'name'].nil?
|
58
|
+
query_params[:'ModifiedSince'] = opts[:'modified_since'] if !opts[:'modified_since'].nil?
|
59
|
+
query_params[:'IncludeDeprecated'] = opts[:'include_deprecated'] if !opts[:'include_deprecated'].nil?
|
60
|
+
|
61
|
+
# header parameters
|
62
|
+
header_params = opts[:header_params] || {}
|
63
|
+
# HTTP header 'Accept' (if needed)
|
64
|
+
header_params['Accept'] = @api_client.select_header_accept(['application/json'])
|
65
|
+
|
66
|
+
# form parameters
|
67
|
+
form_params = opts[:form_params] || {}
|
68
|
+
|
69
|
+
# http body (model)
|
70
|
+
post_body = opts[:body]
|
71
|
+
|
72
|
+
# return_type
|
73
|
+
return_type = opts[:return_type] || 'Customers'
|
74
|
+
|
75
|
+
# auth_names
|
76
|
+
auth_names = opts[:auth_names] || ['accountID', 'appKey']
|
77
|
+
|
78
|
+
new_options = opts.merge(
|
79
|
+
:header_params => header_params,
|
80
|
+
:query_params => query_params,
|
81
|
+
:form_params => form_params,
|
82
|
+
:body => post_body,
|
83
|
+
:auth_names => auth_names,
|
84
|
+
:return_type => return_type
|
85
|
+
)
|
86
|
+
|
87
|
+
data, status_code, headers = @api_client.call_api(:GET, local_var_path, new_options)
|
88
|
+
if @api_client.config.debugging
|
89
|
+
@api_client.config.logger.debug "API called: CustomerApi#get_customers\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
|
90
|
+
end
|
91
|
+
return data, status_code, headers
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,402 @@
|
|
1
|
+
=begin
|
2
|
+
#DEAR Inventory API
|
3
|
+
|
4
|
+
#This specifing endpoints for DEAR Inventory API
|
5
|
+
|
6
|
+
The version of the OpenAPI document: 2.0.0
|
7
|
+
Contact: nnhansg@gmail.com
|
8
|
+
Generated by: https://openapi-generator.tech
|
9
|
+
OpenAPI Generator version: 4.2.3
|
10
|
+
|
11
|
+
=end
|
12
|
+
|
13
|
+
require 'date'
|
14
|
+
require 'json'
|
15
|
+
require 'logger'
|
16
|
+
require 'tempfile'
|
17
|
+
require 'faraday'
|
18
|
+
|
19
|
+
module DearInventoryRuby
|
20
|
+
class ApiClient
|
21
|
+
# The Configuration object holding settings to be used in the API client.
|
22
|
+
attr_accessor :config
|
23
|
+
|
24
|
+
# Defines the headers to be used in HTTP requests of all API calls by default.
|
25
|
+
#
|
26
|
+
# @return [Hash]
|
27
|
+
attr_accessor :default_headers
|
28
|
+
|
29
|
+
# Initializes the ApiClient
|
30
|
+
# @option config [Configuration] Configuration for initializing the object, default to Configuration.default
|
31
|
+
def initialize(config = Configuration.default)
|
32
|
+
@config = config
|
33
|
+
@user_agent = "dear-inventory-ruby-#{VERSION}"
|
34
|
+
@default_headers = {
|
35
|
+
'Content-Type' => 'application/json',
|
36
|
+
'User-Agent' => @user_agent
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.default
|
41
|
+
@@default ||= ApiClient.new
|
42
|
+
end
|
43
|
+
|
44
|
+
# Call an API with given options.
|
45
|
+
#
|
46
|
+
# @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
|
47
|
+
# the data deserialized from response body (could be nil), response status code and response headers.
|
48
|
+
def call_api(http_method, path, opts = {})
|
49
|
+
ssl_options = {
|
50
|
+
:ca_file => @config.ssl_ca_file,
|
51
|
+
:verify => @config.ssl_verify,
|
52
|
+
:verify_mode => @config.ssl_verify_mode,
|
53
|
+
:client_cert => @config.ssl_client_cert,
|
54
|
+
:client_key => @config.ssl_client_key
|
55
|
+
}
|
56
|
+
|
57
|
+
connection = Faraday.new(:url => config.base_url, :ssl => ssl_options) do |conn|
|
58
|
+
conn.basic_auth(config.username, config.password)
|
59
|
+
if opts[:header_params]["Content-Type"] == "multipart/form-data"
|
60
|
+
conn.request :multipart
|
61
|
+
conn.request :url_encoded
|
62
|
+
end
|
63
|
+
conn.adapter(Faraday.default_adapter)
|
64
|
+
end
|
65
|
+
|
66
|
+
begin
|
67
|
+
response = connection.public_send(http_method.to_sym.downcase) do |req|
|
68
|
+
build_request(http_method, path, req, opts)
|
69
|
+
end
|
70
|
+
|
71
|
+
if @config.debugging
|
72
|
+
@config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
|
73
|
+
end
|
74
|
+
|
75
|
+
unless response.success?
|
76
|
+
if response.status == 0
|
77
|
+
# Errors from libcurl will be made visible here
|
78
|
+
fail ApiError.new(:code => 0,
|
79
|
+
:message => response.return_message)
|
80
|
+
else
|
81
|
+
fail ApiError.new(:code => response.status,
|
82
|
+
:response_headers => response.headers,
|
83
|
+
:response_body => response.body),
|
84
|
+
response.reason_phrase
|
85
|
+
end
|
86
|
+
end
|
87
|
+
rescue Faraday::TimeoutError
|
88
|
+
fail ApiError.new('Connection timed out')
|
89
|
+
end
|
90
|
+
|
91
|
+
if opts[:return_type]
|
92
|
+
data = deserialize(response, opts[:return_type])
|
93
|
+
else
|
94
|
+
data = nil
|
95
|
+
end
|
96
|
+
return data, response.status, response.headers
|
97
|
+
end
|
98
|
+
|
99
|
+
# Builds the HTTP request
|
100
|
+
#
|
101
|
+
# @param [String] http_method HTTP method/verb (e.g. POST)
|
102
|
+
# @param [String] path URL path (e.g. /account/new)
|
103
|
+
# @option opts [Hash] :header_params Header parameters
|
104
|
+
# @option opts [Hash] :query_params Query parameters
|
105
|
+
# @option opts [Hash] :form_params Query parameters
|
106
|
+
# @option opts [Object] :body HTTP body (JSON/XML)
|
107
|
+
# @return [Typhoeus::Request] A Typhoeus Request
|
108
|
+
def build_request(http_method, path, request, opts = {})
|
109
|
+
url = build_request_url(path)
|
110
|
+
http_method = http_method.to_sym.downcase
|
111
|
+
|
112
|
+
header_params = @default_headers.merge(opts[:header_params] || {})
|
113
|
+
query_params = opts[:query_params] || {}
|
114
|
+
form_params = opts[:form_params] || {}
|
115
|
+
|
116
|
+
update_params_for_auth! header_params, query_params, opts[:auth_names]
|
117
|
+
|
118
|
+
req_opts = {
|
119
|
+
:method => http_method,
|
120
|
+
:headers => header_params,
|
121
|
+
:params => query_params,
|
122
|
+
:params_encoding => @config.params_encoding,
|
123
|
+
:timeout => @config.timeout,
|
124
|
+
:verbose => @config.debugging
|
125
|
+
}
|
126
|
+
|
127
|
+
if [:post, :patch, :put, :delete].include?(http_method)
|
128
|
+
req_body = build_request_body(header_params, form_params, opts[:body])
|
129
|
+
req_opts.update :body => req_body
|
130
|
+
if @config.debugging
|
131
|
+
@config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
|
132
|
+
end
|
133
|
+
end
|
134
|
+
request.headers = header_params
|
135
|
+
request.body = req_body
|
136
|
+
request.url url
|
137
|
+
request.params = query_params
|
138
|
+
download_file(request) if opts[:return_type] == 'File'
|
139
|
+
request
|
140
|
+
end
|
141
|
+
|
142
|
+
# Builds the HTTP request body
|
143
|
+
#
|
144
|
+
# @param [Hash] header_params Header parameters
|
145
|
+
# @param [Hash] form_params Query parameters
|
146
|
+
# @param [Object] body HTTP body (JSON/XML)
|
147
|
+
# @return [String] HTTP body data in the form of string
|
148
|
+
def build_request_body(header_params, form_params, body)
|
149
|
+
# http form
|
150
|
+
if header_params['Content-Type'] == 'application/x-www-form-urlencoded'
|
151
|
+
data = URI.encode_www_form(form_params)
|
152
|
+
elsif header_params['Content-Type'] == 'multipart/form-data'
|
153
|
+
data = {}
|
154
|
+
form_params.each do |key, value|
|
155
|
+
case value
|
156
|
+
when ::File, ::Tempfile
|
157
|
+
# TODO hardcode to application/octet-stream, need better way to detect content type
|
158
|
+
data[key] = Faraday::UploadIO.new(value.path, 'application/octet-stream', value.path)
|
159
|
+
when ::Array, nil
|
160
|
+
# let Faraday handle Array and nil parameters
|
161
|
+
data[key] = value
|
162
|
+
else
|
163
|
+
data[key] = value.to_s
|
164
|
+
end
|
165
|
+
end
|
166
|
+
elsif body
|
167
|
+
data = body.is_a?(String) ? body : body.to_json
|
168
|
+
else
|
169
|
+
data = nil
|
170
|
+
end
|
171
|
+
data
|
172
|
+
end
|
173
|
+
|
174
|
+
# Check if the given MIME is a JSON MIME.
|
175
|
+
# JSON MIME examples:
|
176
|
+
# application/json
|
177
|
+
# application/json; charset=UTF8
|
178
|
+
# APPLICATION/JSON
|
179
|
+
# */*
|
180
|
+
# @param [String] mime MIME
|
181
|
+
# @return [Boolean] True if the MIME is application/json
|
182
|
+
def json_mime?(mime)
|
183
|
+
(mime == '*/*') || !(mime =~ /Application\/.*json(?!p)(;.*)?/i).nil?
|
184
|
+
end
|
185
|
+
|
186
|
+
# Deserialize the response to the given return type.
|
187
|
+
#
|
188
|
+
# @param [Response] response HTTP response
|
189
|
+
# @param [String] return_type some examples: "User", "Array<User>", "Hash<String, Integer>"
|
190
|
+
def deserialize(response, return_type)
|
191
|
+
body = response.body
|
192
|
+
|
193
|
+
# handle file downloading - return the File instance processed in request callbacks
|
194
|
+
# note that response body is empty when the file is written in chunks in request on_body callback
|
195
|
+
return @tempfile if return_type == 'File'
|
196
|
+
|
197
|
+
return nil if body.nil? || body.empty?
|
198
|
+
|
199
|
+
# return response body directly for String return type
|
200
|
+
return body if return_type == 'String'
|
201
|
+
|
202
|
+
# ensuring a default content type
|
203
|
+
content_type = response.headers['Content-Type'] || 'application/json'
|
204
|
+
|
205
|
+
fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
|
206
|
+
|
207
|
+
begin
|
208
|
+
data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
|
209
|
+
rescue JSON::ParserError => e
|
210
|
+
if %w(String Date DateTime).include?(return_type)
|
211
|
+
data = body
|
212
|
+
else
|
213
|
+
raise e
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
convert_to_type data, return_type
|
218
|
+
end
|
219
|
+
|
220
|
+
# Convert data to the given return type.
|
221
|
+
# @param [Object] data Data to be converted
|
222
|
+
# @param [String] return_type Return type
|
223
|
+
# @return [Mixed] Data in a particular type
|
224
|
+
def convert_to_type(data, return_type)
|
225
|
+
return nil if data.nil?
|
226
|
+
case return_type
|
227
|
+
when 'String'
|
228
|
+
data.to_s
|
229
|
+
when 'Integer'
|
230
|
+
data.to_i
|
231
|
+
when 'Float'
|
232
|
+
data.to_f
|
233
|
+
when 'Boolean'
|
234
|
+
data == true
|
235
|
+
when 'DateTime'
|
236
|
+
# parse date time (expecting ISO 8601 format)
|
237
|
+
DateTime.parse data
|
238
|
+
when 'Date'
|
239
|
+
# parse date time (expecting ISO 8601 format)
|
240
|
+
Date.parse data
|
241
|
+
when 'Object'
|
242
|
+
# generic object (usually a Hash), return directly
|
243
|
+
data
|
244
|
+
when /\AArray<(.+)>\z/
|
245
|
+
# e.g. Array<Pet>
|
246
|
+
sub_type = $1
|
247
|
+
data.map { |item| convert_to_type(item, sub_type) }
|
248
|
+
when /\AHash\<String, (.+)\>\z/
|
249
|
+
# e.g. Hash<String, Integer>
|
250
|
+
sub_type = $1
|
251
|
+
{}.tap do |hash|
|
252
|
+
data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
|
253
|
+
end
|
254
|
+
else
|
255
|
+
# models, e.g. Pet
|
256
|
+
DearInventoryRuby.const_get(return_type).build_from_hash(data)
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
# Save response body into a file in (the defined) temporary folder, using the filename
|
261
|
+
# from the "Content-Disposition" header if provided, otherwise a random filename.
|
262
|
+
# The response body is written to the file in chunks in order to handle files which
|
263
|
+
# size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
|
264
|
+
# process can use.
|
265
|
+
#
|
266
|
+
# @see Configuration#temp_folder_path
|
267
|
+
def download_file(request)
|
268
|
+
tempfile = nil
|
269
|
+
encoding = nil
|
270
|
+
request.on_headers do |response|
|
271
|
+
content_disposition = response.headers['Content-Disposition']
|
272
|
+
if content_disposition && content_disposition =~ /filename=/i
|
273
|
+
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
|
274
|
+
prefix = sanitize_filename(filename)
|
275
|
+
else
|
276
|
+
prefix = 'download-'
|
277
|
+
end
|
278
|
+
prefix = prefix + '-' unless prefix.end_with?('-')
|
279
|
+
encoding = response.body.encoding
|
280
|
+
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
|
281
|
+
@tempfile = tempfile
|
282
|
+
end
|
283
|
+
request.on_body do |chunk|
|
284
|
+
chunk.force_encoding(encoding)
|
285
|
+
tempfile.write(chunk)
|
286
|
+
end
|
287
|
+
request.on_complete do |response|
|
288
|
+
tempfile.close if tempfile
|
289
|
+
@config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
|
290
|
+
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
|
291
|
+
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
292
|
+
"explicitly with `tempfile.delete`"
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
296
|
+
# Sanitize filename by removing path.
|
297
|
+
# e.g. ../../sun.gif becomes sun.gif
|
298
|
+
#
|
299
|
+
# @param [String] filename the filename to be sanitized
|
300
|
+
# @return [String] the sanitized filename
|
301
|
+
def sanitize_filename(filename)
|
302
|
+
filename.gsub(/.*[\/\\]/, '')
|
303
|
+
end
|
304
|
+
|
305
|
+
def build_request_url(path)
|
306
|
+
# Add leading and trailing slashes to path
|
307
|
+
path = "/#{path}".gsub(/\/+/, '/')
|
308
|
+
@config.base_url + path
|
309
|
+
end
|
310
|
+
|
311
|
+
# Update hearder and query params based on authentication settings.
|
312
|
+
#
|
313
|
+
# @param [Hash] header_params Header parameters
|
314
|
+
# @param [Hash] query_params Query parameters
|
315
|
+
# @param [String] auth_names Authentication scheme name
|
316
|
+
def update_params_for_auth!(header_params, query_params, auth_names)
|
317
|
+
Array(auth_names).each do |auth_name|
|
318
|
+
auth_setting = @config.auth_settings[auth_name]
|
319
|
+
next unless auth_setting
|
320
|
+
case auth_setting[:in]
|
321
|
+
when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
|
322
|
+
when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
|
323
|
+
else fail ArgumentError, 'Authentication token must be in `query` of `header`'
|
324
|
+
end
|
325
|
+
end
|
326
|
+
end
|
327
|
+
|
328
|
+
# Sets user agent in HTTP header
|
329
|
+
#
|
330
|
+
# @param [String] user_agent User agent (e.g. openapi-generator/ruby/1.0.0)
|
331
|
+
def user_agent=(user_agent)
|
332
|
+
@user_agent = user_agent
|
333
|
+
@default_headers['User-Agent'] = @user_agent
|
334
|
+
end
|
335
|
+
|
336
|
+
# Return Accept header based on an array of accepts provided.
|
337
|
+
# @param [Array] accepts array for Accept
|
338
|
+
# @return [String] the Accept header (e.g. application/json)
|
339
|
+
def select_header_accept(accepts)
|
340
|
+
return nil if accepts.nil? || accepts.empty?
|
341
|
+
# use JSON when present, otherwise use all of the provided
|
342
|
+
json_accept = accepts.find { |s| json_mime?(s) }
|
343
|
+
json_accept || accepts.join(',')
|
344
|
+
end
|
345
|
+
|
346
|
+
# Return Content-Type header based on an array of content types provided.
|
347
|
+
# @param [Array] content_types array for Content-Type
|
348
|
+
# @return [String] the Content-Type header (e.g. application/json)
|
349
|
+
def select_header_content_type(content_types)
|
350
|
+
# use application/json by default
|
351
|
+
return 'application/json' if content_types.nil? || content_types.empty?
|
352
|
+
# use JSON when present, otherwise use the first one
|
353
|
+
json_content_type = content_types.find { |s| json_mime?(s) }
|
354
|
+
json_content_type || content_types.first
|
355
|
+
end
|
356
|
+
|
357
|
+
# Convert object (array, hash, object, etc) to JSON string.
|
358
|
+
# @param [Object] model object to be converted into JSON string
|
359
|
+
# @return [String] JSON string representation of the object
|
360
|
+
def object_to_http_body(model)
|
361
|
+
return model if model.nil? || model.is_a?(String)
|
362
|
+
local_body = nil
|
363
|
+
if model.is_a?(Array)
|
364
|
+
local_body = model.map { |m| object_to_hash(m) }
|
365
|
+
else
|
366
|
+
local_body = object_to_hash(model)
|
367
|
+
end
|
368
|
+
local_body.to_json
|
369
|
+
end
|
370
|
+
|
371
|
+
# Convert object(non-array) to hash.
|
372
|
+
# @param [Object] obj object to be converted into JSON string
|
373
|
+
# @return [String] JSON string representation of the object
|
374
|
+
def object_to_hash(obj)
|
375
|
+
if obj.respond_to?(:to_hash)
|
376
|
+
obj.to_hash
|
377
|
+
else
|
378
|
+
obj
|
379
|
+
end
|
380
|
+
end
|
381
|
+
|
382
|
+
# Build parameter value according to the given collection format.
|
383
|
+
# @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
|
384
|
+
def build_collection_param(param, collection_format)
|
385
|
+
case collection_format
|
386
|
+
when :csv
|
387
|
+
param.join(',')
|
388
|
+
when :ssv
|
389
|
+
param.join(' ')
|
390
|
+
when :tsv
|
391
|
+
param.join("\t")
|
392
|
+
when :pipes
|
393
|
+
param.join('|')
|
394
|
+
when :multi
|
395
|
+
# return the array directly as typhoeus will handle it as expected
|
396
|
+
param
|
397
|
+
else
|
398
|
+
fail "unknown collection format: #{collection_format.inspect}"
|
399
|
+
end
|
400
|
+
end
|
401
|
+
end
|
402
|
+
end
|