analytico 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Gemini SBS LLC.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,15 @@
1
+ = Analytico Ruby Client
2
+
3
+ Tracks your app's impressions and custom metrics.
4
+
5
+ == Status
6
+
7
+ This gem is currently unreleased.
8
+
9
+ == Copyright
10
+
11
+ Copyright (c) 2010 Gemini SBS LLC. See LICENSE for details.
12
+
13
+ == Author
14
+
15
+ * (Mauricio Gomes)[http://github.com/mgomes]
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
@@ -0,0 +1,72 @@
1
+ module Analytico
2
+
3
+ class Client
4
+
5
+ @@connection = nil
6
+ @@debug = false
7
+
8
+ class << self
9
+
10
+ def set_credentials(api_key)
11
+ @@connection = Connection.new(api_key)
12
+ @@connection.debug = @@debug
13
+ true
14
+ end
15
+
16
+ def debug=(debug_flag)
17
+ @@debug = debug_flag
18
+ @@connection.debug = @@debug if @@connection
19
+ end
20
+
21
+ def debug
22
+ @@debug
23
+ end
24
+
25
+ def add_impression(*args)
26
+ raise NoConnectionEstablished if @@connection.nil?
27
+
28
+ options = extract_options!(args)
29
+ if options[:fqdn].nil? || options[:ip].nil?
30
+ return true
31
+ end
32
+ params = Hash.new
33
+ params[:impression] = options
34
+ params[:impression][:api_key] = @@connection.api_key
35
+
36
+ response = post(Endpoint.impressions, params)
37
+ HashUtils.recursively_symbolize_keys(response)
38
+ end
39
+
40
+ def add_metric(fqdn, name, value)
41
+ raise NoConnectionEstablished if @@connection.nil?
42
+
43
+ params = Hash.new
44
+ params[:metric] = Hash.new
45
+ params[:metric][:api_key] = @@connection.api_key
46
+ params[:metric][:fqdn] = fqdn
47
+ params[:metric][:name] = name
48
+ params[:metric][:value] = value
49
+
50
+ response = post(Endpoint.metrics, params)
51
+ HashUtils.recursively_symbolize_keys(response)
52
+ end
53
+
54
+ def post(endpoint, data=nil)
55
+ @@connection.post endpoint, data
56
+ end
57
+
58
+ private
59
+
60
+ def extract_options!(args)
61
+ if args.last.is_a?(Hash)
62
+ return args.pop
63
+ else
64
+ return {}
65
+ end
66
+ end
67
+
68
+ end
69
+
70
+ end
71
+
72
+ end
@@ -0,0 +1,105 @@
1
+ module Analytico
2
+
3
+ class Connection
4
+
5
+ attr_accessor :debug
6
+ attr_reader :api_key, :default_options
7
+
8
+ def initialize(api_key)
9
+ @api_key = api_key
10
+ @default_options = { }
11
+ @debug = false
12
+ end
13
+
14
+ def post(endpoint, data=nil)
15
+ request :post, endpoint, data
16
+ end
17
+
18
+ private
19
+
20
+ def request(method, endpoint, data=nil)
21
+ headers = { 'User-Agent' => "Analytico Ruby Client v#{VERSION}",
22
+ 'Content-Type' => "application/json" }
23
+
24
+ if data.nil?
25
+ data = @default_options
26
+ else
27
+ data.merge!(@default_options)
28
+ end
29
+
30
+ if debug
31
+ puts "request: #{method.to_s.upcase} #{endpoint}"
32
+ puts "headers:"
33
+ headers.each do |key, value|
34
+ puts "#{key}=#{value}"
35
+ end
36
+ if [:post, :put].include?(method)
37
+ puts "data:"
38
+ puts Yajl::Encoder.encode data
39
+ end
40
+ end
41
+
42
+ begin
43
+ data = Yajl::Encoder.encode data
44
+ rescue
45
+ logger("there was an error encoding your submission: #{$!}")
46
+ return nil
47
+ end
48
+
49
+ response = send_request(method, endpoint, headers, data)
50
+
51
+ if debug
52
+ if response.nil?
53
+ puts "There was an error processing the response from Analytico."
54
+ else
55
+ puts "\nresponse: #{response.code}"
56
+ puts "headers:"
57
+ response.header.each do |key, value|
58
+ puts "#{key}=#{value}"
59
+ end
60
+ puts "body:"
61
+ puts response.body
62
+ end
63
+ end
64
+
65
+ if response.nil? || response.body.empty?
66
+ content = nil
67
+ else
68
+ begin
69
+ content = Yajl::Parser.new.parse(response.body)
70
+ rescue
71
+ logger("received invalid response: #{$!}")
72
+ return "{}"
73
+ end
74
+ end
75
+
76
+ content
77
+ end
78
+
79
+ def send_request(method, endpoint, headers, data=nil)
80
+ begin
81
+ response = RestClient::Request.execute(:method => :post,
82
+ :url => endpoint,
83
+ :payload => data,
84
+ :headers => headers,
85
+ :timeout => 0.1)
86
+ rescue => e
87
+ logger("there was an error transmitting your entry: #{$!}")
88
+ return nil
89
+ end
90
+
91
+ response
92
+ end
93
+
94
+ def logger(error_msg)
95
+ msg = "%%%% There was an error communicating with Analytico: #{error_msg}"
96
+ if defined? Rails
97
+ Rails.logger.info msg
98
+ else
99
+ STDERR.puts msg
100
+ end
101
+ end
102
+
103
+ end
104
+
105
+ end
@@ -0,0 +1,23 @@
1
+ module Analytico
2
+
3
+ class Endpoint
4
+
5
+ class << self
6
+
7
+ def impressions
8
+ endpoint_url "impressions.json"
9
+ end
10
+
11
+ def metrics
12
+ endpoint_url "metrics.json"
13
+ end
14
+
15
+ def endpoint_url(path)
16
+ [REALM, path].join('/')
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+
23
+ end
@@ -0,0 +1,28 @@
1
+ module Analytico
2
+
3
+ class HashUtils
4
+
5
+ def self.symbolize_keys(hash)
6
+ hash.inject({}) do |options, (key, value)|
7
+ options[(key.to_sym rescue key) || key] = value
8
+ options
9
+ end
10
+ end
11
+
12
+ def self.recursively_symbolize_keys(object)
13
+ if object.is_a? Hash
14
+ symbolized_hash = symbolize_keys(object)
15
+ symbolized_hash.each do |key, value|
16
+ symbolized_hash[key] = recursively_symbolize_keys(value)
17
+ end
18
+ symbolized_hash
19
+ elsif object.is_a? Array
20
+ object.map {|value| recursively_symbolize_keys(value) }
21
+ else
22
+ object
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,13 @@
1
+ module AnalyticoController
2
+
3
+ private
4
+
5
+ def analytico_impression
6
+ Analytico::Client.add_impression(:fqdn => request.host, :ip => request.ip, :user_agent => request.user_agent, :referrer => request.referrer, :url => request.path)
7
+ end
8
+
9
+ end
10
+
11
+ if defined? ActionController
12
+ ActionController::Base.send :include, AnalyticoController
13
+ end
data/lib/analytico.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'uri'
2
+ require 'yajl'
3
+ require 'rest_client'
4
+
5
+ # Analytico specific libs
6
+ require 'analytico/connection'
7
+ require 'analytico/endpoint'
8
+ require 'analytico/hash_utils'
9
+ require 'analytico/client'
10
+
11
+ # Rails integration libs
12
+ require 'analytico/railtie/rack_impression'
13
+
14
+ module Analytico
15
+
16
+ REALM = "http://analytico.heroku.com"
17
+ VERSION = File.read(File.join(File.dirname(__FILE__), '..', 'VERSION'))
18
+
19
+ class AnalyticoError < StandardError; end
20
+ class InsufficentArguments < AnalyticoError; end
21
+ class InvalidAPIKey < AnalyticoError; end
22
+ class NoConnectionEstablished < AnalyticoError; end
23
+
24
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "AnalyticoRuby" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'analytico'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: analytico
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
+ platform: ruby
12
+ authors:
13
+ - Mauricio Gomes
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-09 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: yajl-ruby
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 0
32
+ - 7
33
+ - 7
34
+ version: 0.7.7
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rest-client
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 15
46
+ segments:
47
+ - 1
48
+ - 6
49
+ - 0
50
+ version: 1.6.0
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: rack
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">"
60
+ - !ruby/object:Gem::Version
61
+ hash: 23
62
+ segments:
63
+ - 1
64
+ - 0
65
+ - 0
66
+ version: 1.0.0
67
+ type: :runtime
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 13
78
+ segments:
79
+ - 1
80
+ - 2
81
+ - 9
82
+ version: 1.2.9
83
+ type: :development
84
+ version_requirements: *id004
85
+ description: Ruby client for the Analytico impression and app metric tracking service.
86
+ email: mgomes@geminisbs.com
87
+ executables: []
88
+
89
+ extensions: []
90
+
91
+ extra_rdoc_files:
92
+ - LICENSE
93
+ - README.rdoc
94
+ files:
95
+ - LICENSE
96
+ - README.rdoc
97
+ - VERSION
98
+ - lib/analytico.rb
99
+ - lib/analytico/client.rb
100
+ - lib/analytico/connection.rb
101
+ - lib/analytico/endpoint.rb
102
+ - lib/analytico/hash_utils.rb
103
+ - lib/analytico/railtie/rack_impression.rb
104
+ - spec/analytico_spec.rb
105
+ - spec/spec_helper.rb
106
+ has_rdoc: true
107
+ homepage: http://github.com/geminisbs/analytico_ruby
108
+ licenses: []
109
+
110
+ post_install_message:
111
+ rdoc_options:
112
+ - --charset=UTF-8
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ hash: 3
121
+ segments:
122
+ - 0
123
+ version: "0"
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ none: false
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ hash: 3
130
+ segments:
131
+ - 0
132
+ version: "0"
133
+ requirements: []
134
+
135
+ rubyforge_project:
136
+ rubygems_version: 1.3.7
137
+ signing_key:
138
+ specification_version: 3
139
+ summary: Ruby client for Analytico
140
+ test_files:
141
+ - spec/analytico_spec.rb
142
+ - spec/spec_helper.rb