Unfuddle API获取帐户信息

时间:2012-03-12 11:33:48

标签: ruby-on-rails ruby activeresource unfuddle

我尝试使用ActiveResource从Unfuddle API获取帐户信息

网址为http://mydomain.unfuddle.com/api/v1/account

这是我的ActiveResource类

class Account < ActiveResource::Base
  self.collection_name = "account"
  self.site = "https://mydomain.unfuddle.com/api/v1"
  self.user = "me"
  self.password = "pass"
end

如果我尝试通过Account.all获取我的帐户信息,我将获得一个空数组,但如果我尝试这个

require 'net/https'

UNFUDDLE_SETTINGS = {
  :subdomain  => 'mydomain',
  :username   => 'me',
  :password   => 'pass',
  :ssl        => true
}

http = Net::HTTP.new("#{UNFUDDLE_SETTINGS[:subdomain]}.unfuddle.com",UNFUDDLE_SETTINGS[:ssl] ? 443 : 80)

if UNFUDDLE_SETTINGS[:ssl]
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

begin
  request = Net::HTTP::Get.new('/api/v1/account')
  request.basic_auth UNFUDDLE_SETTINGS[:username], UNFUDDLE_SETTINGS[:password]

  response = http.request(request)
  if response.code == "200"
    puts response.body
  else
    puts "HTTP Status Code: #{response.code}."
  end
rescue => e
  puts e.message
end

我收到了我的帐户信息,以及为什么ActiveResource方法无效的任何想法?

**更新

我忘了指出我遇到此问题https://github.com/rails/rails/issues/2318而我使用 erikkallens 黑客。

1 个答案:

答案 0 :(得分:2)

似乎是这个问题https://github.com/rails/rails/issues/2318,我尝试了 vaskas 解决方案,但默认情况下我无法修改它。

class Account < ActiveResource::Base
  self.collection_name = "account"
  self.site = "https://mydomain.unfuddle.com/api/v1"
  self.user = "me"
  self.password = "pass"
  self.format = AccountXMLFormatter.new
end

class AccountXMLFormatter
  include ActiveResource::Formats::XmlFormat
  def decode(xml)
    [account: ActiveResource::Formats::XmlFormat.decode(xml)]
  end
end