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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 291f639647bb81fcc5dd82a384cd0ecff70db477
4
- data.tar.gz: 26f6551f09bcd80f4ff351ba55189bb77bc71580
3
+ metadata.gz: 3bfb325fe40f6c44c801632f76474d5ca5d84918
4
+ data.tar.gz: e2e347bdd4b29006da35915c44dfbef52544c8f5
5
5
  SHA512:
6
- metadata.gz: 1dd49cab50167b71972428f3d53f61f22f72b8ff8d7dd69af0607d616d9280e19fee121864697ed9e423a0981e72b90ca8eae03b85146f8c6885f2a7b7d865a1
7
- data.tar.gz: 321ece67fd29fea8af22fcd3197a12218edb9443a3760e4525ed84a158fe3ba787a9ca38c8c5a4abd5501d01f867c2e73b12ee8b1c03e09f11ce13f82e2575a3
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
- @query_params = @query_params.merge(params) unless params.nil?
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
- endpoint_uri,
96
+ uri_hash[:endpoint],
100
97
  method: :get,
101
- params: endpoint_params,
98
+ params: uri_hash[:params],
102
99
  headers: headers,
103
100
  accept_encoding: 'gzip'
104
101
  )
105
102
 
106
- net_response = typhoeus_request.run
103
+ response = typhoeus_request.run
107
104
 
108
- handle_errors(net_response)
105
+ handle_errors(response)
109
106
 
110
- build_response(net_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
- @query_params = @query_params.merge(params) unless params.nil?
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
- endpoint_uri,
141
+ uri_hash[:endpoint],
148
142
  method: :post,
149
- params: endpoint_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
- net_response = typhoeus_request.run
150
+ response = typhoeus_request.run
157
151
 
158
- handle_errors(net_response)
152
+ handle_errors(response)
159
153
 
160
- build_response(net_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(net_response)
172
- @builder.new(response: net_response, decorators: @decorators).build
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(net_response)
188
- exception_class = if net_response.success? # 2xx Status
189
- Parliament::NoContentResponseError if net_response.headers&.[]('Content-Length') == '0' ||
190
- (net_response.headers&.[]('Content-Length').nil? && net_response.body.empty?)
191
- elsif /\A4\w{2}/.match(net_response.code.to_s) # 4xx Status
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(net_response.code.to_s) # 5xx Status
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, net_response) if exception_class
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
@@ -73,6 +73,7 @@ module Parliament
73
73
  uri_string = [@base_url, @endpoint_parts].join('/').gsub(' ', '%20')
74
74
 
75
75
  uri = URI.parse(uri_string)
76
+ uri.query = URI.encode_www_form(@query_params) unless @query_params.empty?
76
77
 
77
78
  uri.to_s
78
79
  end
@@ -1,3 +1,3 @@
1
1
  module Parliament
2
- VERSION = '1.0.0.pre'.freeze
2
+ VERSION = '1.0.0.pre2'.freeze
3
3
  end
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.pre
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-16 00:00:00.000000000 Z
11
+ date: 2018-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus