restful_metrics 1.1.5 → 2.0.0.pre.alpha.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a87120f361df0b33c22f49d2a2706bfc5e4db523
4
- data.tar.gz: dde139c1af427a6e76680bda29578cf3e8fdc0c4
3
+ metadata.gz: 84feeac72efa68de2b75d957db08b0a5de75f867
4
+ data.tar.gz: 05b3a6939decc3c2803057e8b41853405bbeb5d1
5
5
  SHA512:
6
- metadata.gz: 78ecbc502a47d8532acc9baa75681615745342760dd436fde052f904a1f5bb8a91240c3d392ccfb7569acbff062746c99f2c9b81678d09d9204c16763ab1072a
7
- data.tar.gz: 49dc2a581bda4b66ca5e25c4063ad6d33bb109523833f4f0282f3f083dd5834864dfa21f9781f5fe9e23f0c6a9933c28c612314a10826add3e7b210167528d1b
6
+ metadata.gz: 25d9d091c84f1ea4a41b2e99e6de605e665a352f94f9aacc7eadd33d325627a4580b17cec8984795958f9cde9636db0f5d1941351cc7ad838e6b8fcc97f72da9
7
+ data.tar.gz: fc89d8d3578c1eebfdd682988834cf467fe0587ea47e21119796537e1b05dfb6e79849f68ad9a900ce6020880b010f7b9b6422a2adb6c94aadfd17aa31ba38c5
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- restful_metrics (1.1.5)
4
+ restful_metrics (2.0.0.pre.alpha.1)
5
5
  rest-client (~> 1.6.0)
6
6
  yajl-ruby (>= 0.8.1)
7
7
 
@@ -25,8 +25,6 @@ GEM
25
25
  i18n (~> 0.5.0)
26
26
  activesupport (3.0.11)
27
27
  builder (2.1.2)
28
- delayed_job (3.0.1)
29
- activesupport (~> 3.0)
30
28
  diff-lcs (1.1.3)
31
29
  erubis (2.6.6)
32
30
  abstract (>= 1.0.0)
@@ -63,7 +61,6 @@ PLATFORMS
63
61
  DEPENDENCIES
64
62
  actionpack (~> 3.0.0)
65
63
  activesupport (~> 3.0.0)
66
- delayed_job (~> 3.0.1)
67
64
  mocha (~> 0.10.4)
68
65
  rack (~> 1.2.5)
69
66
  restful_metrics!
data/README.md CHANGED
@@ -6,10 +6,10 @@ For more detailed instructions, check out our [Dev Center](http://devcenter.rest
6
6
 
7
7
  ## Install
8
8
 
9
- * Version 1.x of this client is deprecated. Please switch to 2.x *
9
+ * Please note the syntax for version 2.x of the Ruby client has changed. *
10
10
 
11
11
  ```
12
- gem install restful_metrics-ruby
12
+ gem install restful_metrics
13
13
  ```
14
14
 
15
15
  ## Configure
@@ -22,8 +22,6 @@ The only step required for initialization is setting your API key. Once it's set
22
22
  RestfulMetrics::Client.set_credentials('214c7da8edd333abc78712313918ffe5')
23
23
  ```
24
24
 
25
- You can skip this step if you've installed the RESTful Metrics Heroku Addon.
26
-
27
25
  ### Disable Flag
28
26
 
29
27
  The client also has an optional flag that prevents the client from actually sending data points to the server. This allows you to keep your RESTful Metrics tracking code in place even in your test enviornments. You can disable the client with the following (by default the client is enabled):
@@ -34,36 +32,34 @@ The client also has an optional flag that prevents the client from actually send
34
32
 
35
33
  We added the optional conditional check in the above example to illustrate how the flag can be set dynamically during your application's launch.
36
34
 
37
- ### Asynchronous Mode
38
-
39
- The client currently has built-in support for Delayed::Job. If you enable the asynchronous flag the client will automatically queue the data point for transmission to the server at a later time. This is highly recommended for applications that are sensitive to latency.
35
+ ### Asynchronous Transmission
40
36
 
41
- ``` ruby
42
- RestfulMetrics::Client.async = true
43
- ```
37
+ You can wrap all your RESTful Metrics calls with Delayed::Job / Resque / Sidekiq, etc.
44
38
 
45
- If you use a worker library other than Delayed::Job, you can wrap all your RESTful Metrics calls. For example, if we were using Resque we might add this to an initializer:
39
+ For example:
46
40
 
47
41
  ``` ruby
48
42
  module CompoundMetric
49
43
  @queue = :metrics
50
44
 
51
- def self.perform(fqdn, name, values, distinct_id = nil)
52
- RestfulMetrics::Client.add_compound_metric(fqdn, name, values, distinct_id)
45
+ def self.perform(fqdn, name, values, timestamp=nil, distinct_id=nil)
46
+ RestfulMetrics::Client.add_compound_metric :app => fqdn,
47
+ :name => name,
48
+ :values => values,
49
+ :occurred_at => timestamp,
50
+ :distinct_id => distinct_id
53
51
  end
54
52
  end
55
53
 
56
54
  module Metric
57
55
  @queue = :metrics
58
56
 
59
- def self.perform(fqdn, name, value, distinct_id = nil)
60
- RestfulMetrics::Client.add_metric(fqdn, name, value, distinct_id)
61
- end
62
- end
63
-
64
- def restful_metrics_add_data_point(metric_type, app, metric, value, distinct_id=nil)
65
- unless RestfulMetrics::Client.disabled?
66
- Resque.enqueue(metric_type, app, metric, value, distinct_id)
57
+ def self.perform(fqdn, name, value, timestamp=nil, distinct_id=nil)
58
+ RestfulMetrics::Client.add_metric :app => fqdn,
59
+ :name => name,
60
+ :value => value,
61
+ :occurred_at => timestamp,
62
+ :distinct_id => distinct_id
67
63
  end
68
64
  end
69
65
  ```
@@ -71,11 +67,13 @@ If you use a worker library other than Delayed::Job, you can wrap all your RESTf
71
67
  We've now separated the metrics and compound metrics into their own queue called `metrics`. To add a compound metric data point, we call:
72
68
 
73
69
  ``` ruby
74
- restful_metrics_add_data_point(CompoundMetric, "myapp.com", "impression", ["apple juice", "orange juice", "soda"], "fe352fe23e823668e23e7")
70
+ Resque.enqueue CompoundMetric, :app => 'myapp.com',
71
+ :name => 'impression',
72
+ :values => ['apple juice', 'orange juice', 'soda'],
73
+ :occurred_at => Time.now,
74
+ :distinct_id => user.uuid
75
75
  ```
76
76
 
77
- Make sure that the `async` flag is set to off. This flag only applies to the built-in Delayed::Job support.
78
-
79
77
  ## Sending Data Points
80
78
 
81
79
  ### Metrics
@@ -87,12 +85,13 @@ Attribute | Value
87
85
  Application Identifier | "myapp.com"
88
86
  Metric Name | "impression"
89
87
  Value | 1
88
+ Occurred At | Time or DateTime object
90
89
  Distinct User Identifier | "fe352fe23e823668e23e7"
91
90
 
92
91
  You would transmit this data point with the following:
93
92
 
94
93
  ``` ruby
95
- RestfulMetrics::Client.add_metric("myapp.com", "impression", 1, "fe352fe23e823668e23e7")
94
+ RestfulMetrics::Client.add_metric(:app => "myapp.com", :name => "impression", :value => 1, :occurred_at => Time.now, :distinct_id => "fe352fe23e823668e23e7")
96
95
  ```
97
96
 
98
97
  ### Compound Metrics
@@ -104,12 +103,13 @@ Attribute | Value
104
103
  Application Identifier | "myapp.com"
105
104
  Compound Metric Name | "beverage_search"
106
105
  Values | ["apple juice", "orange juice", "soda"]
106
+ Occurred At | Time or DateTime object
107
107
  Distinct User Identifier | "fe352fe23e823668e23e7"
108
108
 
109
109
  You would transmit this data point with the following:
110
110
 
111
111
  ``` ruby
112
- RestfulMetrics::Client.add_compound_metric("myapp.com", "impression", ["apple juice", "orange juice", "soda"], "fe352fe23e823668e23e7")
112
+ RestfulMetrics::Client.add_compound_metric(:app => "myapp.com", :name => "impression", :values => ["apple juice", "orange juice", "soda"], :occurred_at => Time.now, :distinct_id => "fe352fe23e823668e23e7")
113
113
  ```
114
114
 
115
115
  ## Copyright
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.5
1
+ 2.0.0-alpha.1
@@ -5,7 +5,7 @@ module RestfulMetrics
5
5
  extend LogTools
6
6
 
7
7
  @@connection = nil
8
- @@debug, @@async, @@disabled = false
8
+ @@debug, @@disabled = false
9
9
 
10
10
  class << self
11
11
 
@@ -24,18 +24,6 @@ module RestfulMetrics
24
24
  @@debug
25
25
  end
26
26
 
27
- def async=(async_flag)
28
- # DelayedJob integration
29
- require 'delayed_job' if async_flag
30
-
31
- @@async = async_flag && (defined?(Delayed) != nil)
32
- @@connection.async = @@async if @@connection
33
- end
34
-
35
- def async?
36
- @@async
37
- end
38
-
39
27
  def disabled=(disabled_flag)
40
28
  @@disabled = disabled_flag
41
29
  end
@@ -44,27 +32,48 @@ module RestfulMetrics
44
32
  @@disabled
45
33
  end
46
34
 
47
- def add_metric(fqdn, name, value, distinct_id = nil)
35
+ def add_metric(*args)
36
+ options = extract_options!(args)
37
+
48
38
  params = Hash.new
49
39
  params[:metric] = Hash.new
50
- params[:metric][:fqdn] = fqdn
51
- params[:metric][:name] = name
52
- params[:metric][:value] = value
53
- unless distinct_id.nil?
54
- params[:metric][:distinct_id] = distinct_id
40
+ params[:metric][:fqdn] = options[:app]
41
+ params[:metric][:name] = options[:name]
42
+ params[:metric][:value] = options[:value]
43
+
44
+ unless options[:distinct_id].nil?
45
+ params[:metric][:distinct_id] = options[:distinct_id]
46
+ end
47
+
48
+ if options[:occurred_at].nil?
49
+ params[:metric][:occurred_at] = Time.now.to_i
50
+ else
51
+
52
+ raise InvalidTimestamp unless options[:occurred_at].respond_to?(:to_i)
53
+ params[:metric][:occurred_at] = options[:occurred_at].to_i
55
54
  end
56
55
 
57
56
  post(Endpoint.metrics, params)
58
57
  end
59
58
 
60
- def add_compound_metric(fqdn, name, values, distinct_id = nil)
59
+ def add_compound_metric(*args)
60
+ options = extract_options!(args)
61
+
61
62
  params = Hash.new
62
63
  params[:compound_metric] = Hash.new
63
- params[:compound_metric][:fqdn] = fqdn
64
- params[:compound_metric][:name] = name
65
- params[:compound_metric][:values] = values
66
- unless distinct_id.nil?
67
- params[:compound_metric][:distinct_id] = distinct_id
64
+ params[:compound_metric][:fqdn] = options[:app]
65
+ params[:compound_metric][:name] = options[:name]
66
+ params[:compound_metric][:values] = options[:values]
67
+
68
+ unless options[:distinct_id].nil?
69
+ params[:compound_metric][:distinct_id] = options[:distinct_id]
70
+ end
71
+
72
+ params[:compound_metric][:occurred_at] = if options[:occurred_at].nil?
73
+ Time.now.to_i
74
+ else
75
+ raise InvalidTimestamp unless options[:occurred_at].respond_to?(:to_i)
76
+ options[:occurred_at].to_i
68
77
  end
69
78
 
70
79
  post(Endpoint.compound_metrics, params)
@@ -80,12 +89,7 @@ module RestfulMetrics
80
89
 
81
90
  raise NoConnectionEstablished if @@connection.nil?
82
91
 
83
- if async?
84
- self.delay.transmit(endpoint, data)
85
- true
86
- else
87
- transmit(endpoint, data)
88
- end
92
+ transmit(endpoint, data)
89
93
  end
90
94
 
91
95
  def transmit(endpoint, data)
@@ -55,12 +55,12 @@ module RestfulMetrics
55
55
  if response.nil?
56
56
  logger "There was an error processing the response from Restful Metrics."
57
57
  else
58
- logger "\nresponse: #{response.code}"
59
- logger "headers:"
60
- response.header.each do |key, value|
58
+ logger "Response Code: #{response.code}"
59
+ logger "Response Headers:"
60
+ response.headers.each do |key, value|
61
61
  logger "#{key}=#{value}"
62
62
  end
63
- logger "body:"
63
+ logger "Body:"
64
64
  logger response.body
65
65
  end
66
66
  end
@@ -24,5 +24,6 @@ module RestfulMetrics
24
24
  class InsufficentArguments < RestfulMetricsError; end
25
25
  class InvalidAPIKey < RestfulMetricsError; end
26
26
  class NoConnectionEstablished < RestfulMetricsError; end
27
-
27
+ class InvalidTimestamp < RestfulMetricsError; end
28
+
28
29
  end
@@ -18,7 +18,6 @@ Gem::Specification.new do |s|
18
18
  s.add_development_dependency "actionpack", "~> 3.0.0"
19
19
  s.add_development_dependency "rack", "~> 1.2.5"
20
20
  s.add_development_dependency "activesupport", "~> 3.0.0"
21
- s.add_development_dependency "delayed_job", "~> 3.0.1"
22
21
 
23
22
  s.files = `git ls-files`.split("\n")
24
23
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
data/spec/client_spec.rb CHANGED
@@ -4,13 +4,13 @@ describe "A NON-initialized RESTful Metrics client" do
4
4
 
5
5
  it "should NOT send a metric data point" do
6
6
  lambda {
7
- RestfulMetrics::Client.add_metric("foo.bar.org", "hit", 1)
7
+ RestfulMetrics::Client.add_metric(:app => "foo.bar.org", :name => "hit", :value => 1)
8
8
  }.should raise_error(RestfulMetrics::NoConnectionEstablished)
9
9
  end
10
10
 
11
11
  it "should NOT send a compound metric data point" do
12
12
  lambda {
13
- RestfulMetrics::Client.add_compound_metric("foo.bar.org", "hit", [1,2,3])
13
+ RestfulMetrics::Client.add_compound_metric(:app => "foo.bar.org", :name => "hit", :values => [1,2,3])
14
14
  }.should raise_error(RestfulMetrics::NoConnectionEstablished)
15
15
  end
16
16
 
@@ -23,13 +23,13 @@ describe "A NON-initialized RESTful Metrics client" do
23
23
 
24
24
  it "should NOT send a metric data point" do
25
25
  lambda {
26
- RestfulMetrics::Client.add_metric("foo.bar.org", "hit", 1).should be_true
26
+ RestfulMetrics::Client.add_metric(:app => "foo.bar.org", :name => "hit", :value => 1).should be_true
27
27
  }.should raise_error(RestfulMetrics::NoConnectionEstablished)
28
28
  end
29
29
 
30
30
  it "should NOT send a compound metric data point" do
31
31
  lambda {
32
- RestfulMetrics::Client.add_compound_metric("foo.bar.org", "hit", [1,2,3]).should be_true
32
+ RestfulMetrics::Client.add_compound_metric(:app => "foo.bar.org", :name => "hit", :values => [1,2,3]).should be_true
33
33
  }.should raise_error(RestfulMetrics::NoConnectionEstablished)
34
34
  end
35
35
 
@@ -49,12 +49,12 @@ describe "A disabled RESTful Metrics client" do
49
49
 
50
50
  it "should NOT send a metric if the client is in disabled-mode" do
51
51
  RestfulMetrics::Client.expects(:transmit).never
52
- RestfulMetrics::Client.add_metric("foo.bar.org", "hit", 1).should == false
52
+ RestfulMetrics::Client.add_metric(:app => "foo.bar.org", :name => "hit", :value => 1).should == false
53
53
  end
54
54
 
55
55
  it "should NOT send a compound metric if the client is in disabled-mode" do
56
56
  RestfulMetrics::Client.expects(:transmit).never
57
- RestfulMetrics::Client.add_compound_metric("foo.bar.org", "hit", [1,2,3]).should == false
57
+ RestfulMetrics::Client.add_compound_metric(:app => "foo.bar.org", :name => "hit", :values => [1,2,3]).should == false
58
58
  end
59
59
 
60
60
  end
@@ -71,7 +71,7 @@ describe "An initialized RESTful Metrics client" do
71
71
  200
72
72
  end
73
73
 
74
- def header
74
+ def headers
75
75
  { "one" => "test", "two" => "test" }
76
76
  end
77
77
 
@@ -83,7 +83,6 @@ describe "An initialized RESTful Metrics client" do
83
83
  RestfulMetrics::Client.disabled = false
84
84
  RestClient::Request.any_instance.stubs(:execute).returns(SampleRequest.new)
85
85
  Logger.any_instance.stubs(:warn).returns("") # disable output
86
- Delayed::Job.delete_all
87
86
  end
88
87
 
89
88
  describe "in debug mode" do
@@ -95,11 +94,11 @@ describe "An initialized RESTful Metrics client" do
95
94
  end
96
95
 
97
96
  it "should send a metric to RESTful Metrics while outputting debug info" do
98
- RestfulMetrics::Client.add_metric("foo.bar.org", "hit", 1).should be_true
97
+ RestfulMetrics::Client.add_metric(:app => "foo.bar.org", :name => "hit", :value => 1).should be_true
99
98
  end
100
99
 
101
100
  it "should send the compound metric to RESTful Metrics while outputting debug info" do
102
- RestfulMetrics::Client.add_compound_metric("foo.bar.org", "hit", [1,2,3]).should be_true
101
+ RestfulMetrics::Client.add_compound_metric(:app => "foo.bar.org", :name => "hit", :values => [1,2,3]).should be_true
103
102
  end
104
103
 
105
104
  end
@@ -110,45 +109,73 @@ describe "An initialized RESTful Metrics client" do
110
109
  RestfulMetrics::Client.set_credentials('4ed4ef44e44ed4').should be_true
111
110
  end
112
111
 
113
- it "should set async mode when delayed_job is loaded" do
114
- RestfulMetrics::Client.async = true
115
- RestfulMetrics::Client.async?.should be_true
116
- end
117
-
118
112
  end
119
113
 
120
- describe "adding metrics synchronously" do
121
-
122
- before(:all) do
123
- RestfulMetrics::Client.async = false
124
- RestfulMetrics::Client.async?.should be_false
125
- end
126
-
114
+ describe "adding metrics" do
127
115
  it "should send the metric to RESTful Metrics" do
128
- RestfulMetrics::Client.add_metric("foo.bar.org", "hit", 1).should be_true
116
+ RestfulMetrics::Client.add_metric(:app => "foo.bar.org", :name => "hit", :value => 1, :occurred_at => Time.now).should be_true
129
117
  end
130
118
 
131
119
  it "should send the compound metric to RESTful Metrics" do
132
- RestfulMetrics::Client.add_compound_metric("foo.bar.org", "hit", [1,2,3]).should be_true
120
+ RestfulMetrics::Client.add_compound_metric(:app => "foo.bar.org", :name => "hit", :values => [1,2,3], :occurred_at => Time.now).should be_true
133
121
  end
134
122
 
135
123
  end
136
124
 
137
- describe "adding metrics asynchronously" do
125
+ describe '#add_metric' do
126
+ let(:params) {
127
+ p = {:app => "foo.bar.org", :name => "hit", :value => 1}
128
+ p[:occurred_at] = occurred_at unless occurred_at.blank?
129
+ p
130
+ }
138
131
 
139
- before(:all) do
140
- RestfulMetrics::Client.async = true
141
- RestfulMetrics::Client.async?.should be_true
132
+ context 'when :occurred_at is blank' do
133
+ let(:occurred_at) { nil }
134
+
135
+ it 'fills in the current time' do
136
+ #TODO: Use the same parameter names everywhere. Move translations out to the boundary of our system.
137
+ params_with_timestamp = { :metric => params.merge(:occurred_at => Time.now.to_i) }
138
+ params_with_timestamp[:metric][:fqdn] = params_with_timestamp[:metric].delete(:app)
139
+
140
+ RestfulMetrics::Client.expects(:post).with(RestfulMetrics::Endpoint.metrics, params_with_timestamp)
141
+ RestfulMetrics::Client.add_metric(params)
142
+ end
142
143
  end
144
+ end
143
145
 
144
- it "should create a delayed job for the metric" do
145
- RestfulMetrics::Client.add_metric("foo.bar.org", "hit", 1).should be_true
146
- Delayed::Job.count.should == 1
146
+ describe '#add_compound_metric' do
147
+ let(:params) {
148
+ p = {:app => "foo.bar.org", :name => "hit", :values => [1, 2, 3]}
149
+ p[:occurred_at] = occurred_at unless occurred_at.blank?
150
+ p
151
+ }
152
+
153
+ context 'when :occurred_at is blank' do
154
+ let(:occurred_at) { nil }
155
+
156
+ it 'fills in the current time' do
157
+ #TODO: Use the same parameter names everywhere. Move translations out to the boundary of our system.
158
+ params_with_timestamp = { :compound_metric => params.merge(:occurred_at => Time.now.to_i) }
159
+ params_with_timestamp[:compound_metric][:fqdn] = params_with_timestamp[:compound_metric].delete(:app)
160
+
161
+ RestfulMetrics::Client.expects(:post).with(RestfulMetrics::Endpoint.compound_metrics, params_with_timestamp)
162
+ RestfulMetrics::Client.add_compound_metric(params)
163
+ end
147
164
  end
165
+ end
166
+
167
+ describe "raising errors" do
148
168
 
149
- it "should create a delayed job for the compound metric" do
150
- RestfulMetrics::Client.add_compound_metric("foo.bar.org", "hit", [1,2,3]).should be_true
151
- Delayed::Job.count.should == 1
169
+ it "should NOT accept an invalid occurred_at timestamp when sending metrics" do
170
+ lambda {
171
+ RestfulMetrics::Client.add_metric(:app => "foo.bar.org", :name => "hit", :value => 1, :occurred_at => Date.today)
172
+ }.should raise_error(RestfulMetrics::InvalidTimestamp)
173
+ end
174
+
175
+ it "should NOT accept an invalid occurred_at timestamp when sending compound metrics" do
176
+ lambda {
177
+ RestfulMetrics::Client.add_compound_metric(:app => "foo.bar.org", :name => "hit", :values => [1,2,3], :occurred_at => Date.today).should be_false
178
+ }.should raise_error(RestfulMetrics::InvalidTimestamp)
152
179
  end
153
180
 
154
181
  end
@@ -2,43 +2,79 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "Rails controller integration" do
4
4
 
5
- class ApplicationController < ActionController::Base
5
+ class TestController
6
+ include RestfulMetrics::ControllerMethods::InstanceMethods
6
7
 
7
- before_filter :track_impression
8
-
9
- private
8
+ def cookies
9
+ @cookies ||= {}
10
+ end
10
11
 
11
- def track_impression
12
- RestfulMetrics::Client.add_metric("foo.bar.org", "impression", 1, restful_metrics_cookie)
13
- end
12
+ def request
13
+ require 'ostruct'
14
14
 
15
+ struct = OpenStruct.new
16
+ struct.host = 'foo.com'
17
+ struct
18
+ end
15
19
  end
16
20
 
17
- class TestController < ApplicationController
21
+ let(:test_controller) { TestController.new }
22
+
23
+ describe '#restful_metrics_cookie' do
24
+ let(:result) { test_controller.send(:restful_metrics_cookie) }
18
25
 
19
- def index
20
- @distinct_id = restful_metrics_cookie
21
- RestfulMetrics::Client.add_metric("foo.bar.org", "custom", 1, @distinct_id)
22
- render :text => "OK"
26
+ it 'generates a distinct id' do
27
+ test_controller.expects(:generate_distinct_id).once
28
+ result
23
29
  end
24
30
 
25
- def rescue_action(e); raise(e); end
26
- end
31
+ it 'sets the :restful_metrics cookie' do
32
+ result
33
+ test_controller.cookies[:restful_metrics].should be_present
34
+ end
27
35
 
28
- before(:each) do
29
- ActionDispatch::Request.any_instance.stubs(:cookie_jar).returns({})
30
- RestfulMetrics::Connection.any_instance.stubs(:post).returns(true)
31
- RestfulMetrics::Client.set_credentials('xyz123')
32
- end
36
+ context 'when the :analytico cookie exists' do
37
+ let(:existing_cookie) { '1234123412341234' }
38
+
39
+ before do
40
+ test_controller.cookies[:analytico] = existing_cookie
41
+ end
42
+
43
+ it 'returns the existing cookie' do
44
+ result.should == existing_cookie
45
+ end
46
+ end
33
47
 
34
- it "should track all metrics in the stack" do
35
- RestfulMetrics::Client.expects(:add_metric).twice.returns(true)
48
+ context 'when the :restful_metrics cookie exists' do
49
+ let(:existing_cookie) { '897123408971234' }
36
50
 
37
- env = Rack::MockRequest.env_for('/', :params => {'id' => '1'})
38
- status, headers, body = TestController.action(:index).call(env)
39
- @response = ActionDispatch::TestResponse.new(status, headers, body)
51
+ before do
52
+ test_controller.cookies[:restful_metrics] = existing_cookie
53
+ end
40
54
 
41
- @response.code.should == "200"
55
+ it 'returns the existing cookie' do
56
+ result.should == existing_cookie
57
+ end
58
+ end
42
59
  end
43
60
 
61
+ describe '#generate_distinct_id' do
62
+ let(:test_controller) { TestController.new }
63
+ let(:result) { test_controller.send(:generate_distinct_id) }
64
+
65
+ it 'returns a string' do
66
+ result.class.should == String
67
+ end
68
+
69
+ it 'returns 32 characters' do
70
+ result.length.should == 32
71
+ end
72
+
73
+ it 'returns unique ids' do
74
+ ids = 100.times.collect { test_controller.send(:generate_distinct_id) }
75
+ unique_ids = ids.sort.uniq
76
+
77
+ ids.length.should == unique_ids.length
78
+ end
79
+ end
44
80
  end
data/spec/spec_helper.rb CHANGED
@@ -13,8 +13,6 @@ require 'rspec'
13
13
  require 'mocha'
14
14
  require 'restful_metrics'
15
15
 
16
- require 'delayed_job'
17
-
18
16
  include Rack::Test::Methods
19
17
 
20
18
  # Requires supporting files with custom matchers and macros, etc,
@@ -24,5 +22,3 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
24
22
  RSpec.configure do |config|
25
23
  config.mock_framework = :mocha
26
24
  end
27
-
28
- Delayed::Worker.backend = :mock
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restful_metrics
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.5
4
+ version: 2.0.0.pre.alpha.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mauricio Gomes
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-02-11 00:00:00.000000000 Z
12
+ date: 2015-03-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  version_requirements: !ruby/object:Gem::Requirement
@@ -109,20 +109,6 @@ dependencies:
109
109
  version: 3.0.0
110
110
  prerelease: false
111
111
  name: activesupport
112
- - !ruby/object:Gem::Dependency
113
- version_requirements: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - ~>
116
- - !ruby/object:Gem::Version
117
- version: 3.0.1
118
- type: :development
119
- requirement: !ruby/object:Gem::Requirement
120
- requirements:
121
- - - ~>
122
- - !ruby/object:Gem::Version
123
- version: 3.0.1
124
- prerelease: false
125
- name: delayed_job
126
112
  description: Ruby client for the RESTful Metrics service.
127
113
  email: mauricio@restful-labs.com
128
114
  executables: []
@@ -147,8 +133,6 @@ files:
147
133
  - restful_metrics.gemspec
148
134
  - spec/client_spec.rb
149
135
  - spec/cookie_integration_spec.rb
150
- - spec/delayed/backend/mock.rb
151
- - spec/delayed/serialization/mock.rb
152
136
  - spec/endpoint_spec.rb
153
137
  - spec/spec_helper.rb
154
138
  homepage: http://github.com/restful-labs/resetful_metrics-ruby
@@ -165,9 +149,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
165
149
  version: '0'
166
150
  required_rubygems_version: !ruby/object:Gem::Requirement
167
151
  requirements:
168
- - - '>='
152
+ - - '>'
169
153
  - !ruby/object:Gem::Version
170
- version: '0'
154
+ version: 1.3.1
171
155
  requirements: []
172
156
  rubyforge_project:
173
157
  rubygems_version: 2.4.5
@@ -177,8 +161,6 @@ summary: Ruby client for the RESTful Metrics service.
177
161
  test_files:
178
162
  - spec/client_spec.rb
179
163
  - spec/cookie_integration_spec.rb
180
- - spec/delayed/backend/mock.rb
181
- - spec/delayed/serialization/mock.rb
182
164
  - spec/endpoint_spec.rb
183
165
  - spec/spec_helper.rb
184
166
  has_rdoc:
@@ -1,112 +0,0 @@
1
- require 'ostruct'
2
-
3
- # An in-memory backend suitable only for testing. Tries to behave as if it were an ORM.
4
- module Delayed
5
- module Backend
6
- module Mock
7
- class Job
8
- attr_accessor :id
9
- attr_accessor :priority
10
- attr_accessor :attempts
11
- attr_accessor :handler
12
- attr_accessor :last_error
13
- attr_accessor :run_at
14
- attr_accessor :locked_at
15
- attr_accessor :locked_by
16
- attr_accessor :failed_at
17
- attr_accessor :queue
18
-
19
- include Delayed::Backend::Base
20
-
21
- cattr_accessor :id
22
- self.id = 0
23
-
24
- def initialize(hash = {})
25
- self.attempts = 0
26
- self.priority = 0
27
- self.id = (self.class.id += 1)
28
- hash.each{|k,v| send(:"#{k}=", v)}
29
- end
30
-
31
- @jobs = []
32
- def self.all
33
- @jobs
34
- end
35
-
36
- def self.count
37
- all.size
38
- end
39
-
40
- def self.delete_all
41
- all.clear
42
- end
43
-
44
- def self.create(attrs = {})
45
- new(attrs).tap do |o|
46
- o.save
47
- end
48
- end
49
-
50
- def self.create!(*args); create(*args); end
51
-
52
- def self.clear_locks!(worker_name)
53
- all.select{|j| j.locked_by == worker_name}.each {|j| j.locked_by = nil; j.locked_at = nil}
54
- end
55
-
56
- # Find a few candidate jobs to run (in case some immediately get locked by others).
57
- def self.find_available(worker_name, limit = 5, max_run_time = Worker.max_run_time)
58
- jobs = all.select do |j|
59
- j.run_at <= db_time_now &&
60
- (j.locked_at.nil? || j.locked_at < db_time_now - max_run_time || j.locked_by == worker_name) &&
61
- j.failed_at.nil?
62
- end
63
-
64
- jobs = jobs.select{|j| Worker.queues.include?(j.queue)} if Worker.queues.any?
65
- jobs = jobs.select{|j| j.priority >= Worker.min_priority} if Worker.min_priority
66
- jobs = jobs.select{|j| j.priority <= Worker.max_priority} if Worker.max_priority
67
- jobs.sort_by{|j| [j.priority, j.run_at]}[0..limit-1]
68
- end
69
-
70
- # Lock this job for this worker.
71
- # Returns true if we have the lock, false otherwise.
72
- def lock_exclusively!(max_run_time, worker)
73
- now = self.class.db_time_now
74
- if locked_by != worker
75
- # We don't own this job so we will update the locked_by name and the locked_at
76
- self.locked_at = now
77
- self.locked_by = worker
78
- end
79
-
80
- return true
81
- end
82
-
83
- def self.db_time_now
84
- Time.current
85
- end
86
-
87
- def update_attributes(attrs = {})
88
- attrs.each{|k,v| send(:"#{k}=", v)}
89
- save
90
- end
91
-
92
- def destroy
93
- self.class.all.delete(self)
94
- end
95
-
96
- def save
97
- self.run_at ||= Time.current
98
-
99
- self.class.all << self unless self.class.all.include?(self)
100
- true
101
- end
102
-
103
- def save!; save; end
104
-
105
- def reload
106
- reset
107
- self
108
- end
109
- end
110
- end
111
- end
112
- end
File without changes