Merge pull request #952 from cantino/gzip_support

Use FaradayMiddleware::Gzip in faraday_middleware 0.10.0

Akinori MUSHA 9 年之前
父節點
當前提交
0e06e8c250
共有 4 個文件被更改,包括 50 次插入13 次删除
  1. 2 2
      Gemfile
  2. 16 10
      Gemfile.lock
  3. 2 0
      app/concerns/web_request_concern.rb
  4. 30 1
      spec/models/agents/website_agent_spec.rb

+ 2 - 2
Gemfile

@@ -29,7 +29,7 @@ gem 'twitter-stream', github: 'cantino/twitter-stream', branch: 'huginn'
29 29
 gem 'omniauth-twitter'
30 30
 
31 31
 # Tumblr Agents
32
-gem 'tumblr_client'
32
+gem 'tumblr_client', github: 'knu/tumblr_client', branch: 'patch-1'
33 33
 gem 'omniauth-tumblr'
34 34
 
35 35
 # Dropbox Agents
@@ -64,7 +64,7 @@ gem 'devise', '~> 3.4.0'
64 64
 gem 'dotenv-rails', '~> 2.0.1'
65 65
 gem 'em-http-request', '~> 1.1.2'
66 66
 gem 'faraday', '~> 0.9.0'
67
-gem 'faraday_middleware'
67
+gem 'faraday_middleware', '>= 0.10.0'
68 68
 gem 'feed-normalizer'
69 69
 gem 'font-awesome-sass', '~> 4.3.2'
70 70
 gem 'foreman', '~> 0.63.0'

+ 16 - 10
Gemfile.lock

@@ -20,6 +20,19 @@ GIT
20 20
       rest-client (~> 1.8)
21 21
 
22 22
 GIT
23
+  remote: git://github.com/knu/tumblr_client.git
24
+  revision: d6f1f64a7cba381345c588e28ebcff28048c3a6c
25
+  branch: patch-1
26
+  specs:
27
+    tumblr_client (0.8.5)
28
+      faraday (~> 0.9.0)
29
+      faraday_middleware (~> 0.9)
30
+      json
31
+      mime-types
32
+      oauth
33
+      simple_oauth
34
+
35
+GIT
23 36
   remote: git://github.com/wunderlist/omniauth-wunderlist.git
24 37
   revision: d0910d0396107b9302aa1bc50e74bb140990ccb8
25 38
   ref: d0910d0396107b9302aa1bc50e74bb140990ccb8
@@ -170,7 +183,7 @@ GEM
170 183
     extlib (0.9.16)
171 184
     faraday (0.9.1)
172 185
       multipart-post (>= 1.2, < 3)
173
-    faraday_middleware (0.9.1)
186
+    faraday_middleware (0.10.0)
174 187
       faraday (>= 0.7.4, < 0.10)
175 188
     feed-normalizer (1.5.2)
176 189
       hpricot (>= 0.6)
@@ -464,13 +477,6 @@ GEM
464 477
     tins (1.3.2)
465 478
     treetop (1.5.3)
466 479
       polyglot (~> 0.3)
467
-    tumblr_client (0.8.4)
468
-      faraday (~> 0.9.0)
469
-      faraday_middleware (~> 0.9.0)
470
-      json
471
-      mime-types
472
-      oauth
473
-      simple_oauth
474 480
     twilio-ruby (3.11.6)
475 481
       builder (>= 2.1.2)
476 482
       jwt (>= 0.1.2)
@@ -538,7 +544,7 @@ DEPENDENCIES
538 544
   dropbox-api
539 545
   em-http-request (~> 1.1.2)
540 546
   faraday (~> 0.9.0)
541
-  faraday_middleware
547
+  faraday_middleware (>= 0.10.0)
542 548
   feed-normalizer
543 549
   ffi (>= 1.9.4)
544 550
   font-awesome-sass (~> 4.3.2)
@@ -595,7 +601,7 @@ DEPENDENCIES
595 601
   spectrum-rails
596 602
   string-scrub
597 603
   therubyracer (~> 0.12.2)
598
-  tumblr_client
604
+  tumblr_client!
599 605
   twilio-ruby (~> 3.11.5)
600 606
   twitter (~> 5.14.0)
601 607
   twitter-stream!

+ 2 - 0
app/concerns/web_request_concern.rb

@@ -121,6 +121,8 @@ module WebRequestConcern
121 121
         builder.request :basic_auth, *userinfo
122 122
       end
123 123
 
124
+      builder.use FaradayMiddleware::Gzip
125
+
124 126
       case backend = faraday_backend
125 127
         when :typhoeus
126 128
           require 'typhoeus/adapters/faraday'

+ 30 - 1
spec/models/agents/website_agent_spec.rb

@@ -170,6 +170,35 @@ describe Agents::WebsiteAgent do
170 170
     end
171 171
 
172 172
     describe 'unzipping' do
173
+      it 'should unzip automatically if the response has Content-Encoding: gzip' do
174
+        json = {
175
+          'response' => {
176
+            'version' => 2,
177
+            'title' => "hello!"
178
+          }
179
+        }
180
+        zipped = ActiveSupport::Gzip.compress(json.to_json)
181
+        stub_request(:any, /gzip/).to_return(body: zipped, headers: { 'Content-Encoding' => 'gzip' }, status: 200)
182
+        site = {
183
+          'name' => "Some JSON Response",
184
+          'expected_update_period_in_days' => "2",
185
+          'type' => "json",
186
+          'url' => "http://gzip.com",
187
+          'mode' => 'on_change',
188
+          'extract' => {
189
+            'version' => { 'path' => 'response.version' },
190
+          },
191
+          # no unzip option
192
+        }
193
+        checker = Agents::WebsiteAgent.new(:name => "Weather Site", :options => site)
194
+        checker.user = users(:bob)
195
+        checker.save!
196
+
197
+        checker.check
198
+        event = Event.last
199
+        expect(event.payload['version']).to eq(2)
200
+      end
201
+
173 202
       it 'should unzip with unzip option' do
174 203
         json = {
175 204
           'response' => {
@@ -178,7 +207,7 @@ describe Agents::WebsiteAgent do
178 207
           }
179 208
         }
180 209
         zipped = ActiveSupport::Gzip.compress(json.to_json)
181
-        stub_request(:any, /gzip/).to_return(:body => zipped, :status => 200)
210
+        stub_request(:any, /gzip/).to_return(body: zipped, status: 200)
182 211
         site = {
183 212
           'name' => "Some JSON Response",
184 213
           'expected_update_period_in_days' => "2",