api_caller 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/api_caller.rb +28 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac28f76e8d98dbe79734c9a5f856dc45effcf2c0
|
4
|
+
data.tar.gz: f9a916d49e3f6aacd950131bca2418d85b0dca6a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5308ea5fd5c1d30718c7d13be5e6058201edaa06667a1ac3cfbd56fe2b7653c0e284c3afd0b96e23840693b287e36e6c61005fdc85fd139a1f354a5653b3155b
|
7
|
+
data.tar.gz: 6acfa9365c84e7bb8f1ce37d96d4305078896fcdd9a7ef92499a8ccbab285163a92b01fbe2f35abe8276712abe306a6c9076890716133a27868e5287d5b2a418
|
data/lib/api_caller.rb
CHANGED
@@ -19,21 +19,45 @@ class ApiCaller
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def self.post(uri_string:, params:, content_type: nil, logger: nil)
|
22
|
+
if content_type
|
23
|
+
body_params_post(uri_string: uri_string, params: params, content_type: content_type, logger: logger)
|
24
|
+
else
|
25
|
+
url_params_post(uri_string: uri_string, params: params, logger: logger)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def self.url_params_post(uri_string:, params:, logger: nil)
|
22
32
|
response = nil
|
23
33
|
|
24
34
|
uri = URI(uri_string)
|
25
35
|
begin
|
26
36
|
Net::HTTP.start(uri.host, uri.port, {use_ssl: uri.scheme == 'https', open_timeout: 1}) do |http|
|
27
|
-
request =
|
37
|
+
request = Net::HTTP::Post.new(uri)
|
38
|
+
post_data = URI.encode_www_form(params)
|
39
|
+
response = http.request(request, post_data)
|
40
|
+
response.body
|
41
|
+
end
|
42
|
+
rescue Exception => e
|
43
|
+
logger.error("[API FAIL] - URI: [#{uri_string}] - Message: #{e.message}\n #{e.backtrace.join("\n ")}") if logger
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.body_params_post(uri_string:, params:, content_type: nil, logger: nil)
|
48
|
+
response = nil
|
49
|
+
|
50
|
+
uri = URI(uri_string)
|
51
|
+
begin
|
52
|
+
Net::HTTP.start(uri.host, uri.port, {use_ssl: uri.scheme == 'https', open_timeout: 1}) do |http|
|
53
|
+
request = Net::HTTP::Post.new(uri, content_type)
|
28
54
|
request.body = params
|
29
55
|
response = http.request(request)
|
56
|
+
response.body
|
30
57
|
end
|
31
58
|
rescue Exception => e
|
32
59
|
logger.error("[API FAIL] - URI: [#{uri_string}] - Message: #{e.message}\n #{e.backtrace.join("\n ")}") if logger
|
33
60
|
end
|
34
|
-
|
35
|
-
response
|
36
61
|
end
|
37
62
|
|
38
|
-
|
39
63
|
end
|