Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update Faraday

  • Loading branch information...
commit 7af443c111d644ba9cdf180e8d8e5582dc3099c3 1 parent dad747d
@jherdman jherdman authored
View
7 lib/zeppelin.rb
@@ -1,4 +1,5 @@
require 'faraday'
+require 'faraday_middleware'
require 'time'
# A very tiny Urban Airship Push Notification API client.
@@ -302,13 +303,11 @@ def remove_tag_from_device(device_token, tag_name)
private
def initialize_connection
- Faraday::Request::JSON.adapter = MultiJson
-
conn = Faraday::Connection.new(BASE_URI, @options) do |builder|
builder.request :json
- builder.use Zeppelin::Middleware::JsonParser
- builder.use Zeppelin::Middleware::ResponseRaiseError
+ builder.response :json, :content_type => /\bjson$/
+ builder.response :zeppelin_raise_error
builder.adapter :net_http
end
View
3  lib/zeppelin/middleware.rb
@@ -3,5 +3,6 @@ module Middlware
end
end
-require 'zeppelin/middleware/json_parser'
require 'zeppelin/middleware/response_raise_error'
+
+Faraday.register_middleware :response, zeppelin_raise_error: Zeppelin::Middleware::ResponseRaiseError
View
37 lib/zeppelin/middleware/json_parser.rb
@@ -1,37 +0,0 @@
-require 'multi_json'
-
-class Zeppelin
- module Middleware
- # Middleware for Faraday that parses JSON response bodies. Based on code in
- # the FaradayMiddleware project.
- #
- # @private
- class JsonParser < Faraday::Middleware
- CONTENT_TYPE = 'Content-Type'
-
- def initialize(app=nil)
- @app = app
- end
-
- def call(env)
- @app.call(env).on_complete do
- parse_response(env) if process_content_type?(env) && parse_response?(env)
- end
- end
-
- private
-
- def parse_response(env)
- env[:body] = MultiJson.decode(env[:body])
- end
-
- def process_content_type?(env)
- env[:response_headers][CONTENT_TYPE].to_s =~ /application\/(.*)json/
- end
-
- def parse_response?(env)
- env[:body].respond_to? :to_str
- end
- end
- end
-end
View
34 spec/middleware/json_parser_spec.rb
@@ -1,34 +0,0 @@
-require 'spec_helper'
-
-describe Zeppelin::Middleware::JsonParser do
- let(:json_body) { "{\"foo\":\"bar\"}" }
-
- let(:expected_parsed_body) { { 'foo' => 'bar' } }
-
- it 'parses a standard JSON content type' do
- process(json_body, 'application/json').body.should eq(expected_parsed_body)
- end
-
- it 'parses vendor JSON content type' do
- process(json_body, 'application/vnd.urbanairship+json').body.should eq(expected_parsed_body)
- end
-
- it 'does not change nil body' do
- process(nil).body.should be_nil
- end
-
- it 'does not parse non-JSON content types' do
- process('<hello>world</hello>', 'text/xml').body.should eq('<hello>world</hello>')
- end
-
- def process(body, content_type=nil, options={})
- env = { :body => body, :response_headers => Faraday::Utils::Headers.new }
- env[:response_headers]['content-type'] = content_type if content_type
-
- middleware = Zeppelin::Middleware::JsonParser.new(
- lambda { |env| Faraday::Response.new(env) }
- )
-
- middleware.call(env)
- end
-end
View
38 spec/zeppelin_spec.rb
@@ -28,9 +28,9 @@
it { subject.connection.builder.handlers.should include(Faraday::Adapter::NetHttp) }
- it { subject.connection.builder.handlers.should include(Faraday::Request::JSON) }
+ it { subject.connection.builder.handlers.should include(FaradayMiddleware::EncodeJson) }
- it { subject.connection.builder.handlers.should include(Zeppelin::Middleware::JsonParser) }
+ it { subject.connection.builder.handlers.should include(FaradayMiddleware::ParseJson) }
it { subject.connection.builder.handlers.should include(Zeppelin::Middleware::ResponseRaiseError) }
@@ -76,7 +76,7 @@
it 'gets information about a device' do
stub_requests do |stub|
- stub.get(uri) { [200, { 'Content-Type' => 'application/json' }, MultiJson.encode(response_body)] }
+ stub.get(uri) { [200, { 'Content-Type' => 'application/json' }, JSON.dump(response_body)] }
end
subject.device_token(device_token).should eq(response_body)
@@ -139,7 +139,7 @@
it 'requests a page of device tokens' do
stub_requests do |stub|
- stub.get('/api/device_tokens/?page=') { [200, { 'Content-Type' => 'application/json' }, MultiJson.encode(results_without_next_page)] }
+ stub.get('/api/device_tokens/?page=') { [200, { 'Content-Type' => 'application/json' }, JSON.dump(results_without_next_page)] }
end
subject.device_tokens.should eq(results_without_next_page)
@@ -147,7 +147,7 @@
it 'includes the page number of the next page' do
stub_requests do |stub|
- stub.get('/api/device_tokens/?page=') { [200, { 'Content-Type' => 'application/json' }, MultiJson.encode(results_with_next_page)] }
+ stub.get('/api/device_tokens/?page=') { [200, { 'Content-Type' => 'application/json' }, JSON.dump(results_with_next_page)] }
end
subject.device_tokens['next_page'].should eq(2)
@@ -155,7 +155,7 @@
it 'does not include the page number if there are no additional pages' do
stub_requests do |stub|
- stub.get('/api/device_tokens/?page=') { [200, { 'Content-Type' => 'application/json' }, MultiJson.encode(results_without_next_page)] }
+ stub.get('/api/device_tokens/?page=') { [200, { 'Content-Type' => 'application/json' }, JSON.dump(results_without_next_page)] }
end
subject.device_tokens.should_not have_key('next_page')
@@ -163,7 +163,7 @@
it 'requests a specified page of device_tokens' do
stub_requests do |stub|
- stub.get('/api/device_tokens/?page=4') { [200, { 'Content-Type' => 'application/json' }, MultiJson.encode(results_without_next_page)] }
+ stub.get('/api/device_tokens/?page=4') { [200, { 'Content-Type' => 'application/json' }, JSON.dump(results_without_next_page)] }
end
subject.device_tokens(4)
@@ -195,7 +195,7 @@
it 'accepts a payload' do
stub_requests do |stub|
- stub.put(uri, MultiJson.encode(payload)) { [200, {}, ''] }
+ stub.put(uri, JSON.dump(payload)) { [200, {}, ''] }
end
subject.register_apid(device_token, payload).should be_true
@@ -219,7 +219,7 @@
it 'responds with information about a device when request is successful' do
stub_requests do |stub|
- stub.get(uri) { [200, { 'Content-Type' => 'application/json' }, MultiJson.encode(response_body)] }
+ stub.get(uri) { [200, { 'Content-Type' => 'application/json' }, JSON.dump(response_body)] }
end
subject.apid(device_token).should eq(response_body)
@@ -278,7 +278,7 @@
it 'requests a page of APIDs' do
stub_requests do |stub|
- stub.get('/api/apids/?page=') { [200, { 'Content-Type' => 'application/json' }, MultiJson.encode(results_without_next_page)] }
+ stub.get('/api/apids/?page=') { [200, { 'Content-Type' => 'application/json' }, JSON.dump(results_without_next_page)] }
end
subject.apids.should eq(results_without_next_page)
@@ -286,7 +286,7 @@
it 'includes the page number of the next page' do
stub_requests do |stub|
- stub.get('/api/apids/?page=') { [200, { 'Content-Type' => 'application/json' }, MultiJson.encode(results_with_next_page)] }
+ stub.get('/api/apids/?page=') { [200, { 'Content-Type' => 'application/json' }, JSON.dump(results_with_next_page)] }
end
subject.apids['next_page'].should eq(2)
@@ -294,7 +294,7 @@
it 'does not include the page number if there are no additional pages' do
stub_requests do |stub|
- stub.get('/api/apids/?page=') { [200, { 'Content-Type' => 'application/json' }, MultiJson.encode(results_without_next_page)] }
+ stub.get('/api/apids/?page=') { [200, { 'Content-Type' => 'application/json' }, JSON.dump(results_without_next_page)] }
end
subject.apids.should_not have_key('next_page')
@@ -302,7 +302,7 @@
it 'requests a specified page of APIDs' do
stub_requests do |stub|
- stub.get('/api/apids/?page=4') { [200, { 'Content-Type' => 'application/json' }, MultiJson.encode(results_without_next_page)] }
+ stub.get('/api/apids/?page=4') { [200, { 'Content-Type' => 'application/json' }, JSON.dump(results_without_next_page)] }
end
subject.apids(4)
@@ -326,7 +326,7 @@
it 'is true when the request is successful' do
stub_requests do |stub|
- stub.post(uri, MultiJson.encode(payload)) { [200, {}, ''] }
+ stub.post(uri, JSON.dump(payload)) { [200, {}, ''] }
end
subject.push(payload).should be_true
@@ -364,7 +364,7 @@
it 'is true when the request was successful' do
stub_requests do |stub|
- stub.post(uri, MultiJson.encode(payload)) { [200, {}, ''] }
+ stub.post(uri, JSON.dump(payload)) { [200, {}, ''] }
end
subject.batch_push(message1, message2).should be_true
@@ -388,7 +388,7 @@
it 'is true when the request is successful' do
stub_requests do |stub|
- stub.post(uri, MultiJson.encode(payload)) { [200, {}, ''] }
+ stub.post(uri, JSON.dump(payload)) { [200, {}, ''] }
end
subject.broadcast(payload).should be_true
@@ -414,7 +414,7 @@
it 'is the response body for a successful request' do
stub_requests do |stub|
- stub.get(uri) { [200, { 'Content-Type' => 'application/json' }, MultiJson.encode(response_body)] }
+ stub.get(uri) { [200, { 'Content-Type' => 'application/json' }, JSON.dump(response_body)] }
end
subject.feedback(since)
@@ -486,7 +486,7 @@
it 'is the collection of tags on a device when request is successful' do
stub_requests do |stub|
- stub.get(uri) { [200, { 'Content-Type' => 'application/json' }, MultiJson.encode(response_body)] }
+ stub.get(uri) { [200, { 'Content-Type' => 'application/json' }, JSON.dump(response_body)] }
end
subject.device_tags(device_token).should eq(response_body)
@@ -558,7 +558,7 @@
it 'is true when request is successful' do
stub_requests do |stub|
- stub.get(uri) { [200, { 'Content-Type' => 'application/json' }, MultiJson.encode(response_body)] }
+ stub.get(uri) { [200, { 'Content-Type' => 'application/json' }, JSON.dump(response_body)] }
end
subject.tags
View
3  zeppelin.gemspec
@@ -15,10 +15,11 @@ Gem::Specification.new do |s|
s.rubyforge_project = 'zeppelin'
s.add_dependency 'faraday'
- s.add_dependency 'multi_json', '~> 1.1'
+ s.add_dependency 'faraday_middleware'
s.add_development_dependency 'rspec'
s.add_development_dependency 'rake'
+ s.add_development_dependency 'json'
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
Please sign in to comment.
Something went wrong with that request. Please try again.