【问题标题】:Unfuddle API get accounts infoUnfuddle API 获取账户信息
【发布时间】:2012-03-12 11:33:48
【问题描述】:

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

【问题讨论】:

    标签: ruby-on-rails ruby activeresource unfuddle


    【解决方案1】:

    似乎是这个问题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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-12
      • 2019-02-27
      • 1970-01-01
      • 2013-05-04
      相关资源
      最近更新 更多