rail-locator-api 1.0.22 → 1.0.24
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rail-locator-api/api_request.rb +72 -7
- data/lib/rail-locator-api/version.rb +1 -1
- data/lib/rail-locator-api.rb +1 -0
- metadata +15 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8bd00871ef27b6d49f125af92cd42cc3a9962f36f0df2a40cba25edebfea0ba9
|
4
|
+
data.tar.gz: 0e4cdf61f4bbd87eed4ea3e5f75404b86dc3625a7319658ad23a1145f26b3737
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91dbeb069e8f2a59d21df3efcef7d3ba0de201d6cb24928ea869aa5705d6e932a9aad8039ecdcadf52f817bbe0f64883c5921b44a6e60fade91376e9316edcd0
|
7
|
+
data.tar.gz: 1c10769ea83bdd40651bfcc8728a521f00a14dfe8f3b83fc179dd9efbef93999b82bed9a5574824427d03a50f5611a2fc8e34757734494d3602a983a5983fd32
|
@@ -8,13 +8,21 @@ module RailLocatorApi
|
|
8
8
|
def post(params: nil, headers: nil, format: nil, body: {})
|
9
9
|
validate_api_key
|
10
10
|
begin
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
if is_multipart?(body)
|
12
|
+
url = URI("#{self.api_url}#{format.present? ? ".#{format}": ""}")
|
13
|
+
http = Net::HTTP.new(url.host, url.port);
|
14
|
+
request = Net::HTTP::Post.new(url)
|
15
|
+
request.set_form body.to_params, 'multipart/form-data'
|
16
|
+
http.request(request)
|
16
17
|
else
|
17
|
-
response
|
18
|
+
response = self.rest_client(format).post do |request|
|
19
|
+
configure_request(request: request, params: params, headers: headers, body: body)
|
20
|
+
end
|
21
|
+
if [nil, 'json'].include?(format)
|
22
|
+
parse_response(response)
|
23
|
+
else
|
24
|
+
response
|
25
|
+
end
|
18
26
|
end
|
19
27
|
rescue => e
|
20
28
|
handle_error(e)
|
@@ -221,12 +229,31 @@ module RailLocatorApi
|
|
221
229
|
request.headers['X-Is-Allow-Access-To-Coordinates'] = "true"
|
222
230
|
end
|
223
231
|
request.headers.merge!(headers) if headers
|
224
|
-
|
232
|
+
|
233
|
+
request.body = MultiJson.dump(body) if body && request.headers['Content-Type'] == 'application/json'
|
234
|
+
|
225
235
|
request.options.timeout = self.timeout
|
226
236
|
request.options.open_timeout = self.open_timeout
|
227
237
|
end
|
228
238
|
end
|
229
239
|
|
240
|
+
def is_multipart?(body, result=false)
|
241
|
+
if body.is_a?(Hash)
|
242
|
+
body.inject({}) do |acc, kv|
|
243
|
+
result = is_multipart?(kv.last, result)
|
244
|
+
end
|
245
|
+
elsif body.is_a?(Array)
|
246
|
+
body.map do |item|
|
247
|
+
result = is_multipart?(item, result)
|
248
|
+
end
|
249
|
+
result
|
250
|
+
elsif body.is_a?(ActionDispatch::Http::UploadedFile)
|
251
|
+
return true
|
252
|
+
else
|
253
|
+
result || false
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
230
257
|
def rest_client(format=nil)
|
231
258
|
client = Faraday.new("#{self.api_url}#{format.present? ? ".#{format}": ""}", proxy: self.proxy,
|
232
259
|
ssl: self.ssl_options) do |faraday|
|
@@ -316,3 +343,41 @@ module RailLocatorApi
|
|
316
343
|
end
|
317
344
|
end
|
318
345
|
end
|
346
|
+
|
347
|
+
class Hash
|
348
|
+
def to_params(namespace = nil)
|
349
|
+
query = []
|
350
|
+
each do |key, value|
|
351
|
+
if value.present?
|
352
|
+
if value.is_a?(Hash) || value.is_a?(Array)
|
353
|
+
query += value.to_params(namespace ? "#{namespace}[#{key}]" : key)
|
354
|
+
else
|
355
|
+
query << value.to_params(namespace ? "#{namespace}[#{key}]" : key)
|
356
|
+
end
|
357
|
+
end
|
358
|
+
end
|
359
|
+
query
|
360
|
+
end
|
361
|
+
end
|
362
|
+
|
363
|
+
class Object
|
364
|
+
def to_params(key)
|
365
|
+
if self.is_a? ActionDispatch::Http::UploadedFile
|
366
|
+
[key.to_param, File.open(self.path)]
|
367
|
+
else
|
368
|
+
[key.to_param, to_param.to_s]
|
369
|
+
end
|
370
|
+
end
|
371
|
+
end
|
372
|
+
|
373
|
+
class Array
|
374
|
+
def to_params(key)
|
375
|
+
prefix = "#{key}[]"
|
376
|
+
|
377
|
+
if empty?
|
378
|
+
nil.to_params(prefix)
|
379
|
+
else
|
380
|
+
collect { |value| value.to_params(prefix) }
|
381
|
+
end
|
382
|
+
end
|
383
|
+
end
|
data/lib/rail-locator-api.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rail-locator-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.24
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pavel Osetrov
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: multipart-post
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
83
97
|
description: ''
|
84
98
|
email: pavel.osetrov@me.com
|
85
99
|
executables: []
|