Skip to content

Commit

Permalink
Expose middleware stack for configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
rossta committed Nov 28, 2015
1 parent 864e03f commit 69b5667
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 20 deletions.
32 changes: 13 additions & 19 deletions lib/tacokit/client.rb
Expand Up @@ -48,8 +48,6 @@ class Client
include Tacokit::Client::Webhooks

def_delegators :configuration, *Configuration.keys
def_delegators :configuration, :user_authenticated?, :user_credentials
def_delegators :configuration, :app_authenticated?, :app_credentials
def_delegators :transform, :serialize, :deserialize, :serialize_params

attr_accessor :last_response
Expand Down Expand Up @@ -117,25 +115,21 @@ def transform
@transform ||= Transform.new
end

def connection
@connection ||= Faraday.new(url: api_endpoint) do |http|
http.headers[:user_agent] = "TacoKit 0.0.1"
def connection_options
{
url: api_endpoint,
builder: configuration.stack,
headers: { user_agent: "Tacokit #{Tacokit::VERSION}" }
}
end

if user_authenticated?
http.request :oauth, user_credentials
elsif app_authenticated?
http.params.update app_credentials
def connection
@connection ||= Faraday.new(connection_options) do |http|
if configuration.user_authenticated?
http.request :oauth, configuration.user_credentials
elsif configuration.app_authenticated?
http.params.update configuration.app_credentials
end

http.request :json
http.request :multipart
http.request :url_encoded

http.response :json, content_type: /\bjson$/
http.response :raise_error
http.response :logger if ENV["DEBUG"]

http.adapter Faraday.default_adapter
end
end
end
Expand Down
9 changes: 8 additions & 1 deletion lib/tacokit/configuration.rb
Expand Up @@ -13,7 +13,8 @@ def self.keys
:oauth_token,
:oauth_secret,
:api_endpoint,
:web_endpoint
:web_endpoint,
:stack
]
end

Expand Down Expand Up @@ -43,6 +44,12 @@ def app_credentials
{ key: app_key, token: app_token }.delete_if { |k, v| v.nil? }
end

def stack
@stack ||= Faraday::RackBuilder.new(&Middleware.default_stack(self))
yield @stack if block_given?
@stack
end

private

def defaults
Expand Down
16 changes: 16 additions & 0 deletions lib/tacokit/middleware.rb
Expand Up @@ -3,5 +3,21 @@
module Tacokit
module Middleware
Faraday::Response.register_middleware raise_error: -> { Tacokit::Middleware::RaiseError }

module_function

def default_stack(config = Tacokit.configuration)
proc do |http|
http.request :json
http.request :multipart
http.request :url_encoded

http.response :json, content_type: /\bjson$/
http.response :raise_error
http.response :logger if ENV["DEBUG"]

http.adapter Faraday.default_adapter
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -10,6 +10,7 @@
require "tacokit"
require "rspec"
require "webmock/rspec"
require "pry"

WebMock.disable_net_connect!

Expand Down
24 changes: 24 additions & 0 deletions spec/tacokit/configuration_spec.rb
Expand Up @@ -33,6 +33,30 @@
expect(configuration.app_secret).to eq("app_secret")
end

it "exposes middleware stack" do
handlers = configuration.stack.handlers
expect(handlers.size).to eq(6)
[FaradayMiddleware::EncodeJson,
Faraday::Request::Multipart,
Faraday::Request::UrlEncoded,
FaradayMiddleware::ParseJson,
Tacokit::Middleware::RaiseError,
Faraday::Adapter::NetHttp].each do |middleware|
expect(handlers).to include(middleware), "handlers did not include #{middleware}"
end
end

it "has configurable middleware stack" do
test_middleware = Class.new(Faraday::Middleware)

configuration.stack do |conn|
conn.use test_middleware
end

expect(configuration.stack.handlers.size).to eq(7)
expect(configuration.stack.handlers.last).to eq(test_middleware)
end

describe "#initialize" do
it "sets key attributes provided as a hash" do
configuration = Tacokit::Configuration.new \
Expand Down

0 comments on commit 69b5667

Please sign in to comment.