analytico 0.2.0 → 0.2.2

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.0
1
+ 0.2.2
@@ -1,3 +1,6 @@
1
+ require 'active_support/core_ext'
2
+ require 'delayed_job'
3
+
1
4
  module Analytico
2
5
 
3
6
  class Client
@@ -51,7 +54,19 @@ module Analytico
51
54
  HashUtils.recursively_symbolize_keys(response)
52
55
  end
53
56
 
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
+
54
65
  def post(endpoint, data=nil)
66
+ unless production_env?
67
+ warn "Analytico: Skipping communication to endpoint #{endpoint} while not in production. Would have sent: #{data.inspect}"
68
+ return nil
69
+ end
55
70
  @@connection.post endpoint, data
56
71
  end
57
72
 
@@ -65,6 +80,12 @@ module Analytico
65
80
  end
66
81
  end
67
82
 
83
+ def production_env?
84
+ return true if defined?(RAILS_ENV) && RAILS_ENV == "production"
85
+ return true if defined?(Rails) && Rails.respond_to?(:env) && Rails.env == "production"
86
+ return false
87
+ end
88
+
68
89
  end
69
90
 
70
91
  end
@@ -0,0 +1,23 @@
1
+ module Delayed
2
+ module Backend
3
+ module Mock
4
+ class Job
5
+ include Delayed::Backend::Base
6
+
7
+ @@job_count = 0
8
+
9
+ def self.create(params)
10
+ @@job_count += 1
11
+ end
12
+
13
+ def self.count
14
+ @@job_count
15
+ end
16
+
17
+ def self.reset
18
+ @@job_count = 0
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,87 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ #
4
+ # Use a mock backend for testing
5
+ #
6
+ Delayed::Worker.backend = :mock
7
+
8
+ #
9
+ # Fake the rails environment for testing
10
+ #
11
+ class Rails; cattr_accessor :env; end
12
+ def rails_env(env, &block)
13
+ old_env = Rails.env
14
+ Rails.env = env
15
+ yield
16
+ Rails.env = old_env
17
+ end
18
+
19
+ describe Analytico::Client do
20
+ before(:each) do
21
+ @connection = Analytico::Connection
22
+ @connection.any_instance.stubs(:post).returns(true)
23
+ @client = Analytico::Client
24
+ @client.set_credentials('xyz123')
25
+ Delayed::Job.reset
26
+ end
27
+
28
+ describe "add_impression" do
29
+ it "should not record an impression without :fqdn" do
30
+ @connection.any_instance.expects(:post).never
31
+ @client.add_impression({}).should be_true
32
+ end
33
+
34
+ it "should not record an impression without :ip" do
35
+ @connection.any_instance.expects(:post).never
36
+ @client.add_impression({}).should be_true
37
+ end
38
+
39
+ it "requires :fqdn and :ip to record an impression" do
40
+ rails_env "production" do
41
+ @connection.any_instance.stubs(:post).returns({"code" => "bar"})
42
+ o = @client.add_impression :fqdn => "foo.bar.org", :ip => "1.2.3.4"
43
+ o.should be_kind_of Hash
44
+ o.should include(:code)
45
+ o[:code].should == "bar"
46
+ end
47
+ end
48
+ end
49
+
50
+ describe "async_impression" do
51
+ it "should create a delayed job to send an impression" do
52
+ @client.async_impression :fqdn => "foo.bar.org", :ip => "1.2.3.4", :bam => "fu"
53
+ Delayed::Job.count.should == 1
54
+ end
55
+ end
56
+
57
+ describe "add_metric" do
58
+ it "should sends a metric to analytico" do
59
+ rails_env "production" do
60
+ @connection.any_instance.expects(:post).at_least_once.returns({})
61
+ @client.add_metric("foo.bar.org", "ding", "dong")
62
+ end
63
+ end
64
+ end
65
+
66
+ describe "async_metric" do
67
+ it "should create a delayed job" do
68
+ @client.async_metric("pop.rocks.com", "snap", "crackle")
69
+ Delayed::Job.count.should == 1
70
+ end
71
+ end
72
+
73
+ describe "post" do
74
+ it "should return nil if NOT in production" do
75
+ rails_env "development" do
76
+ @client.add_metric("foo.bar.org", "bam", "boozle").should be_nil
77
+ end
78
+ end
79
+
80
+ it "should send the request if in production" do
81
+ rails_env "production" do
82
+ @connection.any_instance.stubs(:post).returns(100)
83
+ @client.add_metric("foo.bar.org", "bam", "boozle").should == 100
84
+ end
85
+ end
86
+ end
87
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,10 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
3
4
  require 'analytico'
4
5
  require 'spec'
5
6
  require 'spec/autorun'
6
7
 
7
8
  Spec::Runner.configure do |config|
8
-
9
+ config.mock_with :mocha
9
10
  end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: analytico
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 2
9
- - 0
10
- version: 0.2.0
8
+ - 2
9
+ version: 0.2.2
11
10
  platform: ruby
12
11
  authors:
13
12
  - Mauricio Gomes
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-10-09 00:00:00 -04:00
17
+ date: 2010-11-12 00:00:00 -08:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
@@ -26,7 +25,6 @@ dependencies:
26
25
  requirements:
27
26
  - - ~>
28
27
  - !ruby/object:Gem::Version
29
- hash: 13
30
28
  segments:
31
29
  - 0
32
30
  - 7
@@ -42,7 +40,6 @@ dependencies:
42
40
  requirements:
43
41
  - - ~>
44
42
  - !ruby/object:Gem::Version
45
- hash: 15
46
43
  segments:
47
44
  - 1
48
45
  - 6
@@ -58,7 +55,6 @@ dependencies:
58
55
  requirements:
59
56
  - - ">"
60
57
  - !ruby/object:Gem::Version
61
- hash: 23
62
58
  segments:
63
59
  - 1
64
60
  - 0
@@ -67,21 +63,61 @@ dependencies:
67
63
  type: :runtime
68
64
  version_requirements: *id003
69
65
  - !ruby/object:Gem::Dependency
70
- name: rspec
66
+ name: i18n
71
67
  prerelease: false
72
68
  requirement: &id004 !ruby/object:Gem::Requirement
73
69
  none: false
74
70
  requirements:
75
71
  - - ">="
76
72
  - !ruby/object:Gem::Version
77
- hash: 13
78
73
  segments:
74
+ - 0
75
+ version: "0"
76
+ type: :runtime
77
+ version_requirements: *id004
78
+ - !ruby/object:Gem::Dependency
79
+ name: activesupport
80
+ prerelease: false
81
+ requirement: &id005 !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ type: :runtime
90
+ version_requirements: *id005
91
+ - !ruby/object:Gem::Dependency
92
+ name: delayed_job
93
+ prerelease: false
94
+ requirement: &id006 !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - <
98
+ - !ruby/object:Gem::Version
99
+ segments:
100
+ - 2
79
101
  - 1
102
+ - 0
103
+ version: 2.1.0
104
+ type: :runtime
105
+ version_requirements: *id006
106
+ - !ruby/object:Gem::Dependency
107
+ name: rspec
108
+ prerelease: false
109
+ requirement: &id007 !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - <
113
+ - !ruby/object:Gem::Version
114
+ segments:
80
115
  - 2
81
- - 9
82
- version: 1.2.9
116
+ - 0
117
+ - 0
118
+ version: 2.0.0
83
119
  type: :development
84
- version_requirements: *id004
120
+ version_requirements: *id007
85
121
  description: Ruby client for the Analytico impression and app metric tracking service.
86
122
  email: mgomes@geminisbs.com
87
123
  executables: []
@@ -101,6 +137,8 @@ files:
101
137
  - lib/analytico/endpoint.rb
102
138
  - lib/analytico/hash_utils.rb
103
139
  - lib/analytico/railtie/rack_impression.rb
140
+ - lib/delayed/backend/mock.rb
141
+ - spec/analytico_client_spec.rb
104
142
  - spec/analytico_spec.rb
105
143
  - spec/spec_helper.rb
106
144
  has_rdoc: true
@@ -117,7 +155,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
117
155
  requirements:
118
156
  - - ">="
119
157
  - !ruby/object:Gem::Version
120
- hash: 3
121
158
  segments:
122
159
  - 0
123
160
  version: "0"
@@ -126,7 +163,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
163
  requirements:
127
164
  - - ">="
128
165
  - !ruby/object:Gem::Version
129
- hash: 3
130
166
  segments:
131
167
  - 0
132
168
  version: "0"
@@ -138,5 +174,6 @@ signing_key:
138
174
  specification_version: 3
139
175
  summary: Ruby client for Analytico
140
176
  test_files:
177
+ - spec/analytico_client_spec.rb
141
178
  - spec/analytico_spec.rb
142
179
  - spec/spec_helper.rb