2012-08-13 42 views
0

我已經設法使這個工作幾乎可以工作,但我認爲我正在弄到一些嚴重困惑的事情。所有的例子似乎都是基於他們的id找到單個記錄。如何使用basecamp API返回與公司關聯的所有電子郵件地址的列表

我想我將能夠傳遞一個PARAM在單個屬性

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

的Basecamp :: Company.find(過濾:所有,:PARAMS => {:名稱=> 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"] 

回答

0

事實證明:首先將返回我一個單一的公司,因爲我想,但是我沒有找到方法的邏輯,因爲我不想第一個條目,我想符合我的PARAMS中的任何條目..

#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 
相關問題