1

我正在尝试使用 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 hack 。

4

1 回答 1

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
于 2012-03-12T14:57:26.833 回答