analytico 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.3.0
@@ -6,13 +6,13 @@ module Analytico
6
6
  class Client
7
7
 
8
8
  @@connection = nil
9
- @@debug = false
9
+ @@debug, @@async = false
10
10
 
11
11
  class << self
12
12
 
13
13
  def set_credentials(api_key)
14
14
  @@connection = Connection.new(api_key)
15
- @@connection.debug = @@debug
15
+ self.debug = @@debug
16
16
  true
17
17
  end
18
18
 
@@ -25,6 +25,15 @@ module Analytico
25
25
  @@debug
26
26
  end
27
27
 
28
+ def async=(async_flag)
29
+ @@async = async_flag
30
+ @@connection.async = @@async if @@connection
31
+ end
32
+
33
+ def async
34
+ @@async
35
+ end
36
+
28
37
  def add_impression(*args)
29
38
  raise NoConnectionEstablished if @@connection.nil?
30
39
 
@@ -54,20 +63,16 @@ module Analytico
54
63
  HashUtils.recursively_symbolize_keys(response)
55
64
  end
56
65
 
57
- def async_impression(*args)
58
- self.send_later(:add_impression, *args)
59
- end
60
-
61
- def async_metric(*args)
62
- self.send_later(:add_metric, *args)
63
- end
64
-
65
66
  def post(endpoint, data=nil)
66
67
  unless production_env?
67
- warn "Analytico: Skipping communication to endpoint #{endpoint} while not in production. Would have sent: #{data.inspect}"
68
+ warn "%%% Analytico %%% Skipping while not in production. Would have sent (async: #{@@async ? 'on' : 'off' }): #{data.inspect}"
68
69
  return nil
69
70
  end
70
- @@connection.post endpoint, data
71
+ if @@async
72
+ self.send_later @@connection.post(endpoint, data)
73
+ else
74
+ @@connection.post endpoint, data
75
+ end
71
76
  end
72
77
 
73
78
  private
@@ -2,13 +2,14 @@ module Analytico
2
2
 
3
3
  class Connection
4
4
 
5
- attr_accessor :debug
5
+ attr_accessor :debug, :async
6
6
  attr_reader :api_key, :default_options
7
7
 
8
8
  def initialize(api_key)
9
9
  @api_key = api_key
10
10
  @default_options = { }
11
11
  @debug = false
12
+ @async = false
12
13
  end
13
14
 
14
15
  def post(endpoint, data=nil)
@@ -78,11 +79,18 @@ module Analytico
78
79
 
79
80
  def send_request(method, endpoint, headers, data=nil)
80
81
  begin
81
- response = RestClient::Request.execute(:method => :post,
82
- :url => endpoint,
83
- :payload => data,
84
- :headers => headers,
85
- :timeout => 0.1)
82
+ if @async
83
+ response = RestClient::Request.execute(:method => :post,
84
+ :url => endpoint,
85
+ :payload => data,
86
+ :headers => headers)
87
+ else
88
+ response = RestClient::Request.execute(:method => :post,
89
+ :url => endpoint,
90
+ :payload => data,
91
+ :headers => headers,
92
+ :timeout => 0.1)
93
+ end
86
94
  rescue => e
87
95
  logger("there was an error transmitting your entry: #{$!}")
88
96
  return nil
@@ -3,7 +3,37 @@ module AnalyticoController
3
3
  private
4
4
 
5
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)
6
+ if cookies
7
+ if cookies[:analytico]
8
+ analytico_cookie = cookies[:analytico]
9
+ else
10
+ analytico_cookie = generate_cookie
11
+ cookies[:analytico] = {
12
+ :value => analytico_cookie,
13
+ :expires => 2.years.from_now,
14
+ :domain => request.host
15
+ }
16
+ end
17
+
18
+ Analytico::Client.add_impression(:fqdn => request.host,
19
+ :ip => request.ip,
20
+ :user_agent => request.user_agent,
21
+ :referrer => request.referrer,
22
+ :url => request.path,
23
+ :cookie => analytico_cookie)
24
+ else
25
+ Analytico::Client.add_impression(:fqdn => request.host,
26
+ :ip => request.ip,
27
+ :user_agent => request.user_agent,
28
+ :referrer => request.referrer,
29
+ :url => request.path)
30
+ end
31
+ end
32
+
33
+ def generate_cookie
34
+ digest = Digest::MD5.new
35
+ digest << "#{Time.now}-#{rand(999999)}"
36
+ digest.to_s
7
37
  end
8
38
 
9
39
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: analytico
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 19
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
- - 2
8
- - 2
9
- version: 0.2.2
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - Mauricio Gomes
@@ -14,7 +15,7 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-11-12 00:00:00 -08:00
18
+ date: 2010-11-13 00:00:00 -05:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
@@ -25,6 +26,7 @@ dependencies:
25
26
  requirements:
26
27
  - - ~>
27
28
  - !ruby/object:Gem::Version
29
+ hash: 13
28
30
  segments:
29
31
  - 0
30
32
  - 7
@@ -40,6 +42,7 @@ dependencies:
40
42
  requirements:
41
43
  - - ~>
42
44
  - !ruby/object:Gem::Version
45
+ hash: 15
43
46
  segments:
44
47
  - 1
45
48
  - 6
@@ -55,6 +58,7 @@ dependencies:
55
58
  requirements:
56
59
  - - ">"
57
60
  - !ruby/object:Gem::Version
61
+ hash: 23
58
62
  segments:
59
63
  - 1
60
64
  - 0
@@ -70,6 +74,7 @@ dependencies:
70
74
  requirements:
71
75
  - - ">="
72
76
  - !ruby/object:Gem::Version
77
+ hash: 3
73
78
  segments:
74
79
  - 0
75
80
  version: "0"
@@ -83,6 +88,7 @@ dependencies:
83
88
  requirements:
84
89
  - - ">="
85
90
  - !ruby/object:Gem::Version
91
+ hash: 3
86
92
  segments:
87
93
  - 0
88
94
  version: "0"
@@ -96,6 +102,7 @@ dependencies:
96
102
  requirements:
97
103
  - - <
98
104
  - !ruby/object:Gem::Version
105
+ hash: 11
99
106
  segments:
100
107
  - 2
101
108
  - 1
@@ -111,6 +118,7 @@ dependencies:
111
118
  requirements:
112
119
  - - <
113
120
  - !ruby/object:Gem::Version
121
+ hash: 15
114
122
  segments:
115
123
  - 2
116
124
  - 0
@@ -155,6 +163,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
155
163
  requirements:
156
164
  - - ">="
157
165
  - !ruby/object:Gem::Version
166
+ hash: 3
158
167
  segments:
159
168
  - 0
160
169
  version: "0"
@@ -163,6 +172,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
172
  requirements:
164
173
  - - ">="
165
174
  - !ruby/object:Gem::Version
175
+ hash: 3
166
176
  segments:
167
177
  - 0
168
178
  version: "0"