0

我已经设法让它几乎可以工作,但我认为我遇到了一些严重的困惑。所有示例似乎都涵盖了根据 id 查找单个记录。

我以为我可以传递一个参数来过滤单个属性

https://xxxx.basecamphq.com/companies.xml?name=xxxx

Basecamp::Company.find(:all, :params => { :name => xxx })

require 'rubygems'
require 'English'
require 'trollop'
require 'basecamp'
require 'ap'

opts = Trollop::options do
    version "1.0"
    banner <<-EOS
    #{File.basename($0)}
    Description: Dump the users from a company stored in basecampe via the api.

    Usage:
           #{File.basename($0)} [options]
    where [options] are:
    EOS
    opt :company , "Company to export" , :default => "XXXX"
    opt :token_file, "File to read token from " , :default => "~/.basecamp/token"
end


basecamp_token = IO.read(File.expand_path(opts[:token_file])).chomp

Basecamp.establish_connection!('xxxx.basecamphq.com', basecamp_token , 'X' , true)

#Work out company id from company name
company = Basecamp::Company.find(:all).select { |c| c.attributes["name"] == opts[:company] }
company_id = company[0].instance_variable_get("@attributes")["id"]

people = Basecamp::Person.find(:all, :params => {:company_id => company_id })
#ap people

#todo work out how to iterate over all people
puts people[0].instance_variable_get("@attributes")["email_address"]
4

1 回答 1

0

结果:first 会根据我的需要返回一家公司,但是我不认为该方法合乎逻辑,因为我不想要第一个条目,我想要任何与我的参数匹配的条目..

#Work out company id from company name
company = Basecamp::Company.find(:first, :params => {:name => opts[:company] })
people = Basecamp::Person.find(:all, :params => {:company_id => company.id })

people.each do | person |
    puts person.email_address
end
于 2012-08-15T10:13:47.703 回答