2011-05-13 45 views
1

我与Garb宝石红宝石打不过我无法访问的结果。Accesing装束宝石结果

这是我的代码。

Garb::Session.login('[email protected]', 'password') 
profile = Garb::Management::Profile.all.detect {|profile| profile.web_property_id == 'UA-XXXXX-X'} 
@ga = profile.stats(:start_date => (Date.today - 1), :end_date => Date.today) 

如果我在视图上使用调试,我可以看到结果,但无论我尝试我不能访​​问结果。

下面是调试结果

--- !ruby/object:Garb::ResultSet 
     results: 
     - !ruby/object:OpenStruct 
      table: 
      :exits: "7820" 
      :pageviews: "24171" 
     sampled: false 
     total_results: 1 

  • @ ga.results.table.exits
  • @ ga.exits
  • @ ga.table.exits

我已经尝试过将它制作成一个数组,并且没有运气。

你以前用过这颗宝石吗?如果是的话,我如何获得这些结果。

感谢您的时间

回答

4

Garb::ResultSet是一个枚举,所以你可以使用任何的枚举方法就可以了(eachmap等)。个别结果OpenStruct S,这样你就可以直接访问它们。

@ga.map &:exits # returns an array of all exits 
+0

太谢谢你了。这是我的头脑。 – Lee 2011-05-23 21:25:34

0

我使用此代码来拉我想要使用GARB。

require 'rubygems' 
require 'garb' 
require 'csv' 

CA_CERT_FILE = "Cthe exact location of your cacert.pem file" 
username  = "your google analytics email address" 
password  = "your google analytics password" 
web_profile = "UA-the correct number here" 
limit   = 10000 
filter  = {:productName.contains => "some product"} # this is your filter 
sorter  = :date.desC# you can use this part to sort 
csvfile  = "YourResults.csv" 


Garb::Session.login(username, password, :secure => true) 

class Report 
extend Garb::Model 

    metrics  :itemQuantity 

    dimensions :productName, 
       :date, 
       :customVarName2 


end 

CSV.open(csvfile, "w") do |csv| 
     csv << [ "itemQuantity", 
        "productName", 
        "date", 
        "customVarName2" 
        ] 
end 

1.times do |i| # Be careful, you can cause duplication with this iteration. only do once per 10,000 expected rows 
    profile = Garb::Management::Profile.all.detect {|p| p.web_property_id == web_profile} 
    options = { 
       :start_date  => (Date.today - 30), 
       :end_date   => Date.today, 
       :limit   => limit, 
       :offset   => (i*limit) + 1, 
       :sort   => sorter 
       } 

    result = Report.results(profile, options) 
    result = Report.results(profile, :filters => filter)  

    CSV.open(csvfile, "a") do |csv| 

     result.each do |r| 
      csv << ["#{r.item_quantity}", 
         "#{r.product_name}", 
         "#{r.date}", 
         "#{r.custom_var_name2}" 
         ] 
      end 
     end 
end 

要找到cacert.pem文件放在这里http://curl.haxx.se/ca/cacert.pem 保存,作为一个文件,并在代码中引用它。

有一点要注意的是,你将需要请求在pascalcase的指标和维度,但然后把它们放进你的CSV以下划线情况下(为什么,我不知道)。

除此之外,你很乖。完成后请打开CSV文件。