Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit f7e0b2ce0268149dd53e23da78ff8469f538f41b @gamache gamache committed Sep 13, 2010
Showing with 105 additions and 0 deletions.
  1. +1 −0 Changelog
  2. +19 −0 LICENSE
  3. +5 −0 README
  4. +15 −0 clicky-api.gemspec
  5. +65 −0 lib/clicky-api.rb
  6. 0 test/test-suite.rb
@@ -0,0 +1 @@
+0.1: Works. First commit.
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2010 Where, Inc.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
5 README
@@ -0,0 +1,5 @@
+Clicky-api is a Ruby Gem which interfaces with version 4 of the Clicky web
+analytics API, documented at http://getclicky.com/help/api.
+
+
+This gem was written by Pete Gamache with the gracious support of Where, Inc.
@@ -0,0 +1,15 @@
+Gem::Specification.new do |s|
+ s.name = %q{clicky-api}
+ s.version = "0.1"
+ s.date = %q{2010-09-13}
+ s.authors = ["pete gamache"]
+ s.email = %q{gamache@gmail.com}
+ s.summary = %q{Clicky-api provides an interface to the Clicky web analytics API, version 4.}
+ s.homepage = %q{http://github.com/where/clicky-api/}
+ s.description = %q{Clicky-api provides an interface to the Clicky web analytics API, version 4.}
+ s.files = [
+ "README", "Changelog", "LICENSE",
+ "lib/clicky-api.rb", "test/test-suite.rb"
+ ]
+ s.test_file = 'test/test-suite.rb'
+end
@@ -0,0 +1,65 @@
+require 'rubygems'
+require 'httparty'
+
+class ClickyAPI
+ include HTTParty
+ base_uri "http://api.getclicky.com/api/stats/4"
+
+ ## the list of acceptable API parameters -- unused at the moment
+ @@PARAMS = %w{
+ site_id sitekey type output limit date
+ daily hourly item visitor_details time_offset
+ segments ip_address href landing exit
+ num_actions search domain link browser os
+ resolution country city language hostname
+ org source shorturl custom session_id
+ title href action_type filter json_callback
+ json_var null_values app
+ }
+
+ ## required API params -- unused at the moment
+ @@REQUIRED_PARAMS = %w{ site_id sitekey type }
+
+ ## params set to persist between calls. keys are stringified. this is
+ ## a class variable -- global scope is in effect
+ @@set_params = nil
+
+ ## load_config_file! is called from the Rails initializer. This code might
+ ## need to be re-run if you're using the "development" environment, and
+ ## the file gets reloaded. Gross.
+ def self.load_config_file!(file="#{Rails.root}/config/clicky-api.yml")
+ @@set_params = {}
+ config = YAML.load_file(file) || {}
+ ClickyAPI.set!(config)
+ end
+
+ ## the 'set_params!' method is used to set params persistently, class-wide
+ def self.set_params!(param_hash={})
+ self.load_config_file! if @@set_params.nil? ## development mode only
+ param_hash.each { |k,v| @@set_params[k.to_s] = v }
+ end
+
+ ## get_params returns the hash of params set by #set_params!
+ def get_params
+ {}.merge(@@set_params)
+ end
+
+ ## the 'stats' method is used to obtain results. I wanted to call it
+ ## 'get' but the HTTParty module uses that method name.
+ def self.stats(param_hash={})
+ self.load_config_file! if @@set_params.nil? # development mode only
+
+ ## merge parameters, converting keys to strings
+ params = {}
+ @@set_params.each { |k,v| params[k.to_s] = v }
+ param_hash.each { |k,v| params[k.to_s] = v }
+
+ ## allow 'type' param to take arrays; join values with commas
+ if params['type'].kind_of?(Array)
+ params['type'] = params['type'].map{|v| v.to_s}.join(',')
+ end
+
+ return get('', :query => params).parsed_response['response']
+ end
+
+end
No changes.

0 comments on commit f7e0b2c

Please sign in to comment.