freerobby/sendgrid_toolkit — Issue #5: error when retrieving statistics-aggregate with specific :category option 
Author: eliasbaixas
Created: 2011-10-08 09:50:43+00:00
State: open

Hello,

when requesting the aggregate statistics for a sendgrid category, for example like this:

statistics.retrieve_aggregate :category => "my_category"

an exception is thrown (NoMethodError: undefined method `has_key?' for #)
from sendgrid_toolkit-1.0.5/lib/sendgrid_toolkit/statistics.rb:12

from what I see, retrieve_aggregate returns a Hash if no category is specified, in which case has_key? does exist. OR it returns an array with one hash if a :category was specified, like:
(without specifying category:)
irb> r = HTTParty.post("https://sendgrid.com/api/stats.get.json?", :query => get_credentials.merge(:aggregate => 1), :format => :json)
{"repeat_spamreports"=>19,
"invalid_email"=>17,
"spamreports"=>21,
"bounces"=>177,
"delivered"=>12977,
"blocked"=>23,
"unique_clicks"=>124,
"clicks"=>218,
"requests"=>13460,
"unsubscribes"=>11,
"unique_opens"=>3847,
"repeat_bounces"=>248,
"spam_drop"=>0,
"opens"=>7740,
"repeat_unsubscribes"=>37}

(specifying a category:)
r = HTTParty.post("https://sendgrid.com/api/stats.get.json?", :query => get_credentials.merge(:aggregate => 1,:category => "my_category"), :format => :json)
[{"repeat_spamreports"=>0,
"invalid_email"=>3,
"name"=>"my_category",
"spamreports"=>6,
"bounces"=>98,
"delivered"=>3245,
"blocked"=>6,
"unique_clicks"=>0,
"clicks"=>0,
"requests"=>3337,
"unsubscribes"=>0,
"unique_opens"=>763,
"repeat_bounces"=>11,
"spam_drop"=>0,
"opens"=>1227,
"repeat_unsubscribes"=>0}]

so a solution would be to check if its an array in lib/sendgrid_toolkit/statistics.rb#retrieve_aggregate

thanks !

Elias