Skip to content

Commit

Permalink
Add Client#conn for making requests on behalf of client using basic auth
Browse files Browse the repository at this point in the history
  • Loading branch information
sausman committed Jan 7, 2016
1 parent 0f22509 commit b7d4352
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
15 changes: 4 additions & 11 deletions lib/dwolla/auth.rb
Expand Up @@ -42,20 +42,13 @@ def query
end

def self.request_token client, params
res = Faraday.new(&client.faraday).post do |req|
req.headers["Authorization"] = basic_auth(client)
req.url client.token_url
req.body = URI.encode_www_form(params)
end
res = client.conn.post client.token_url, params
res_body = Util.parse_json res.body
if res.status == 200
Token.new client, Util.parse_json(res.body)
Token.new client, res_body
else
Error.raise! Util.parse_json(res.body)
Error.raise! res_body
end
end

def self.basic_auth client
"Basic #{Base64.encode64("#{client.id}:#{client.secret}")}"
end
end
end
14 changes: 10 additions & 4 deletions lib/dwolla/client.rb
Expand Up @@ -21,7 +21,7 @@ def initialize opts
@id = opts[:id]
@secret = opts[:secret]
yield self if block_given?
# conn
conn
freeze
end

Expand All @@ -44,9 +44,15 @@ def faraday &block
@faraday
end

# def conn &block
# @conn ||= Faraday.new &block
# end
def conn
@conn ||= Faraday.new do |f|
f.request :multipart
f.request :url_encoded
f.request :basic_auth, id, secret
faraday.call(f) if faraday
f.adapter Faraday.default_adapter unless faraday
end
end

def auth_url
ENVIRONMENTS[environment][:auth_url]
Expand Down
2 changes: 1 addition & 1 deletion spec/dwolla/auth_spec.rb
Expand Up @@ -237,7 +237,7 @@
def stub_token_request client, params, response
stub_request(:post, token_url(client))
.with(:headers => {"Content-Type" => "application/x-www-form-urlencoded"},
:body => URI.encode_www_form(params))
:body => params)
.to_return(:status => response[:status],
:body => JSON.generate(response[:body]))
end
Expand Down
16 changes: 14 additions & 2 deletions spec/dwolla/client_spec.rb
Expand Up @@ -96,11 +96,23 @@
end

it "#faraday" do
block = Proc.new {}
client = Dwolla::Client.new(:id => id, :secret => secret)
client = Dwolla::Client.new :id => id, :secret => secret
expect(client.faraday).to be nil
end

it "#conn" do
client = Dwolla::Client.new :id => id, :secret => secret
expect(client.conn).to be_a Faraday::Connection
expect(client.conn).to be client.conn
end

it "#conn with faraday" do
james_bond = spy "007"
block = Proc.new {|a| james_bond.call(a) }
client = Dwolla::Client.new(:id => id, :secret => secret) {|c| c.faraday &block }
expect(james_bond).to have_received(:call).with(client.conn)
end

it "#auth_url" do
client = Dwolla::Client.new :id => id, :secret => secret
expect(client.auth_url).to eq Dwolla::Client::ENVIRONMENTS[client.environment][:auth_url]
Expand Down

0 comments on commit b7d4352

Please sign in to comment.