Permalink
Browse files

Merge pull request #2 from site5/feature/faraday-refactor

Refactored to use Faraday
  • Loading branch information...
2 parents f31c527 + 3f989df commit 09578d1dd5fc55da4f358e85ad320040d3946640 @itspriddle itspriddle committed Mar 14, 2012
Showing with 9 additions and 21 deletions.
  1. +2 −1 hamburglar.gemspec
  2. +2 −0 lib/hamburglar.rb
  3. +5 −20 lib/hamburglar/gateways/base.rb
View
@@ -16,7 +16,8 @@ Gem::Specification.new do |s|
s.files += Dir['lib/**/*']
s.files += Dir['test/**/*']
-
+ s.add_dependency 'faraday', '~> 0.8.0rc2'
+ s.add_dependency 'faraday_middleware', '~> 0.8.5'
s.add_runtime_dependency('jruby-openssl', '~> 0.7.3') if RUBY_PLATFORM == 'java'
s.add_development_dependency('rspec', '~> 2.6')
s.add_development_dependency('rake', '~> 0.9.2.2')
View
@@ -1,4 +1,6 @@
require 'hamburglar/errors'
+require 'faraday'
+require 'faraday_middleware'
module Hamburglar
autoload :Version, 'hamburglar/version'
@@ -124,29 +124,14 @@ def parse_response(raw = '')
end
# Performs a GET request on the given URI, redirects if needed
- #
- # See Following Redirection at
- # http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html
def fetch(uri_str, limit = 10)
# You should choose better exception.
raise ArgumentError, 'HTTP redirect too deep' if limit == 0
-
- uri = URI.parse(uri_str)
- http = Net::HTTP.new(uri.host, uri.port)
- if uri.scheme == 'https'
- http.use_ssl = true
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
- http.ca_file = File.expand_path('../../../cacert.pem', __FILE__)
- end
- request = Net::HTTP::Get.new(uri.request_uri)
- response = http.start { |http| http.request(request) }
-
- case response
- when Net::HTTPSuccess then response
- when Net::HTTPRedirection then fetch(response['location'], limit - 1)
- else
- response.error!
- end
+ Faraday.new(:url => uri_str, :ssl => {:verify => true, :ca_file => File.expand_path('../../../cacert.pem', __FILE__)}) do |c|
+ c.response :raise_error
+ c.response :follow_redirects, :limit => limit
+ c.adapter :net_http
+ end.get
end
end

0 comments on commit 09578d1

Please sign in to comment.