fluidfeatures 0.3.4 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,6 +14,9 @@ module FluidFeatures
14
14
 
15
15
  API_REQUEST_LOG_MAX_SIZE = 200
16
16
 
17
+ # Do not gzip request or response body if size is under N bytes
18
+ MIN_GZIP_SIZE = 1024
19
+
17
20
  def initialize(base_uri, logger)
18
21
 
19
22
  @uuid = UUID.new.generate
@@ -83,8 +86,10 @@ module FluidFeatures
83
86
  begin
84
87
 
85
88
  request = Net::HTTP::Get.new url_path
89
+ request["Authorization"] = auth_token
86
90
  request["Accept"] = "application/json"
87
- request['AUTHORIZATION'] = auth_token
91
+ request["Accept-Encoding"] = "gzip"
92
+
88
93
  @etags_lock.synchronize do
89
94
  if cache and @etags.has_key? url_path
90
95
  request["If-None-Match"] = @etags[url_path][:etag]
@@ -101,7 +106,7 @@ module FluidFeatures
101
106
  no_change = true
102
107
  success = true
103
108
  elsif response.is_a? Net::HTTPSuccess
104
- payload = JSON.load(response.body) rescue nil
109
+ payload = parse_response_body response
105
110
  if cache
106
111
  @etags_lock.synchronize do
107
112
  @etags[url_path] = {
@@ -112,7 +117,7 @@ module FluidFeatures
112
117
  end
113
118
  success = true
114
119
  else
115
- payload = JSON.load(response.body) rescue nil
120
+ payload = parse_response_body response
116
121
  if payload and payload.is_a? Hash and payload.has_key? "error"
117
122
  err_msg = payload["error"]
118
123
  end
@@ -143,11 +148,12 @@ module FluidFeatures
143
148
  err_msg = nil
144
149
  success = false
145
150
  begin
151
+
146
152
  request = Net::HTTP::Put.new uri_path
147
- request["Content-Type"] = "application/json"
153
+ request["Authorization"] = auth_token
148
154
  request["Accept"] = "application/json"
149
- request['AUTHORIZATION'] = auth_token
150
- request.body = JSON.dump(payload)
155
+ request["Accept-Encoding"] = "gzip"
156
+ encode_request_body(request, payload)
151
157
 
152
158
  request_start_time = Time.now
153
159
  response = @http.request uri, request
@@ -158,7 +164,7 @@ module FluidFeatures
158
164
  if response.is_a? Net::HTTPSuccess
159
165
  success = true
160
166
  else
161
- response_payload = JSON.load(response.body) rescue nil
167
+ response_payload = parse_response_body response
162
168
  if response_payload.is_a? Hash and response_payload.has_key? "error"
163
169
  err_msg = response_payload["error"]
164
170
  end
@@ -183,11 +189,12 @@ module FluidFeatures
183
189
  err_msg = nil
184
190
  success = false
185
191
  begin
192
+
186
193
  request = Net::HTTP::Post.new url_path
187
- request["Content-Type"] = "application/json"
188
194
  request["Accept"] = "application/json"
189
- request['AUTHORIZATION'] = auth_token
190
- request.body = JSON.dump(payload)
195
+ request["Accept-Encoding"] = "gzip"
196
+ request["Authorization"] = auth_token
197
+ encode_request_body(request, payload)
191
198
 
192
199
  request_start_time = Time.now
193
200
  response = @http.request request
@@ -198,7 +205,7 @@ module FluidFeatures
198
205
  if response.is_a? Net::HTTPSuccess
199
206
  success = true
200
207
  else
201
- response_payload = JSON.load(response.body) rescue nil
208
+ response_payload = parse_response_body response
202
209
  if response_payload.is_a? Hash and response_payload.has_key? "error"
203
210
  err_msg = response_payload["error"]
204
211
  end
@@ -215,5 +222,35 @@ module FluidFeatures
215
222
  return success
216
223
  end
217
224
 
225
+ def parse_response_body response
226
+ content = response.body
227
+ if response["Content-Encoding"] == "gzip"
228
+ content = Zlib::GzipReader.new(
229
+ StringIO.new(content)
230
+ ).read
231
+ end
232
+ JSON.load(content) rescue nil
233
+ end
234
+
235
+ def encode_request_body(request, payload, encoding="gzip")
236
+
237
+ # Encode as JSON string
238
+ content = JSON.dump(payload)
239
+
240
+ # Gzip compress if necessary
241
+ if encoding == "gzip" and content.size >= MIN_GZIP_SIZE
242
+ compressed = StringIO.new
243
+ gz_writer = Zlib::GzipWriter.new(compressed)
244
+ gz_writer.write(content)
245
+ gz_writer.close
246
+ content = compressed.string
247
+ end
248
+
249
+ request["Content-Type"] = "application/json"
250
+ request["Content-Encoding"] = encoding
251
+ request.body = content
252
+
253
+ end
254
+
218
255
  end
219
256
  end
@@ -1,3 +1,3 @@
1
1
  module FluidFeatures
2
- VERSION = '0.3.4'
2
+ VERSION = '0.4.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluidfeatures
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: