Created
May 18, 2012 17:19
-
-
Save sfaxon/2726527 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'faraday' | |
require 'debugger' | |
# require File.join(File.dirname(__FILE__), 'lib/active_resource') | |
module ActiveResource | |
module Request | |
class PerRequestToken < Faraday::Middleware | |
def initialize(app = nil, options = {}) | |
@per_options = options | |
super(app) | |
end | |
def call(env) | |
if @per_options[:request_auth_token] | |
env[:request_headers].merge!('X-AUTH-TOKEN' => @per_options[:request_auth_token]) | |
end | |
@app.call(env) | |
end | |
end | |
end | |
end | |
class Scope | |
attr_reader :stack | |
def initialize | |
@stack ||= {} | |
end | |
def add(middleware, args) | |
@stack[middleware] = args | |
end | |
end | |
# base connection method | |
def connection | |
Faraday.new("http://localhost") do |builder| | |
yield builder if block_given? | |
builder.adapter :test do |stub| | |
stub.get('/something') {[ 200, {}, 'yay' ]} | |
end | |
end | |
end | |
@scope = Scope.new() | |
@scope.add(ActiveResource::Request::PerRequestToken, :request_auth_token => 'sekret') | |
x = connection { |builder| | |
@scope.stack.each do |scope, params| | |
builder.use scope, params | |
end | |
}.get('/something') | |
puts x.env[:request_headers] # {"X-AUTH-TOKEN"=>"sekret"} | |
@scope = Scope.new | |
y = connection { |builder| | |
@scope.stack.each do |scope, params| | |
builder.use scope, params | |
end | |
}.get('/something') | |
puts y.env[:request_headers] # {} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment