public
Last active

A preliminary clicky client example built on api_smith

  • Download Gist
gcwi.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
require 'rubygems'
require 'api_smith'
require 'date'
require 'ipaddr'
 
module GettinClickyWithIt
 
class Error < StandardError; end
 
SMART_NUMBER_TRANSFORMER = lambda { |v| Integer(v) rescue v }
 
class StatsCollection < APISmith::Smash
property :date
property :items
end
 
 
def self.collection_transformers
@collection_transformers ||= Hash.new do |h, k|
h[k] = Class.new(StatsCollection).tap do |klass|
klass.transformer_for :items, k
end
end
end
 
class TallyStat < APISmith::Smash
property :title
property :value, :transformer => SMART_NUMBER_TRANSFORMER
property :value_percent, :transformer => :to_f
property :url
property :clicky_url
# Goal Information
property :incompleted, :transformer => :to_i
property :conversion, :transformer => :to_f
property :revenue
property :cost
end
 
class ItemStat < APISmith::Smash
property :time, :transformer => lambda { |v| Time.at(v.to_i) }
property :time_pretty
property :item
property :clicky_url
 
alias query item
alias link item
end
 
class VisitStat < APISmith::Smash
 
property :time, :transformer => lambda { |v| Time.at(v.to_i) }
property :time_pretty
property :time_total, :transformer => :to_i
property :ip_address, :transformer => lambda { |v| IPAddr.new(v) }
property :session_id, :transformer => :to_i
property :actions, :transformer => :to_i
property :web_browser
property :operating_system
property :screen_resolution
property :javascript, :transformer => lambda { |v| v == '1' }
property :language
property :referrer_url
property :referrer_domain
property :referrer_search
property :referrer_type
property :geolocation
property :latitude, :transformer => :to_f
property :longitude, :transformer => :to_f
# These need a premium account fwiw.
property :hostname
property :organization
property :campaign
property :goals_incompleted
property :goals_completed
property :custom
property :clicky_url
 
alias javascript? javascript
 
end
 
class ActionStat < APISmith::Smash
 
property :time, :transformer => lambda { |v| Time.at(v.to_i) }
property :time_pretty
property :time_total, :transformer => :to_i
property :ip_address, :transformer => lambda { |v| IPAddr.new(v) }
property :session_id, :transformer => :to_i
property :action_type
property :action_title
property :action_url
property :referrer_url
property :referrer_domain
property :referrer_search
property :custom
property :clicky_url
 
%w(pageview download outbound).each do |v|
define_method(:"#{v}?") { action_type == v }
end
 
end
 
POPULAR_METHODS = %w(pages pages-entrance pages-exit downloads clicks links links-domains links-outbound searches searches-keywords searches-engines
goals split-tests campaigns countries cities regions languages web-browsers operating-systems screen-resolutions hostnames organizations
engagement-actions engagement-times visitors-most-active traffic-sources tweets shorturls)
TALLY_METHODS = %w(visitors visitors-unique actions actions-average time-average time-average-pretty bounce-rate visitors-online feedburner-statistics)
ITEM_LISTING_TYPES = %w(searches-recent searches-unique links-recent links-unique)
 
CUSTOM_METHODS = {
'visitors-list' => collection_transformers[VisitStat],
'actions-list' => collection_transformers[ActionStat]
}
 
AUTO_TRANSFORMER = lambda do |v|
type = v["type"]
if (transformer = CUSTOM_METHODS[type])
transformer.call v['dates']
elsif POPULAR_METHODS.include?(type) or TALLY_METHODS.include?(type)
collection_transformers[TallyStat].call v['dates']
elsif ITEM_LISTING_TYPES.include?(type)
collection_transformers[ItemStat].call v['dates']
else
v
end
end
 
class Client
include APISmith::Client
 
base_uri 'http://api.getclicky.com/'
endpoint 'api/stats/4'
 
attr_reader :site_id, :site_key
 
def initialize(site_id, site_key)
@site_key = site_key
@site_id = site_id
add_query_options! :site_id => site_id, :sitekey => site_key
end
 
(POPULAR_METHODS + ITEM_LISTING_TYPES + TALLY_METHODS + CUSTOM_METHODS.keys).each do |m|
define_method m.tr('-', '_') do |*args|
singular_api_call m, *args
end
end
 
private
 
def check_response_errors(response)
if response.first.is_a?(Hash) and (error = response.first['error'])
raise Error.new(error)
end
end
 
def base_query_options
{:output => 'json'}
end
 
def singular_api_call(type, options = {})
response = get '/', :extra_query => normalise_options(type, options),
:response_container => [0], :transform => AUTO_TRANSFORMER
extract_item response, options
end
 
def normalise_options(type, options)
encode_parameters(options).merge 'type' => [*type].join(",")
end
 
def extract_item(response, options)
if options[:daily] or options[:hourly]
response
else
response.first.items
end
end
 
def encode_parameters(params)
encoded = {}
params.each_pair do |k, v|
case k.to_s
when 'custom'
v.each_pair do |name, value|
encoded["#{k}[#{name}]"] = value
end
when 'date'
encoded[k] = normalise_value(v) { |t| normalise_date t }
else
encoded[k] = normalise_value v
end
end
encoded
end
 
def normalise_value(value, &blk)
blk ||= :to_s.to_proc
case value
when Range
[blk.call(value.begin), blk.call(value.end)] * ","
when Array
value.map(&blk) * "|"
when true
'1'
when false
'0'
else
value.to_s
end
end
 
def normalise_date(date)
if date.respond_to?(:strftime)
date.strftime "%Y-%m-%d"
else
date.to_s
end
end
 
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.