Skip to content

Commit

Permalink
Use curl to connect to Fogbugz, instead of open-uri or Net::HTTP.
Browse files Browse the repository at this point in the history
This ensures SSL is properly taken care of:  Ruby on Mac OS X 10.5 failed with an SSL verification error.
  • Loading branch information
francois committed Jun 3, 2008
1 parent ef550d0 commit 23d792a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
16 changes: 10 additions & 6 deletions fogbugz_service.rb
@@ -1,5 +1,4 @@
require "uri" require "uri"
require "open-uri"
require "rexml/document" require "rexml/document"
require "rexml/xpath" require "rexml/xpath"
require "activesupport" require "activesupport"
Expand All @@ -12,8 +11,9 @@ class BadCredentials < FogbugzError; end


attr_reader :root_uri, :api_uri attr_reader :root_uri, :api_uri


def initialize(root) def initialize(root, curl)
@root_uri = root.respond_to?(:scheme) ? root : URI.parse(root) @root_uri = root.respond_to?(:scheme) ? root : URI.parse(root)
@curl = curl
end end


def validate! def validate!
Expand All @@ -34,7 +34,9 @@ def connect


def logon(email, password) def logon(email, password)
params = {"cmd" => "logon", "email" => email, "password" => password} params = {"cmd" => "logon", "email" => email, "password" => password}
document = get(@api_uri.merge("?#{params.to_query}")) uri = @api_uri.dup
uri.query = params.to_query
document = get(uri)
bad_logon = REXML::XPath.first(document.root, "//error") bad_logon = REXML::XPath.first(document.root, "//error")
raise BadCredentials, "Bad credentials supplied to Fogbugz: #{bad_logon}" unless bad_logon.blank? raise BadCredentials, "Bad credentials supplied to Fogbugz: #{bad_logon}" unless bad_logon.blank?
REXML::XPath.first(document.root, "//token/text()").to_s REXML::XPath.first(document.root, "//token/text()").to_s
Expand All @@ -43,11 +45,13 @@ def logon(email, password)
protected protected
# Returns an REXML::Document to the specified URI # Returns an REXML::Document to the specified URI
def get(uri) def get(uri)
data = open(uri) cmd = "#{@curl} --silent '#{uri.to_s}'"
puts cmd
data = `#{cmd}`
begin begin
REXML::Document.new(data.read) REXML::Document.new(data)
rescue REXML::ParseException rescue REXML::ParseException
raise BadXml, "Could not parse response data:\n#{data.read}" raise BadXml, "Could not parse response data:\n#{data}"
end end
end end
end end
5 changes: 3 additions & 2 deletions test/fogbugz_service_logon_test.rb
Expand Up @@ -4,14 +4,15 @@
class FogbugzServiceLogonTest < Test::Unit::TestCase class FogbugzServiceLogonTest < Test::Unit::TestCase
def setup def setup
@service_uri = URI.parse("http://fogbugz.my-service.com/") @service_uri = URI.parse("http://fogbugz.my-service.com/")
@service = FogbugzService.new(@service_uri) @service = FogbugzService.new(@service_uri, "/path/to/curl")
@service.stubs(:get).returns(REXML::Document.new(VALID_API_RESPONSE)) @service.stubs(:get).returns(REXML::Document.new(VALID_API_RESPONSE))
@uri = @service.validate! @uri = @service.validate!
end end


def test_logon_calls_fogbugz_to_retrieve_token def test_logon_calls_fogbugz_to_retrieve_token
params = {"cmd" => "logon", "email" => "me@my-domain.com", "password" => "my-super-duper-password"}.to_query params = {"cmd" => "logon", "email" => "me@my-domain.com", "password" => "my-super-duper-password"}.to_query
@service.expects(:get).with(@uri.merge("?#{params}")).returns(REXML::Document.new(VALID_LOGON_RESPONSE)) @uri.query = params
@service.expects(:get).with(@uri).returns(REXML::Document.new(VALID_LOGON_RESPONSE))
@service.logon("me@my-domain.com", "my-super-duper-password") @service.logon("me@my-domain.com", "my-super-duper-password")
end end


Expand Down
2 changes: 1 addition & 1 deletion test/fogbugz_service_test.rb
Expand Up @@ -4,7 +4,7 @@
class FogbugzServiceTest < Test::Unit::TestCase class FogbugzServiceTest < Test::Unit::TestCase
def setup def setup
@service_uri = URI.parse("http://fogbugz.my-service.com/") @service_uri = URI.parse("http://fogbugz.my-service.com/")
@service = FogbugzService.new(@service_uri) @service = FogbugzService.new(@service_uri, "/path/to/curl")
end end


def test_validate_connects_to_fogbugz_and_retrieves_the_api_url def test_validate_connects_to_fogbugz_and_retrieves_the_api_url
Expand Down

0 comments on commit 23d792a

Please sign in to comment.