wheniwork-ruby 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +19 -0
- data/Gemfile.lock +106 -0
- data/LICENSE +201 -0
- data/README.md +111 -0
- data/docs/DefaultApi.md +240 -0
- data/docs/InlineResponse200.md +8 -0
- data/docs/InlineResponse2001.md +10 -0
- data/docs/InlineResponse2002.md +8 -0
- data/docs/InlineResponse2003.md +8 -0
- data/docs/Position.md +11 -0
- data/docs/Shift.md +20 -0
- data/docs/User.md +17 -0
- data/git_push.sh +67 -0
- data/lib/wheniwork-ruby.rb +56 -0
- data/lib/wheniwork-ruby/api/api.rb +279 -0
- data/lib/wheniwork-ruby/api_client.rb +371 -0
- data/lib/wheniwork-ruby/api_error.rb +45 -0
- data/lib/wheniwork-ruby/configuration.rb +191 -0
- data/lib/wheniwork-ruby/models/inline_response_200.rb +195 -0
- data/lib/wheniwork-ruby/models/inline_response_200_1.rb +215 -0
- data/lib/wheniwork-ruby/models/inline_response_200_2.rb +195 -0
- data/lib/wheniwork-ruby/models/inline_response_200_3.rb +195 -0
- data/lib/wheniwork-ruby/models/position.rb +222 -0
- data/lib/wheniwork-ruby/models/shift.rb +303 -0
- data/lib/wheniwork-ruby/models/user.rb +280 -0
- data/lib/wheniwork-ruby/version.rb +24 -0
- data/spec/api/api_spec.rb +99 -0
- data/spec/api_client_spec.rb +260 -0
- data/spec/configuration_spec.rb +46 -0
- data/spec/models/inline_response_200_1_spec.rb +63 -0
- data/spec/models/inline_response_200_2_spec.rb +51 -0
- data/spec/models/inline_response_200_3_spec.rb +51 -0
- data/spec/models/inline_response_200_spec.rb +51 -0
- data/spec/models/position_spec.rb +69 -0
- data/spec/models/shift_spec.rb +123 -0
- data/spec/models/user_spec.rb +105 -0
- data/spec/spec_helper.rb +120 -0
- data/wheniwork-ruby.gemspec +53 -0
- metadata +273 -0
@@ -0,0 +1,279 @@
|
|
1
|
+
=begin
|
2
|
+
Wheniwork
|
3
|
+
|
4
|
+
OpenAPI spec version: v2
|
5
|
+
|
6
|
+
Generated by: https://github.com/swagger-api/swagger-codegen.git
|
7
|
+
|
8
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
9
|
+
you may not use this file except in compliance with the License.
|
10
|
+
You may obtain a copy of the License at
|
11
|
+
|
12
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
13
|
+
|
14
|
+
Unless required by applicable law or agreed to in writing, software
|
15
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
16
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
17
|
+
See the License for the specific language governing permissions and
|
18
|
+
limitations under the License.
|
19
|
+
|
20
|
+
=end
|
21
|
+
|
22
|
+
require "uri"
|
23
|
+
|
24
|
+
module WhenIWork
|
25
|
+
class Api
|
26
|
+
attr_accessor :api_client
|
27
|
+
|
28
|
+
def initialize(api_client = ApiClient.default)
|
29
|
+
@api_client = api_client
|
30
|
+
end
|
31
|
+
|
32
|
+
#
|
33
|
+
# This method allows you to get a list of all positions in your account.
|
34
|
+
# @param [Hash] opts the optional parameters
|
35
|
+
# @option opts [BOOLEAN] :show_deleted Whether to show positions that have been deleted.
|
36
|
+
# @return [InlineResponse200]
|
37
|
+
def get_positions(opts = {})
|
38
|
+
data, _status_code, _headers = get_positions_with_http_info(opts)
|
39
|
+
return data
|
40
|
+
end
|
41
|
+
|
42
|
+
#
|
43
|
+
# This method allows you to get a list of all positions in your account.
|
44
|
+
# @param [Hash] opts the optional parameters
|
45
|
+
# @option opts [BOOLEAN] :show_deleted Whether to show positions that have been deleted.
|
46
|
+
# @return [Array<(InlineResponse200, Fixnum, Hash)>] InlineResponse200 data, response status code and response headers
|
47
|
+
def get_positions_with_http_info(opts = {})
|
48
|
+
if @api_client.config.debugging
|
49
|
+
@api_client.config.logger.debug "Calling API: Api.get_positions ..."
|
50
|
+
end
|
51
|
+
# resource path
|
52
|
+
local_var_path = "/positions".sub('{format}','json')
|
53
|
+
|
54
|
+
# query parameters
|
55
|
+
query_params = {}
|
56
|
+
query_params[:'show_deleted'] = opts[:'show_deleted'] if opts[:'show_deleted']
|
57
|
+
|
58
|
+
# header parameters
|
59
|
+
header_params = {}
|
60
|
+
|
61
|
+
# HTTP header 'Accept' (if needed)
|
62
|
+
local_header_accept = ['application/json']
|
63
|
+
local_header_accept_result = @api_client.select_header_accept(local_header_accept) and header_params['Accept'] = local_header_accept_result
|
64
|
+
|
65
|
+
# HTTP header 'Content-Type'
|
66
|
+
local_header_content_type = ['application/json']
|
67
|
+
header_params['Content-Type'] = @api_client.select_header_content_type(local_header_content_type)
|
68
|
+
|
69
|
+
# form parameters
|
70
|
+
form_params = {}
|
71
|
+
|
72
|
+
# http body (model)
|
73
|
+
post_body = nil
|
74
|
+
auth_names = ['apiKey']
|
75
|
+
data, status_code, headers = @api_client.call_api(:GET, local_var_path,
|
76
|
+
:header_params => header_params,
|
77
|
+
:query_params => query_params,
|
78
|
+
:form_params => form_params,
|
79
|
+
:body => post_body,
|
80
|
+
:auth_names => auth_names,
|
81
|
+
:return_type => 'InlineResponse200')
|
82
|
+
if @api_client.config.debugging
|
83
|
+
@api_client.config.logger.debug "API called: Api#get_positions\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
|
84
|
+
end
|
85
|
+
return data, status_code, headers
|
86
|
+
end
|
87
|
+
|
88
|
+
#
|
89
|
+
# Gets the details of an existing shift.
|
90
|
+
# @param shift_id max records to return
|
91
|
+
# @param [Hash] opts the optional parameters
|
92
|
+
# @return [InlineResponse2002]
|
93
|
+
def get_shift(shift_id, opts = {})
|
94
|
+
data, _status_code, _headers = get_shift_with_http_info(shift_id, opts)
|
95
|
+
return data
|
96
|
+
end
|
97
|
+
|
98
|
+
#
|
99
|
+
# Gets the details of an existing shift.
|
100
|
+
# @param shift_id max records to return
|
101
|
+
# @param [Hash] opts the optional parameters
|
102
|
+
# @return [Array<(InlineResponse2002, Fixnum, Hash)>] InlineResponse2002 data, response status code and response headers
|
103
|
+
def get_shift_with_http_info(shift_id, opts = {})
|
104
|
+
if @api_client.config.debugging
|
105
|
+
@api_client.config.logger.debug "Calling API: Api.get_shift ..."
|
106
|
+
end
|
107
|
+
# verify the required parameter 'shift_id' is set
|
108
|
+
fail ArgumentError, "Missing the required parameter 'shift_id' when calling Api.get_shift" if shift_id.nil?
|
109
|
+
# resource path
|
110
|
+
local_var_path = "/shifts/{shift-id}".sub('{format}','json').sub('{' + 'shift-id' + '}', shift_id.to_s)
|
111
|
+
|
112
|
+
# query parameters
|
113
|
+
query_params = {}
|
114
|
+
|
115
|
+
# header parameters
|
116
|
+
header_params = {}
|
117
|
+
|
118
|
+
# HTTP header 'Accept' (if needed)
|
119
|
+
local_header_accept = ['application/json']
|
120
|
+
local_header_accept_result = @api_client.select_header_accept(local_header_accept) and header_params['Accept'] = local_header_accept_result
|
121
|
+
|
122
|
+
# HTTP header 'Content-Type'
|
123
|
+
local_header_content_type = ['application/json']
|
124
|
+
header_params['Content-Type'] = @api_client.select_header_content_type(local_header_content_type)
|
125
|
+
|
126
|
+
# form parameters
|
127
|
+
form_params = {}
|
128
|
+
|
129
|
+
# http body (model)
|
130
|
+
post_body = nil
|
131
|
+
auth_names = ['apiKey']
|
132
|
+
data, status_code, headers = @api_client.call_api(:GET, local_var_path,
|
133
|
+
:header_params => header_params,
|
134
|
+
:query_params => query_params,
|
135
|
+
:form_params => form_params,
|
136
|
+
:body => post_body,
|
137
|
+
:auth_names => auth_names,
|
138
|
+
:return_type => 'InlineResponse2002')
|
139
|
+
if @api_client.config.debugging
|
140
|
+
@api_client.config.logger.debug "API called: Api#get_shift\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
|
141
|
+
end
|
142
|
+
return data, status_code, headers
|
143
|
+
end
|
144
|
+
|
145
|
+
#
|
146
|
+
# Get a list of shifts.
|
147
|
+
# @param [Hash] opts the optional parameters
|
148
|
+
# @option opts [String] :user_id The ID of the user to get shifts for. For multiple users, enter a list of user IDs separated by commas (e.g. 1,5,3).
|
149
|
+
# @option opts [DateTime] :start Start time for the search window. The default is the current date and time
|
150
|
+
# @option opts [DateTime] :_end End time for the search window. The default is exactly three days from the start time.
|
151
|
+
# @option opts [String] :location_id The ID of the location to get shifts for. For multiple locations, enter a list of location IDs separated by commas.
|
152
|
+
# @option opts [String] :position_id The ID of the position to get shifts for. For multiple position, enter a list of position IDs separated by commas.
|
153
|
+
# @option opts [BOOLEAN] :include_open Whether to include OpenShifts in the results.
|
154
|
+
# @option opts [BOOLEAN] :include_onlyopen Whether to include OpenShifts in the results.
|
155
|
+
# @option opts [BOOLEAN] :unpublished Whether unpublished shifts should be included in the results.
|
156
|
+
# @return [InlineResponse2001]
|
157
|
+
def get_shifts(opts = {})
|
158
|
+
data, _status_code, _headers = get_shifts_with_http_info(opts)
|
159
|
+
return data
|
160
|
+
end
|
161
|
+
|
162
|
+
#
|
163
|
+
# Get a list of shifts.
|
164
|
+
# @param [Hash] opts the optional parameters
|
165
|
+
# @option opts [String] :user_id The ID of the user to get shifts for. For multiple users, enter a list of user IDs separated by commas (e.g. 1,5,3).
|
166
|
+
# @option opts [DateTime] :start Start time for the search window. The default is the current date and time
|
167
|
+
# @option opts [DateTime] :_end End time for the search window. The default is exactly three days from the start time.
|
168
|
+
# @option opts [String] :location_id The ID of the location to get shifts for. For multiple locations, enter a list of location IDs separated by commas.
|
169
|
+
# @option opts [String] :position_id The ID of the position to get shifts for. For multiple position, enter a list of position IDs separated by commas.
|
170
|
+
# @option opts [BOOLEAN] :include_open Whether to include OpenShifts in the results.
|
171
|
+
# @option opts [BOOLEAN] :include_onlyopen Whether to include OpenShifts in the results.
|
172
|
+
# @option opts [BOOLEAN] :unpublished Whether unpublished shifts should be included in the results.
|
173
|
+
# @return [Array<(InlineResponse2001, Fixnum, Hash)>] InlineResponse2001 data, response status code and response headers
|
174
|
+
def get_shifts_with_http_info(opts = {})
|
175
|
+
if @api_client.config.debugging
|
176
|
+
@api_client.config.logger.debug "Calling API: Api.get_shifts ..."
|
177
|
+
end
|
178
|
+
# resource path
|
179
|
+
local_var_path = "/shifts".sub('{format}','json')
|
180
|
+
|
181
|
+
# query parameters
|
182
|
+
query_params = {}
|
183
|
+
query_params[:'user_id'] = opts[:'user_id'] if opts[:'user_id']
|
184
|
+
query_params[:'start'] = opts[:'start'] if opts[:'start']
|
185
|
+
query_params[:'end'] = opts[:'_end'] if opts[:'_end']
|
186
|
+
query_params[:'location_id'] = opts[:'location_id'] if opts[:'location_id']
|
187
|
+
query_params[:'position_id'] = opts[:'position_id'] if opts[:'position_id']
|
188
|
+
query_params[:'include_open'] = opts[:'include_open'] if opts[:'include_open']
|
189
|
+
query_params[:'include_onlyopen'] = opts[:'include_onlyopen'] if opts[:'include_onlyopen']
|
190
|
+
query_params[:'unpublished'] = opts[:'unpublished'] if opts[:'unpublished']
|
191
|
+
|
192
|
+
# header parameters
|
193
|
+
header_params = {}
|
194
|
+
|
195
|
+
# HTTP header 'Accept' (if needed)
|
196
|
+
local_header_accept = ['application/json']
|
197
|
+
local_header_accept_result = @api_client.select_header_accept(local_header_accept) and header_params['Accept'] = local_header_accept_result
|
198
|
+
|
199
|
+
# HTTP header 'Content-Type'
|
200
|
+
local_header_content_type = ['application/json']
|
201
|
+
header_params['Content-Type'] = @api_client.select_header_content_type(local_header_content_type)
|
202
|
+
|
203
|
+
# form parameters
|
204
|
+
form_params = {}
|
205
|
+
|
206
|
+
# http body (model)
|
207
|
+
post_body = nil
|
208
|
+
auth_names = ['apiKey']
|
209
|
+
data, status_code, headers = @api_client.call_api(:GET, local_var_path,
|
210
|
+
:header_params => header_params,
|
211
|
+
:query_params => query_params,
|
212
|
+
:form_params => form_params,
|
213
|
+
:body => post_body,
|
214
|
+
:auth_names => auth_names,
|
215
|
+
:return_type => 'InlineResponse2001')
|
216
|
+
if @api_client.config.debugging
|
217
|
+
@api_client.config.logger.debug "API called: Api#get_shifts\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
|
218
|
+
end
|
219
|
+
return data, status_code, headers
|
220
|
+
end
|
221
|
+
|
222
|
+
#
|
223
|
+
# Get a specific user by their ID.
|
224
|
+
# @param user_id The user identifier number
|
225
|
+
# @param [Hash] opts the optional parameters
|
226
|
+
# @return [InlineResponse2003]
|
227
|
+
def get_user(user_id, opts = {})
|
228
|
+
data, _status_code, _headers = get_user_with_http_info(user_id, opts)
|
229
|
+
return data
|
230
|
+
end
|
231
|
+
|
232
|
+
#
|
233
|
+
# Get a specific user by their ID.
|
234
|
+
# @param user_id The user identifier number
|
235
|
+
# @param [Hash] opts the optional parameters
|
236
|
+
# @return [Array<(InlineResponse2003, Fixnum, Hash)>] InlineResponse2003 data, response status code and response headers
|
237
|
+
def get_user_with_http_info(user_id, opts = {})
|
238
|
+
if @api_client.config.debugging
|
239
|
+
@api_client.config.logger.debug "Calling API: Api.get_user ..."
|
240
|
+
end
|
241
|
+
# verify the required parameter 'user_id' is set
|
242
|
+
fail ArgumentError, "Missing the required parameter 'user_id' when calling Api.get_user" if user_id.nil?
|
243
|
+
# resource path
|
244
|
+
local_var_path = "/users/{user-id}".sub('{format}','json').sub('{' + 'user-id' + '}', user_id.to_s)
|
245
|
+
|
246
|
+
# query parameters
|
247
|
+
query_params = {}
|
248
|
+
|
249
|
+
# header parameters
|
250
|
+
header_params = {}
|
251
|
+
|
252
|
+
# HTTP header 'Accept' (if needed)
|
253
|
+
local_header_accept = ['application/json']
|
254
|
+
local_header_accept_result = @api_client.select_header_accept(local_header_accept) and header_params['Accept'] = local_header_accept_result
|
255
|
+
|
256
|
+
# HTTP header 'Content-Type'
|
257
|
+
local_header_content_type = ['application/json']
|
258
|
+
header_params['Content-Type'] = @api_client.select_header_content_type(local_header_content_type)
|
259
|
+
|
260
|
+
# form parameters
|
261
|
+
form_params = {}
|
262
|
+
|
263
|
+
# http body (model)
|
264
|
+
post_body = nil
|
265
|
+
auth_names = ['apiKey']
|
266
|
+
data, status_code, headers = @api_client.call_api(:GET, local_var_path,
|
267
|
+
:header_params => header_params,
|
268
|
+
:query_params => query_params,
|
269
|
+
:form_params => form_params,
|
270
|
+
:body => post_body,
|
271
|
+
:auth_names => auth_names,
|
272
|
+
:return_type => 'InlineResponse2003')
|
273
|
+
if @api_client.config.debugging
|
274
|
+
@api_client.config.logger.debug "API called: Api#get_user\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
|
275
|
+
end
|
276
|
+
return data, status_code, headers
|
277
|
+
end
|
278
|
+
end
|
279
|
+
end
|
@@ -0,0 +1,371 @@
|
|
1
|
+
=begin
|
2
|
+
Wheniwork
|
3
|
+
|
4
|
+
OpenAPI spec version: v2
|
5
|
+
|
6
|
+
Generated by: https://github.com/swagger-api/swagger-codegen.git
|
7
|
+
|
8
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
9
|
+
you may not use this file except in compliance with the License.
|
10
|
+
You may obtain a copy of the License at
|
11
|
+
|
12
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
13
|
+
|
14
|
+
Unless required by applicable law or agreed to in writing, software
|
15
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
16
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
17
|
+
See the License for the specific language governing permissions and
|
18
|
+
limitations under the License.
|
19
|
+
|
20
|
+
=end
|
21
|
+
|
22
|
+
require 'date'
|
23
|
+
require 'json'
|
24
|
+
require 'logger'
|
25
|
+
require 'tempfile'
|
26
|
+
require 'typhoeus'
|
27
|
+
require 'uri'
|
28
|
+
|
29
|
+
module WhenIWork
|
30
|
+
class ApiClient
|
31
|
+
# The Configuration object holding settings to be used in the API client.
|
32
|
+
attr_accessor :config
|
33
|
+
|
34
|
+
# Defines the headers to be used in HTTP requests of all API calls by default.
|
35
|
+
#
|
36
|
+
# @return [Hash]
|
37
|
+
attr_accessor :default_headers
|
38
|
+
|
39
|
+
# Initializes the ApiClient
|
40
|
+
# @option config [Configuration] Configuraiton for initializing the object, default to Configuration.default
|
41
|
+
def initialize(config = Configuration.default)
|
42
|
+
@config = config
|
43
|
+
@user_agent = "Swagger-Codegen/#{VERSION}/ruby"
|
44
|
+
@default_headers = {
|
45
|
+
'Content-Type' => "application/json",
|
46
|
+
'User-Agent' => @user_agent
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.default
|
51
|
+
@@default ||= ApiClient.new
|
52
|
+
end
|
53
|
+
|
54
|
+
# Call an API with given options.
|
55
|
+
#
|
56
|
+
# @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
|
57
|
+
# the data deserialized from response body (could be nil), response status code and response headers.
|
58
|
+
def call_api(http_method, path, opts = {})
|
59
|
+
request = build_request(http_method, path, opts)
|
60
|
+
response = request.run
|
61
|
+
|
62
|
+
if @config.debugging
|
63
|
+
@config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
|
64
|
+
end
|
65
|
+
|
66
|
+
unless response.success?
|
67
|
+
fail ApiError.new(:code => response.code,
|
68
|
+
:response_headers => response.headers,
|
69
|
+
:response_body => response.body),
|
70
|
+
response.status_message
|
71
|
+
end
|
72
|
+
|
73
|
+
if opts[:return_type]
|
74
|
+
data = deserialize(response, opts[:return_type])
|
75
|
+
else
|
76
|
+
data = nil
|
77
|
+
end
|
78
|
+
return data, response.code, response.headers
|
79
|
+
end
|
80
|
+
|
81
|
+
# Builds the HTTP request
|
82
|
+
#
|
83
|
+
# @param [String] http_method HTTP method/verb (e.g. POST)
|
84
|
+
# @param [String] path URL path (e.g. /account/new)
|
85
|
+
# @option opts [Hash] :header_params Header parameters
|
86
|
+
# @option opts [Hash] :query_params Query parameters
|
87
|
+
# @option opts [Hash] :form_params Query parameters
|
88
|
+
# @option opts [Object] :body HTTP body (JSON/XML)
|
89
|
+
# @return [Typhoeus::Request] A Typhoeus Request
|
90
|
+
def build_request(http_method, path, opts = {})
|
91
|
+
url = build_request_url(path)
|
92
|
+
http_method = http_method.to_sym.downcase
|
93
|
+
|
94
|
+
header_params = @default_headers.merge(opts[:header_params] || {})
|
95
|
+
query_params = opts[:query_params] || {}
|
96
|
+
form_params = opts[:form_params] || {}
|
97
|
+
|
98
|
+
update_params_for_auth! header_params, query_params, opts[:auth_names]
|
99
|
+
|
100
|
+
req_opts = {
|
101
|
+
:method => http_method,
|
102
|
+
:headers => header_params,
|
103
|
+
:params => query_params,
|
104
|
+
:timeout => @config.timeout,
|
105
|
+
:ssl_verifypeer => @config.verify_ssl,
|
106
|
+
:sslcert => @config.cert_file,
|
107
|
+
:sslkey => @config.key_file,
|
108
|
+
:verbose => @config.debugging
|
109
|
+
}
|
110
|
+
|
111
|
+
req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
|
112
|
+
|
113
|
+
if [:post, :patch, :put, :delete].include?(http_method)
|
114
|
+
req_body = build_request_body(header_params, form_params, opts[:body])
|
115
|
+
req_opts.update :body => req_body
|
116
|
+
if @config.debugging
|
117
|
+
@config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
Typhoeus::Request.new(url, req_opts)
|
122
|
+
end
|
123
|
+
|
124
|
+
# Check if the given MIME is a JSON MIME.
|
125
|
+
# JSON MIME examples:
|
126
|
+
# application/json
|
127
|
+
# application/json; charset=UTF8
|
128
|
+
# APPLICATION/JSON
|
129
|
+
# @param [String] mime MIME
|
130
|
+
# @return [Boolean] True if the MIME is applicaton/json
|
131
|
+
def json_mime?(mime)
|
132
|
+
!(mime =~ /\Aapplication\/json(;.*)?\z/i).nil?
|
133
|
+
end
|
134
|
+
|
135
|
+
# Deserialize the response to the given return type.
|
136
|
+
#
|
137
|
+
# @param [Response] response HTTP response
|
138
|
+
# @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
|
139
|
+
def deserialize(response, return_type)
|
140
|
+
body = response.body
|
141
|
+
return nil if body.nil? || body.empty?
|
142
|
+
|
143
|
+
# return response body directly for String return type
|
144
|
+
return body if return_type == 'String'
|
145
|
+
|
146
|
+
# handle file downloading - save response body into a tmp file and return the File instance
|
147
|
+
return download_file(response) if return_type == 'File'
|
148
|
+
|
149
|
+
# ensuring a default content type
|
150
|
+
content_type = response.headers['Content-Type'] || 'application/json'
|
151
|
+
|
152
|
+
fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
|
153
|
+
|
154
|
+
begin
|
155
|
+
data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
|
156
|
+
rescue JSON::ParserError => e
|
157
|
+
if %w(String Date DateTime).include?(return_type)
|
158
|
+
data = body
|
159
|
+
else
|
160
|
+
raise e
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
convert_to_type data, return_type
|
165
|
+
end
|
166
|
+
|
167
|
+
# Convert data to the given return type.
|
168
|
+
# @param [Object] data Data to be converted
|
169
|
+
# @param [String] return_type Return type
|
170
|
+
# @return [Mixed] Data in a particular type
|
171
|
+
def convert_to_type(data, return_type)
|
172
|
+
return nil if data.nil?
|
173
|
+
case return_type
|
174
|
+
when 'String'
|
175
|
+
data.to_s
|
176
|
+
when 'Integer'
|
177
|
+
data.to_i
|
178
|
+
when 'Float'
|
179
|
+
data.to_f
|
180
|
+
when 'BOOLEAN'
|
181
|
+
data == true
|
182
|
+
when 'DateTime'
|
183
|
+
# parse date time (expecting ISO 8601 format)
|
184
|
+
DateTime.parse data
|
185
|
+
when 'Date'
|
186
|
+
# parse date time (expecting ISO 8601 format)
|
187
|
+
Date.parse data
|
188
|
+
when 'Object'
|
189
|
+
# generic object (usually a Hash), return directly
|
190
|
+
data
|
191
|
+
when /\AArray<(.+)>\z/
|
192
|
+
# e.g. Array<Pet>
|
193
|
+
sub_type = $1
|
194
|
+
data.map {|item| convert_to_type(item, sub_type) }
|
195
|
+
when /\AHash\<String, (.+)\>\z/
|
196
|
+
# e.g. Hash<String, Integer>
|
197
|
+
sub_type = $1
|
198
|
+
{}.tap do |hash|
|
199
|
+
data.each {|k, v| hash[k] = convert_to_type(v, sub_type) }
|
200
|
+
end
|
201
|
+
else
|
202
|
+
# models, e.g. Pet
|
203
|
+
WhenIWork.const_get(return_type).new.tap do |model|
|
204
|
+
model.build_from_hash data
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
# Save response body into a file in (the defined) temporary folder, using the filename
|
210
|
+
# from the "Content-Disposition" header if provided, otherwise a random filename.
|
211
|
+
#
|
212
|
+
# @see Configuration#temp_folder_path
|
213
|
+
# @return [Tempfile] the file downloaded
|
214
|
+
def download_file(response)
|
215
|
+
content_disposition = response.headers['Content-Disposition']
|
216
|
+
if content_disposition
|
217
|
+
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
|
218
|
+
prefix = sanitize_filename(filename)
|
219
|
+
else
|
220
|
+
prefix = 'download-'
|
221
|
+
end
|
222
|
+
prefix = prefix + '-' unless prefix.end_with?('-')
|
223
|
+
|
224
|
+
tempfile = nil
|
225
|
+
encoding = response.body.encoding
|
226
|
+
Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding) do |file|
|
227
|
+
file.write(response.body)
|
228
|
+
tempfile = file
|
229
|
+
end
|
230
|
+
@config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
|
231
|
+
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
|
232
|
+
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
233
|
+
"explicitly with `tempfile.delete`"
|
234
|
+
tempfile
|
235
|
+
end
|
236
|
+
|
237
|
+
# Sanitize filename by removing path.
|
238
|
+
# e.g. ../../sun.gif becomes sun.gif
|
239
|
+
#
|
240
|
+
# @param [String] filename the filename to be sanitized
|
241
|
+
# @return [String] the sanitized filename
|
242
|
+
def sanitize_filename(filename)
|
243
|
+
filename.gsub(/.*[\/\\]/, '')
|
244
|
+
end
|
245
|
+
|
246
|
+
def build_request_url(path)
|
247
|
+
# Add leading and trailing slashes to path
|
248
|
+
path = "/#{path}".gsub(/\/+/, '/')
|
249
|
+
URI.encode(@config.base_url + path)
|
250
|
+
end
|
251
|
+
|
252
|
+
# Builds the HTTP request body
|
253
|
+
#
|
254
|
+
# @param [Hash] header_params Header parameters
|
255
|
+
# @param [Hash] form_params Query parameters
|
256
|
+
# @param [Object] body HTTP body (JSON/XML)
|
257
|
+
# @return [String] HTTP body data in the form of string
|
258
|
+
def build_request_body(header_params, form_params, body)
|
259
|
+
# http form
|
260
|
+
if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
|
261
|
+
header_params['Content-Type'] == 'multipart/form-data'
|
262
|
+
data = {}
|
263
|
+
form_params.each do |key, value|
|
264
|
+
case value
|
265
|
+
when File, Array, nil
|
266
|
+
# let typhoeus handle File, Array and nil parameters
|
267
|
+
data[key] = value
|
268
|
+
else
|
269
|
+
data[key] = value.to_s
|
270
|
+
end
|
271
|
+
end
|
272
|
+
elsif body
|
273
|
+
data = body.is_a?(String) ? body : body.to_json
|
274
|
+
else
|
275
|
+
data = nil
|
276
|
+
end
|
277
|
+
data
|
278
|
+
end
|
279
|
+
|
280
|
+
# Update hearder and query params based on authentication settings.
|
281
|
+
#
|
282
|
+
# @param [Hash] header_params Header parameters
|
283
|
+
# @param [Hash] form_params Query parameters
|
284
|
+
# @param [String] auth_names Authentication scheme name
|
285
|
+
def update_params_for_auth!(header_params, query_params, auth_names)
|
286
|
+
Array(auth_names).each do |auth_name|
|
287
|
+
auth_setting = @config.auth_settings[auth_name]
|
288
|
+
next unless auth_setting
|
289
|
+
case auth_setting[:in]
|
290
|
+
when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
|
291
|
+
when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
|
292
|
+
else fail ArgumentError, 'Authentication token must be in `query` of `header`'
|
293
|
+
end
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
# Sets user agent in HTTP header
|
298
|
+
#
|
299
|
+
# @param [String] user_agent User agent (e.g. swagger-codegen/ruby/1.0.0)
|
300
|
+
def user_agent=(user_agent)
|
301
|
+
@user_agent = user_agent
|
302
|
+
@default_headers['User-Agent'] = @user_agent
|
303
|
+
end
|
304
|
+
|
305
|
+
# Return Accept header based on an array of accepts provided.
|
306
|
+
# @param [Array] accepts array for Accept
|
307
|
+
# @return [String] the Accept header (e.g. application/json)
|
308
|
+
def select_header_accept(accepts)
|
309
|
+
return nil if accepts.nil? || accepts.empty?
|
310
|
+
# use JSON when present, otherwise use all of the provided
|
311
|
+
json_accept = accepts.find { |s| json_mime?(s) }
|
312
|
+
return json_accept || accepts.join(',')
|
313
|
+
end
|
314
|
+
|
315
|
+
# Return Content-Type header based on an array of content types provided.
|
316
|
+
# @param [Array] content_types array for Content-Type
|
317
|
+
# @return [String] the Content-Type header (e.g. application/json)
|
318
|
+
def select_header_content_type(content_types)
|
319
|
+
# use application/json by default
|
320
|
+
return 'application/json' if content_types.nil? || content_types.empty?
|
321
|
+
# use JSON when present, otherwise use the first one
|
322
|
+
json_content_type = content_types.find { |s| json_mime?(s) }
|
323
|
+
return json_content_type || content_types.first
|
324
|
+
end
|
325
|
+
|
326
|
+
# Convert object (array, hash, object, etc) to JSON string.
|
327
|
+
# @param [Object] model object to be converted into JSON string
|
328
|
+
# @return [String] JSON string representation of the object
|
329
|
+
def object_to_http_body(model)
|
330
|
+
return model if model.nil? || model.is_a?(String)
|
331
|
+
local_body = nil
|
332
|
+
if model.is_a?(Array)
|
333
|
+
local_body = model.map{|m| object_to_hash(m) }
|
334
|
+
else
|
335
|
+
local_body = object_to_hash(model)
|
336
|
+
end
|
337
|
+
local_body.to_json
|
338
|
+
end
|
339
|
+
|
340
|
+
# Convert object(non-array) to hash.
|
341
|
+
# @param [Object] obj object to be converted into JSON string
|
342
|
+
# @return [String] JSON string representation of the object
|
343
|
+
def object_to_hash(obj)
|
344
|
+
if obj.respond_to?(:to_hash)
|
345
|
+
obj.to_hash
|
346
|
+
else
|
347
|
+
obj
|
348
|
+
end
|
349
|
+
end
|
350
|
+
|
351
|
+
# Build parameter value according to the given collection format.
|
352
|
+
# @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
|
353
|
+
def build_collection_param(param, collection_format)
|
354
|
+
case collection_format
|
355
|
+
when :csv
|
356
|
+
param.join(',')
|
357
|
+
when :ssv
|
358
|
+
param.join(' ')
|
359
|
+
when :tsv
|
360
|
+
param.join("\t")
|
361
|
+
when :pipes
|
362
|
+
param.join('|')
|
363
|
+
when :multi
|
364
|
+
# return the array directly as typhoeus will handle it as expected
|
365
|
+
param
|
366
|
+
else
|
367
|
+
fail "unknown collection format: #{collection_format.inspect}"
|
368
|
+
end
|
369
|
+
end
|
370
|
+
end
|
371
|
+
end
|