Skip to content

Commit

Permalink
Use faraday_middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
sausman committed Jan 7, 2016
1 parent b7d4352 commit 6e34478
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -7,3 +7,4 @@
/pkg/
/spec/reports/
/tmp/
/vendor/
3 changes: 1 addition & 2 deletions README.md
Expand Up @@ -92,7 +92,6 @@ To install this gem onto your local machine, run `bundle exec rake install`. To

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dwolla.


## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
The gem is available as open source under the terms of the [Unlicense License](http://unlicense.org/).
3 changes: 2 additions & 1 deletion dwolla.gemspec
Expand Up @@ -32,5 +32,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "webmock", "~> 1.22"

spec.add_dependency "faraday", "~> 0.9.2"
spec.add_dependency "faraday", "~> 0.9"
spec.add_dependency "faraday_middleware", "~> 0.10"
end
1 change: 1 addition & 0 deletions lib/dwolla.rb
Expand Up @@ -3,6 +3,7 @@
require "json"

require "faraday"
require "faraday_middleware"

require "dwolla/version"
require "dwolla/client"
Expand Down
2 changes: 1 addition & 1 deletion lib/dwolla/auth.rb
Expand Up @@ -43,7 +43,7 @@ def query

def self.request_token client, params
res = client.conn.post client.token_url, params
res_body = Util.parse_json res.body
res_body = Util.deep_symbolize_keys res.body
if res.status == 200
Token.new client, res_body
else
Expand Down
4 changes: 2 additions & 2 deletions lib/dwolla/client.rb
Expand Up @@ -46,9 +46,9 @@ def faraday &block

def conn
@conn ||= Faraday.new do |f|
f.request :multipart
f.request :url_encoded
f.request :basic_auth, id, secret
f.request :url_encoded
f.response :json, :content_type => /\bjson$/
faraday.call(f) if faraday
f.adapter Faraday.default_adapter unless faraday
end
Expand Down
15 changes: 15 additions & 0 deletions lib/dwolla/token.rb
Expand Up @@ -9,6 +9,7 @@ def initialize client, params
@expires_in = params[:expires_in]
@scope = params[:scope]
@account_id = params[:account_id]
conn
freeze
end

Expand All @@ -21,5 +22,19 @@ def stringify_keys
"account_id" => account_id
}.reject {|k,v| v.nil? }
end

private

def conn
@conn ||= Faraday.new do |f|
f.authorization :Bearer, access_token if access_token
f.headers["Accept"] = "application/vnd.dwolla.v1.hal+json"
f.request :multipart
f.request :json
f.response :json, :content_type => /\bjson$/
client.faraday.call(f) if client.faraday
f.adapter Faraday.default_adapter unless client.faraday
end
end
end
end
4 changes: 0 additions & 4 deletions lib/dwolla/util.rb
@@ -1,9 +1,5 @@
module Dwolla
module Util
def self.parse_json string
deep_symbolize_keys JSON.parse(string)
end

def self.deep_symbolize_keys obj
if obj.is_a? Hash
Hash[obj.map{|k,v| [k.to_sym, deep_symbolize_keys(v)] }]
Expand Down
1 change: 1 addition & 0 deletions spec/dwolla/auth_spec.rb
Expand Up @@ -239,6 +239,7 @@ def stub_token_request client, params, response
.with(:headers => {"Content-Type" => "application/x-www-form-urlencoded"},
:body => params)
.to_return(:status => response[:status],
:headers => {"Content-Type" => "application/json"},
:body => JSON.generate(response[:body]))
end

Expand Down

0 comments on commit 6e34478

Please sign in to comment.