Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Faraday 0.9 compatibility

* Removes all faraday_middleware dependencies
* Updates multipart-post for #61
  • Loading branch information...
commit fff64607c344a3e68153f596c3104b9317acebc7 1 parent 817bde0
@steved steved authored
View
1  Gemfile
@@ -5,7 +5,6 @@ source 'https://rubygems.org'
gem "simplecov", :platforms => :ruby_19, :group => :test
gem "jruby-openssl", :platforms => :jruby
-gem "multipart-post", :git => "https://github.com/steved555/multipart-post.git"
group :server do
gem "thin"
View
127 Gemfile.lock
@@ -1,126 +1,114 @@
-GIT
- remote: https://github.com/steved555/multipart-post.git
- revision: 57c66998c05143a684c13694937c8adfd335cae1
- specs:
- multipart-post (1.1.5)
-
PATH
remote: .
specs:
zendesk_api (1.2.5)
- faraday (~> 0.8.0)
- faraday_middleware (~> 0.8.7)
+ faraday (~> 0.9)
hashie (>= 1.2)
inflection
- mime-types (~> 1.0)
+ mime-types (~> 2.0)
multi_json
- multipart-post
+ multipart-post (~> 2.0)
GEM
remote: https://rubygems.org/
specs:
- activemodel (3.2.13)
- activesupport (= 3.2.13)
+ activemodel (3.2.16)
+ activesupport (= 3.2.16)
builder (~> 3.0.0)
- activesupport (3.2.13)
- i18n (= 0.6.1)
+ activesupport (3.2.16)
+ i18n (~> 0.6, >= 0.6.4)
multi_json (~> 1.0)
- addressable (2.3.4)
- backports (3.3.2)
+ addressable (2.3.5)
+ backports (3.5.0)
bond (0.4.3)
- bond (0.4.3-java)
- bootstrap-sass (2.3.2.0)
+ bootstrap-sass (3.1.0.2)
sass (~> 3.2)
- bouncy-castle-java (1.5.0147)
builder (3.0.4)
- bump (0.4.2)
- chunky_png (1.2.8)
- coderay (1.0.9)
+ bump (0.5.0)
+ chunky_png (1.3.0)
+ coderay (1.1.0)
coderay_bash (1.0.5)
coderay (>= 1.0)
compass (0.12.2)
chunky_png (~> 1.2)
fssm (>= 0.2.7)
sass (~> 3.1)
- crack (0.4.0)
- safe_yaml (~> 0.9.0)
+ crack (0.4.2)
+ safe_yaml (~> 1.0.0)
daemons (1.1.9)
- database_cleaner (1.0.1)
- diff-lcs (1.2.4)
+ database_cleaner (1.2.0)
+ diff-lcs (1.2.5)
+ docile (1.1.3)
eventmachine (1.0.3)
- eventmachine (1.0.3-java)
- faraday (0.8.7)
- multipart-post (~> 1.1)
- faraday_middleware (0.8.8)
- faraday (>= 0.7.4, < 0.9)
+ faraday (0.9.0)
+ multipart-post (>= 1.2, < 3)
fssm (0.2.10)
- haml (4.0.3)
+ haml (4.0.5)
tilt
hashie (2.0.5)
- i18n (0.6.1)
+ i18n (0.6.9)
inflection (1.0.0)
- jruby-openssl (0.8.8)
- bouncy-castle-java (>= 1.5.0147)
- json (1.8.0)
- mime-types (1.25.1)
- mongoid (3.1.4)
+ json (1.8.1)
+ mime-types (2.1)
+ mongoid (3.1.6)
activemodel (~> 3.2)
moped (~> 1.4)
origin (~> 1.0)
- tzinfo (~> 0.3.22)
- moped (1.5.0)
- multi_json (1.7.7)
- newrelic_rpm (3.6.4.122)
+ tzinfo (~> 0.3.29)
+ moped (1.5.2)
+ multi_json (1.8.4)
+ multipart-post (2.0.0)
+ newrelic_rpm (3.7.2.192)
origin (1.1.0)
rack (1.5.2)
- rack-protection (1.5.0)
+ rack-protection (1.5.2)
rack
- rack-ssl-enforcer (0.2.5)
+ rack-ssl-enforcer (0.2.6)
rack-test (0.6.2)
rack (>= 1.0)
- rake (10.1.0)
- redcarpet (2.3.0)
+ rake (10.1.1)
+ redcarpet (3.0.0)
ripl (0.7.0)
bond (~> 0.4.2)
- rspec (2.13.0)
- rspec-core (~> 2.13.0)
- rspec-expectations (~> 2.13.0)
- rspec-mocks (~> 2.13.0)
- rspec-core (2.13.1)
- rspec-expectations (2.13.0)
+ rspec (2.14.1)
+ rspec-core (~> 2.14.0)
+ rspec-expectations (~> 2.14.0)
+ rspec-mocks (~> 2.14.0)
+ rspec-core (2.14.7)
+ rspec-expectations (2.14.5)
diff-lcs (>= 1.1.3, < 2.0)
- rspec-mocks (2.13.1)
- safe_yaml (0.9.3)
- sass (3.2.9)
- simplecov (0.7.1)
- multi_json (~> 1.0)
- simplecov-html (~> 0.7.1)
- simplecov-html (0.7.1)
- sinatra (1.4.3)
+ rspec-mocks (2.14.5)
+ safe_yaml (1.0.1)
+ sass (3.2.14)
+ simplecov (0.8.2)
+ docile (~> 1.1.0)
+ multi_json
+ simplecov-html (~> 0.8.0)
+ simplecov-html (0.8.0)
+ sinatra (1.4.4)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
- sinatra-contrib (1.4.0)
+ sinatra-contrib (1.4.2)
backports (>= 2.0)
- eventmachine
+ multi_json
rack-protection
rack-test
- sinatra (~> 1.4.2)
+ sinatra (~> 1.4.0)
tilt (~> 1.3)
- thin (1.5.1)
+ thin (1.6.1)
daemons (>= 1.0.9)
- eventmachine (>= 0.12.6)
+ eventmachine (>= 1.0.0)
rack (>= 1.0.0)
tilt (1.4.1)
- tzinfo (0.3.37)
- vcr (2.5.0)
+ tzinfo (0.3.38)
+ vcr (2.8.0)
webmock (1.9.3)
addressable (>= 2.2.7)
crack (>= 0.3.2)
- yard (0.8.6.1)
+ yard (0.8.7.3)
PLATFORMS
- java
ruby
DEPENDENCIES
@@ -134,7 +122,6 @@ DEPENDENCIES
jruby-openssl
json
mongoid
- multipart-post!
newrelic_rpm
rack-ssl-enforcer
rake
View
7 lib/zendesk_api/client.rb
@@ -1,5 +1,4 @@
require 'faraday'
-require 'faraday_middleware'
require 'zendesk_api/version'
require 'zendesk_api/sideloading'
@@ -9,10 +8,12 @@
require 'zendesk_api/middleware/request/etag_cache'
require 'zendesk_api/middleware/request/retry'
require 'zendesk_api/middleware/request/upload'
+require 'zendesk_api/middleware/request/encode_json'
require 'zendesk_api/middleware/response/callback'
require 'zendesk_api/middleware/response/deflate'
require 'zendesk_api/middleware/response/gzip'
require 'zendesk_api/middleware/response/parse_iso_dates'
+require 'zendesk_api/middleware/response/parse_json'
require 'zendesk_api/middleware/response/raise_error'
require 'zendesk_api/middleware/response/logger'
require 'zendesk_api/delegator'
@@ -134,7 +135,7 @@ def build_connection
builder.use ZendeskAPI::Middleware::Response::Callback, self
builder.use ZendeskAPI::Middleware::Response::Logger, config.logger if config.logger
builder.use ZendeskAPI::Middleware::Response::ParseIsoDates
- builder.response :json, :content_type => 'application/json'
+ builder.use ZendeskAPI::Middleware::Response::ParseJson
adapter = config.adapter || Faraday.default_adapter
@@ -156,7 +157,7 @@ def build_connection
builder.use ZendeskAPI::Middleware::Request::Upload
builder.request :multipart
- builder.request :json
+ builder.use ZendeskAPI::Middleware::Request::EncodeJson
builder.use ZendeskAPI::Middleware::Request::Retry, :logger => config.logger if config.retry # Should always be first in the stack
builder.adapter *adapter
View
26 lib/zendesk_api/middleware/request/encode_json.rb
@@ -0,0 +1,26 @@
+module ZendeskAPI
+ # @private
+ module Middleware
+ # @private
+ module Request
+ class EncodeJson < Faraday::Middleware
+ CONTENT_TYPE = 'Content-Type'.freeze
+ MIME_TYPE = 'application/json'.freeze
+ dependency 'json'
+
+ def call(env)
+ type = env[:request_headers][CONTENT_TYPE].to_s
+ type = type.split(';', 2).first if type.index(';')
+ type
+
+ if env[:body] && !(env[:body].respond_to?(:to_str) && env[:body].empty?) && (type.empty? || type == MIME_TYPE)
+ env[:body] = JSON.dump(env[:body])
+ env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
+ end
+
+ @app.call(env)
+ end
+ end
+ end
+ end
+end
View
12 lib/zendesk_api/middleware/request/etag_cache.rb
@@ -18,17 +18,17 @@ def cache_key(env)
[@cache_key_prefix, env[:url].to_s]
end
- def call(env)
- return @app.call(env) unless [:get, :head].include?(env[:method])
+ def call(environment)
+ return @app.call(environment) unless [:get, :head].include?(environment[:method])
# send known etag
- cached = @cache.read(cache_key(env))
+ cached = @cache.read(cache_key(environment))
if cached
- env[:request_headers]["If-None-Match"] ||= cached[:response_headers]["Etag"]
+ environment[:request_headers]["If-None-Match"] ||= cached[:response_headers]["Etag"]
end
- @app.call(env).on_complete do
+ @app.call(environment).on_complete do |env|
if cached && env[:status] == 304 # not modified
env[:body] = cached[:body]
env[:response_headers].merge!(
@@ -38,7 +38,7 @@ def call(env)
:content_encoding => cached[:response_headers][:content_encoding]
)
elsif env[:status] == 200 && env[:response_headers]["Etag"] # modified and cacheable
- @cache.write(cache_key(env), env)
+ @cache.write(cache_key(env), env.to_h)
end
end
end
View
16 lib/zendesk_api/middleware/response/deflate.rb
@@ -1,17 +1,15 @@
-require 'faraday_middleware/response_middleware'
-
module ZendeskAPI
+ # @private
module Middleware
+ # @private
module Response
# Faraday middleware to handle content-encoding = inflate
# @private
- class Deflate < FaradayMiddleware::ResponseMiddleware
- define_parser do |body|
- Zlib::Inflate.inflate(body)
- end
-
- def parse_response?(env)
- super && env[:response_headers]['content-encoding'] == "deflate"
+ class Deflate < Faraday::Response::Middleware
+ def on_complete(env)
+ if env.body.respond_to?(:to_str) && !env.body.strip.empty? && env[:response_headers]['content-encoding'] == "deflate"
+ env.body = Zlib::Inflate.inflate(env.body)
+ end
end
end
end
View
13 lib/zendesk_api/middleware/response/gzip.rb
@@ -1,4 +1,3 @@
-require 'faraday_middleware/response_middleware'
require 'zlib'
require 'stringio'
@@ -8,13 +7,11 @@ module Middleware
# @private
module Response
# Faraday middleware to handle content-encoding = gzip
- class Gzip < FaradayMiddleware::ResponseMiddleware
- define_parser do |body|
- Zlib::GzipReader.new(StringIO.new(body)).read
- end
-
- def parse_response?(env)
- super && env[:response_headers]['content-encoding'] == "gzip"
+ class Gzip < Faraday::Response::Middleware
+ def on_complete(env)
+ if env[:body].respond_to?(:to_str) && !env[:body].strip.empty? && env[:response_headers]['content-encoding'] == "gzip"
+ env[:body] = Zlib::GzipReader.new(StringIO.new(env[:body])).read
+ end
end
end
end
View
4 lib/zendesk_api/middleware/response/logger.rb
@@ -1,11 +1,9 @@
-require 'faraday_middleware/response_middleware'
-
module ZendeskAPI
module Middleware
module Response
# Faraday middleware to handle logging
# @private
- class Logger < Faraday::Response::Middleware
+ class Logger < Faraday::Middleware
def initialize(app, logger = nil)
super(app)
View
24 lib/zendesk_api/middleware/response/parse_json.rb
@@ -0,0 +1,24 @@
+module ZendeskAPI
+ # @private
+ module Middleware
+ # @private
+ module Response
+ class ParseJson < Faraday::Response::Middleware
+ CONTENT_TYPE = 'Content-Type'.freeze
+ dependency 'json'
+
+ def on_complete(env)
+ type = env[:response_headers][CONTENT_TYPE].to_s
+ type = type.split(';', 2).first if type.index(';')
+ type
+
+ if type == 'application/json' && env[:body].respond_to?(:to_str) && !env[:body].strip.empty?
+ env[:body] = JSON.parse(env[:body])
+ else
+ env[:body] = nil
+ end
+ end
+ end
+ end
+ end
+end
View
8 spec/core/middleware/response/deflate_spec.rb
@@ -5,7 +5,13 @@
subject { '{ "TESTDATA": true }' }
before(:each) do
- stub_request(:get, %r{blergh}).to_return(:headers => { :content_encoding => "deflate" }, :body => Zlib::Deflate.deflate(subject))
+ stub_request(:get, %r{blergh}).to_return(
+ :headers => {
+ :content_encoding => "deflate",
+ :content_type => "application/json"
+ },
+ :body => Zlib::Deflate.deflate(subject)
+ )
end
it "should inflate returned body" do
View
8 spec/core/middleware/response/gzip_spec.rb
@@ -9,7 +9,13 @@
gz.write(subject)
gz.close
- stub_request(:get, %r{blergh}).to_return(:headers => { :content_encoding => "gzip" }, :body => encoded_data.string)
+ stub_request(:get, %r{blergh}).to_return(
+ :headers => {
+ :content_encoding => "gzip",
+ :content_type => "application/json"
+ },
+ :body => encoded_data.string
+ )
end
it "should inflate returned body" do
View
7 zendesk_api.gemspec
@@ -28,13 +28,12 @@ Gem::Specification.new do |s|
# Optional. Only used for uploads testing.
# s.add_development_dependency "actionpack"
- s.add_runtime_dependency "faraday", "~> 0.8.0"
- s.add_runtime_dependency "faraday_middleware", "~> 0.8.7"
+ s.add_runtime_dependency "faraday", "~> 0.9"
s.add_runtime_dependency "hashie", ">= 1.2"
s.add_runtime_dependency "inflection"
s.add_runtime_dependency "multi_json"
- s.add_runtime_dependency "mime-types", "~> 1.0"
- s.add_runtime_dependency "multipart-post"
+ s.add_runtime_dependency "mime-types", "~> 2.0"
+ s.add_runtime_dependency "multipart-post", "~> 2.0"
s.files = `git ls-files -x Gemfile.lock`.split("\n") rescue ''
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.