parliament-ruby 1.0.0.pre → 1.0.0.pre2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/parliament/request/base_request.rb +35 -27
- data/lib/parliament/request/url_request.rb +1 -0
- data/lib/parliament/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3bfb325fe40f6c44c801632f76474d5ca5d84918
|
4
|
+
data.tar.gz: e2e347bdd4b29006da35915c44dfbef52544c8f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b2135460fb79a4f3c738a9e900493984bd7594b39ec9b68991f0002e5033dcb59ab70aabcf3b0665043af3ea33e9f6a13872c18f20b7737265492255098b3275
|
7
|
+
data.tar.gz: 3f96a1e82ff152c6211abbbea4b20611cf1a72213f4e29ef9eed35c6f055dff755d6e1c21502b6ee1fb3609a9490304afa8f111d1d5324fed0343efb7e20da70
|
@@ -90,24 +90,21 @@ module Parliament
|
|
90
90
|
def get(params: nil)
|
91
91
|
Typhoeus::Config.user_agent = 'Ruby'
|
92
92
|
|
93
|
-
|
94
|
-
|
95
|
-
endpoint_uri = URI.parse(query_url)
|
96
|
-
endpoint_params = URI.encode_www_form(@query_params.to_a) unless @query_params.empty?
|
93
|
+
uri_hash = separate_uri(query_url, @query_params, params)
|
97
94
|
|
98
95
|
typhoeus_request = Typhoeus::Request.new(
|
99
|
-
|
96
|
+
uri_hash[:endpoint],
|
100
97
|
method: :get,
|
101
|
-
params:
|
98
|
+
params: uri_hash[:params],
|
102
99
|
headers: headers,
|
103
100
|
accept_encoding: 'gzip'
|
104
101
|
)
|
105
102
|
|
106
|
-
|
103
|
+
response = typhoeus_request.run
|
107
104
|
|
108
|
-
handle_errors(
|
105
|
+
handle_errors(response)
|
109
106
|
|
110
|
-
build_response(
|
107
|
+
build_response(response)
|
111
108
|
end
|
112
109
|
|
113
110
|
# Makes an HTTP POST request and process results into a response.
|
@@ -138,26 +135,23 @@ module Parliament
|
|
138
135
|
def post(params: nil, body: nil, timeout: 60)
|
139
136
|
Typhoeus::Config.user_agent = 'Ruby'
|
140
137
|
|
141
|
-
|
142
|
-
|
143
|
-
endpoint_uri = URI.parse(query_url)
|
144
|
-
endpoint_params = URI.encode_www_form(@query_params.to_a) unless @query_params.empty?
|
138
|
+
uri_hash = separate_uri(query_url, @query_params, params)
|
145
139
|
|
146
140
|
typhoeus_request = Typhoeus::Request.new(
|
147
|
-
|
141
|
+
uri_hash[:endpoint],
|
148
142
|
method: :post,
|
149
|
-
params:
|
143
|
+
params: uri_hash[:params],
|
150
144
|
headers: headers.merge({ 'Content-Type' => 'application/json' }),
|
151
145
|
accept_encoding: 'gzip',
|
152
146
|
timeout: timeout,
|
153
147
|
body: body
|
154
148
|
)
|
155
149
|
|
156
|
-
|
150
|
+
response = typhoeus_request.run
|
157
151
|
|
158
|
-
handle_errors(
|
152
|
+
handle_errors(response)
|
159
153
|
|
160
|
-
build_response(
|
154
|
+
build_response(response)
|
161
155
|
end
|
162
156
|
|
163
157
|
private
|
@@ -168,8 +162,8 @@ module Parliament
|
|
168
162
|
attr_accessor :base_url, :headers
|
169
163
|
end
|
170
164
|
|
171
|
-
def build_response(
|
172
|
-
@builder.new(response:
|
165
|
+
def build_response(response)
|
166
|
+
@builder.new(response: response, decorators: @decorators).build
|
173
167
|
end
|
174
168
|
|
175
169
|
def query_url
|
@@ -184,17 +178,31 @@ module Parliament
|
|
184
178
|
default_headers.merge(@headers)
|
185
179
|
end
|
186
180
|
|
187
|
-
def handle_errors(
|
188
|
-
exception_class = if
|
189
|
-
Parliament::NoContentResponseError if
|
190
|
-
(
|
191
|
-
elsif /\A4\w{2}/.match(
|
181
|
+
def handle_errors(response)
|
182
|
+
exception_class = if response.success? # 2xx Status
|
183
|
+
Parliament::NoContentResponseError if response.headers&.[]('Content-Length') == '0' ||
|
184
|
+
(response.headers&.[]('Content-Length').nil? && response.body.empty?)
|
185
|
+
elsif /\A4\w{2}/.match(response.code.to_s) # 4xx Status
|
192
186
|
Parliament::ClientError
|
193
|
-
elsif /\A5\w{2}/.match(
|
187
|
+
elsif /\A5\w{2}/.match(response.code.to_s) # 5xx Status
|
194
188
|
Parliament::ServerError
|
195
189
|
end
|
196
190
|
|
197
|
-
raise exception_class.new(query_url,
|
191
|
+
raise exception_class.new(query_url, response) if exception_class
|
192
|
+
end
|
193
|
+
|
194
|
+
def separate_uri(query_url, query_params, additional_params)
|
195
|
+
endpoint = URI.parse(query_url)
|
196
|
+
|
197
|
+
temp_params = query_params
|
198
|
+
temp_params = temp_params.merge(URI.decode_www_form(endpoint.query)) if endpoint.query
|
199
|
+
temp_params = temp_params.merge(additional_params) unless additional_params.nil?
|
200
|
+
|
201
|
+
endpoint.query = nil
|
202
|
+
|
203
|
+
encoded_params = URI.encode_www_form(temp_params.to_a) unless temp_params.empty?
|
204
|
+
|
205
|
+
{ endpoint: endpoint, params: encoded_params }
|
198
206
|
end
|
199
207
|
end
|
200
208
|
end
|
data/lib/parliament/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parliament-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.
|
4
|
+
version: 1.0.0.pre2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Rayner
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-03-
|
11
|
+
date: 2018-03-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: typhoeus
|