class GoSquared::Trends

Constants

METRICS

Public Class Methods

new(api_key, site_id) click to toggle source
# File lib/go_squared/trends.rb, line 5
def initialize api_key, site_id
  @api_key  = api_key
  @site_id  = site_id
  
  @start    = 1.month.ago.to_i
  @end      = Time.now.to_i
  @metric   = :pageviews
  @group    = :day
end

Public Instance Methods

from(time) click to toggle source
# File lib/go_squared/trends.rb, line 26
def from time
  @start = time.to_i
  self
end
from_xml() click to toggle source
# File lib/go_squared/trends.rb, line 60
def from_xml
  hash = Hash.from_xml(xml)
  
  hash.recursive_symbolize_keys!
  
  hash[:data][@group].blank? ? hash[:data] : hash[:data][@group]
end
group_by(group) click to toggle source
# File lib/go_squared/trends.rb, line 41
def group_by group
  @group = group.to_sym
  self
end
limit(value) click to toggle source
# File lib/go_squared/trends.rb, line 46
def limit value
  @limit = value
  self
end
on(metric) click to toggle source
# File lib/go_squared/trends.rb, line 21
def on metric
  @metric = metric.to_sym
  self
end
period(value) click to toggle source
# File lib/go_squared/trends.rb, line 51
def period value
  @period = value
  self
end
timezone(value) click to toggle source
# File lib/go_squared/trends.rb, line 36
def timezone value
  @timezone = value
  self
end
to(time) click to toggle source
# File lib/go_squared/trends.rb, line 31
def to time
  @end = time.to_i
  self
end
xml() click to toggle source
# File lib/go_squared/trends.rb, line 56
def xml
  fetch
end

Private Instance Methods

fetch() click to toggle source
# File lib/go_squared/trends.rb, line 70
def fetch
  res = Net::HTTP.start('api.gosquared.com') do |http|
    params = []
    params << "api_key=#{ @api_key }"
    params << "sid=#{ @site_id }"
    params << "metric=#{ @metric }"
    params << "start=#{ @start }"
    params << "end=#{ @end }"
    params << "grouping=#{ @group }"    if @group
    params << "limit=#{ @limit }"       if @limit
    params << "period=#{ @period }"     if @period
    params << "timezone=#{ @timezone }" if @timezone
    
    http.get("/trends.xml?#{ params.join('&') }")
  end
  
  res.body
rescue e
  "<data><error>#{ e }</error></data>"
end