Unfuddle API get accounts info

2019-08-15 03:58发布

I'm trying to get the account info from Unfuddle API using ActiveResource

The url is http://mydomain.unfuddle.com/api/v1/account

this is my ActiveResource class

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

if I try getting my account info with Account.all I'll get an empty array but if I try this

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

I get my account information , any ideas why the ActiveResource approach isn't working ?

**UPDATE

I forgot to specify that I had this issue https://github.com/rails/rails/issues/2318 and I use erikkallens hack .

1条回答
2楼-- · 2019-08-15 04:11

It seems to be this issue https://github.com/rails/rails/issues/2318 , I tried vaskas solution but it didn't work by default I had to modify it.

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
查看更多
登录 后发表回答