Parcourir la source

Merge pull request #952 from cantino/gzip_support

Use FaradayMiddleware::Gzip in faraday_middleware 0.10.0
Akinori MUSHA il y a 9 ans
Parent
commit
0e06e8c250
4 fichiers modifiés avec 50 ajouts et 13 suppressions
  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'
 gem 'omniauth-twitter'
 
 # Tumblr Agents
-gem 'tumblr_client'
+gem 'tumblr_client', github: 'knu/tumblr_client', branch: 'patch-1'
 gem 'omniauth-tumblr'
 
 # Dropbox Agents
@@ -64,7 +64,7 @@ gem 'devise', '~> 3.4.0'
 gem 'dotenv-rails', '~> 2.0.1'
 gem 'em-http-request', '~> 1.1.2'
 gem 'faraday', '~> 0.9.0'
-gem 'faraday_middleware'
+gem 'faraday_middleware', '>= 0.10.0'
 gem 'feed-normalizer'
 gem 'font-awesome-sass', '~> 4.3.2'
 gem 'foreman', '~> 0.63.0'

+ 16 - 10
Gemfile.lock

@@ -19,6 +19,19 @@ GIT
       oauth2 (~> 0.9.1)
       rest-client (~> 1.8)
 
+GIT
+  remote: git://github.com/knu/tumblr_client.git
+  revision: d6f1f64a7cba381345c588e28ebcff28048c3a6c
+  branch: patch-1
+  specs:
+    tumblr_client (0.8.5)
+      faraday (~> 0.9.0)
+      faraday_middleware (~> 0.9)
+      json
+      mime-types
+      oauth
+      simple_oauth
+
 GIT
   remote: git://github.com/wunderlist/omniauth-wunderlist.git
   revision: d0910d0396107b9302aa1bc50e74bb140990ccb8
@@ -170,7 +183,7 @@ GEM
     extlib (0.9.16)
     faraday (0.9.1)
       multipart-post (>= 1.2, < 3)
-    faraday_middleware (0.9.1)
+    faraday_middleware (0.10.0)
       faraday (>= 0.7.4, < 0.10)
     feed-normalizer (1.5.2)
       hpricot (>= 0.6)
@@ -464,13 +477,6 @@ GEM
     tins (1.3.2)
     treetop (1.5.3)
       polyglot (~> 0.3)
-    tumblr_client (0.8.4)
-      faraday (~> 0.9.0)
-      faraday_middleware (~> 0.9.0)
-      json
-      mime-types
-      oauth
-      simple_oauth
     twilio-ruby (3.11.6)
       builder (>= 2.1.2)
       jwt (>= 0.1.2)
@@ -538,7 +544,7 @@ DEPENDENCIES
   dropbox-api
   em-http-request (~> 1.1.2)
   faraday (~> 0.9.0)
-  faraday_middleware
+  faraday_middleware (>= 0.10.0)
   feed-normalizer
   ffi (>= 1.9.4)
   font-awesome-sass (~> 4.3.2)
@@ -595,7 +601,7 @@ DEPENDENCIES
   spectrum-rails
   string-scrub
   therubyracer (~> 0.12.2)
-  tumblr_client
+  tumblr_client!
   twilio-ruby (~> 3.11.5)
   twitter (~> 5.14.0)
   twitter-stream!

+ 2 - 0
app/concerns/web_request_concern.rb

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

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

@@ -170,6 +170,35 @@ describe Agents::WebsiteAgent do
     end
 
     describe 'unzipping' do
+      it 'should unzip automatically if the response has Content-Encoding: gzip' do
+        json = {
+          'response' => {
+            'version' => 2,
+            'title' => "hello!"
+          }
+        }
+        zipped = ActiveSupport::Gzip.compress(json.to_json)
+        stub_request(:any, /gzip/).to_return(body: zipped, headers: { 'Content-Encoding' => 'gzip' }, status: 200)
+        site = {
+          'name' => "Some JSON Response",
+          'expected_update_period_in_days' => "2",
+          'type' => "json",
+          'url' => "http://gzip.com",
+          'mode' => 'on_change',
+          'extract' => {
+            'version' => { 'path' => 'response.version' },
+          },
+          # no unzip option
+        }
+        checker = Agents::WebsiteAgent.new(:name => "Weather Site", :options => site)
+        checker.user = users(:bob)
+        checker.save!
+
+        checker.check
+        event = Event.last
+        expect(event.payload['version']).to eq(2)
+      end
+
       it 'should unzip with unzip option' do
         json = {
           'response' => {
@@ -178,7 +207,7 @@ describe Agents::WebsiteAgent do
           }
         }
         zipped = ActiveSupport::Gzip.compress(json.to_json)
-        stub_request(:any, /gzip/).to_return(:body => zipped, :status => 200)
+        stub_request(:any, /gzip/).to_return(body: zipped, status: 200)
         site = {
           'name' => "Some JSON Response",
           'expected_update_period_in_days' => "2",