activeclient_api 0.2.0 → 0.3.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 +4 -4
- data/lib/active_client/base.rb +45 -10
- data/lib/active_client/version.rb +1 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 17397df41df4b55d54dc885d93b74652be2794ba7c30f5eedd60a6fdbdae9cc1
|
4
|
+
data.tar.gz: 8e0b93cb879059aab94a144665d26120186f504c258cc6a1a85b108164235017
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 35cd5177011d95834c3a2b567c7ac19173301d655a08e370583bf4a2427c0c7edb38d996436b05d960a753fb6b36b8ca7fd4c63c4d5a5cf0a6517156901123a3
|
7
|
+
data.tar.gz: 74932e172488e41b68dd297db60a4d195ee67565fec08623be329668cb0f1bb9ee3d295f53f57105b1c8b0ca14787f8a676f40cc12f069f1c80ecfa578c8eb32
|
data/lib/active_client/base.rb
CHANGED
@@ -1,6 +1,19 @@
|
|
1
1
|
class ActiveClient::Base
|
2
2
|
Response = Struct.new(:body, :success?)
|
3
3
|
|
4
|
+
def self.with_persistent_connection(&)
|
5
|
+
@http = Net::HTTP::Persistent.new name: "gemini"
|
6
|
+
|
7
|
+
yield
|
8
|
+
|
9
|
+
@http.shutdown
|
10
|
+
@http = nil
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.http
|
14
|
+
@http
|
15
|
+
end
|
16
|
+
|
4
17
|
def get(path, skip_parsing: false, **query)
|
5
18
|
make_request(Net::HTTP::Get, path, skip_parsing:, query:)
|
6
19
|
end
|
@@ -29,17 +42,24 @@ class ActiveClient::Base
|
|
29
42
|
|
30
43
|
def instrument(klass:, path:, query:, body:)
|
31
44
|
uri, http, request = construct_request(klass:, path:, query:)
|
32
|
-
|
45
|
+
|
46
|
+
set_body(request, body)
|
47
|
+
|
33
48
|
loggable = loggable_uri(uri)
|
34
49
|
args = { name: self.class.name.demodulize, uri: loggable }
|
35
50
|
|
36
51
|
ActiveSupport::Notifications.
|
37
|
-
instrument("request.active_client", args) { yield http, request }
|
52
|
+
instrument("request.active_client", args) { yield uri, http, request }
|
38
53
|
end
|
39
54
|
|
40
55
|
def make_request(klass, path, skip_parsing: false, query: {}, body: {})
|
41
|
-
instrument(klass:, path:, query:, body:) do |http, request|
|
42
|
-
response =
|
56
|
+
instrument(klass:, path:, query:, body:) do |uri, http, request|
|
57
|
+
response =
|
58
|
+
if http.is_a?(Net::HTTP::Persistent)
|
59
|
+
http.request(uri, request)
|
60
|
+
else
|
61
|
+
http.request(request)
|
62
|
+
end
|
43
63
|
|
44
64
|
Response.new(parse_response(response.body, skip_parsing),
|
45
65
|
response.is_a?(Net::HTTPSuccess))
|
@@ -57,11 +77,13 @@ class ActiveClient::Base
|
|
57
77
|
def construct_request(klass:, path:, query:)
|
58
78
|
uri = construct_uri(path:, query:)
|
59
79
|
|
60
|
-
http =
|
61
|
-
|
62
|
-
|
80
|
+
http =
|
81
|
+
self.class.http.presence || Net::HTTP.new(uri.host, uri.port).tap do
|
82
|
+
it.use_ssl = uri.instance_of?(URI::HTTPS)
|
83
|
+
it.read_timeout = 1200
|
84
|
+
end
|
63
85
|
|
64
|
-
[uri, http, klass.new(uri.request_uri, default_headers)]
|
86
|
+
[uri, http, klass.new(uri.request_uri, default_headers(klass))]
|
65
87
|
end
|
66
88
|
|
67
89
|
def construct_uri(path:, query:)
|
@@ -78,7 +100,7 @@ class ActiveClient::Base
|
|
78
100
|
uri.query = URI.encode_www_form(default_query.merge(query))
|
79
101
|
end
|
80
102
|
|
81
|
-
def default_headers
|
103
|
+
def default_headers(_klass)
|
82
104
|
{ "Accept" => "application/json",
|
83
105
|
"Content-Type" => "application/json" }
|
84
106
|
end
|
@@ -116,7 +138,7 @@ class ActiveClient::Base
|
|
116
138
|
end
|
117
139
|
end
|
118
140
|
|
119
|
-
def deep_inheritable_options(obj)
|
141
|
+
def deep_inheritable_options(obj) # rubocop:disable Metrics/MethodLength
|
120
142
|
case obj
|
121
143
|
when Hash
|
122
144
|
inherited = ActiveSupport::InheritableOptions.new
|
@@ -130,4 +152,17 @@ class ActiveClient::Base
|
|
130
152
|
obj
|
131
153
|
end
|
132
154
|
end
|
155
|
+
|
156
|
+
def set_body(request, body)
|
157
|
+
if body.present? &&
|
158
|
+
request["Content-Type"] == "application/x-www-form-urlencoded"
|
159
|
+
set_form_data(request, body)
|
160
|
+
elsif body.present?
|
161
|
+
request.body = default_body.merge(body).to_json
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
def set_form_data(request, body)
|
166
|
+
request.set_form_data(default_body.merge(body))
|
167
|
+
end
|
133
168
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activeclient_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Pezza
|
@@ -9,6 +9,20 @@ bindir: bin
|
|
9
9
|
cert_chain: []
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: net-http-persistent
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '0'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '0'
|
12
26
|
- !ruby/object:Gem::Dependency
|
13
27
|
name: rails
|
14
28
|
requirement: !ruby/object:Gem::Requirement
|
@@ -56,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
56
70
|
- !ruby/object:Gem::Version
|
57
71
|
version: '0'
|
58
72
|
requirements: []
|
59
|
-
rubygems_version: 3.6.
|
73
|
+
rubygems_version: 3.6.7
|
60
74
|
specification_version: 4
|
61
75
|
summary: Basic client for make api classes
|
62
76
|
test_files: []
|