statsmix 0.1.10 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- A Ruby gem for the StatsMix API - http://www.statsmix.com/developers
1
+ A Ruby gem for the StatsMix API - [http://www.statsmix.com/developers](http://www.statsmix.com/developers)
2
2
 
3
3
  ## What is StatsMix?
4
4
 
@@ -8,12 +8,14 @@ StatsMix makes it easy to track, chart, and share application and business metri
8
8
  * View a real-time chart of these application events in StatsMix's web UI
9
9
  * Share the charts with users inside and outside your organization
10
10
  * Create and share custom dashboards that aggregate multiple metrics together
11
- * Example dashboard: http://www.statsmix.com/d/0e788d59208900e7e3bc
12
- * Example embedded dashboard: http://www.statsmix.com/example-embedded
11
+ * Example dashboard: [http://www.statsmix.com/d/0e788d59208900e7e3bc](http://www.statsmix.com/d/0e788d59208900e7e3bc)
12
+ * Example embedded dashboard: [http://www.statsmix.com/example-embedded](http://www.statsmix.com/example-embedded)
13
13
 
14
- To get started, you'll need an API key for StatsMix. You can get a free developer account here: http://www.statsmix.com/try?plan=developer
14
+ To get started, you'll need an API key for StatsMix. You can get a free developer account here: [http://www.statsmix.com/try?plan=developer](http://www.statsmix.com/try?plan=developer)
15
15
 
16
- Full gem documentation is at http://www.statsmix.com/developers/ruby_gem
16
+ Full gem documentation is at [http://www.statsmix.com/developers/ruby_gem](http://www.statsmix.com/developers/ruby_gem)
17
+
18
+ Partner API documentation is at [http://www.statsmix.com/developers/partner_api](http://www.statsmix.com/developers/partner_api)
17
19
 
18
20
  ## Quick Start
19
21
 
@@ -57,7 +59,19 @@ To redirect all stats in dev environment to a test metric:
57
59
 
58
60
  ## More Documentation
59
61
 
60
- The StatsMix gem supports all the methods documented at http://www.statsmix.com/developers/documentation
62
+ The StatsMix gem supports all the methods documented at [http://www.statsmix.com/developers/documentation](http://www.statsmix.com/developers/documentation)
63
+
64
+ ## Partner API
65
+ We recently added ALPHA-LEVEL support for our Partner API, which allows you to provision users and metrics in StatsMix. The methods are:
66
+
67
+
68
+ StatsMix.create_user({})
69
+ StatsMix.update_user(id,{})
70
+ StatsMix.delete_user(id)
71
+
72
+ In all cases, the affected user's api key will be available via `StatsMix.user_api_key`. You can use the api key for updating and deleting users as well. In other words, __there is no need to store another identifier besides the user's api key.__
73
+
74
+ Full Partner API documentation is at [http://www.statsmix.com/developers/partner_api](http://www.statsmix.com/developers/partner_api)
61
75
 
62
76
  ## Contributing to statsmix
63
77
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.10
1
+ 0.2.1
data/lib/statsmix.rb CHANGED
@@ -14,6 +14,9 @@ class StatsMix
14
14
  # Optional: value, options {:generated_at}
15
15
  # Returns: Net::HTTP object
16
16
  def self.track(name, value = nil, options = {})
17
+ if options.respond_to?('with_indifferent_access')
18
+ options = options.with_indifferent_access
19
+ end
17
20
  self.connect('track')
18
21
  @request_uri = @url.path + '.' + @format
19
22
  @request = Net::HTTP::Get.new(@request_uri)
@@ -23,12 +26,7 @@ class StatsMix
23
26
  end
24
27
  @params[:value] = value if value != nil
25
28
  @params.merge!(options)
26
- @params[:meta] =
27
- if @params[:meta] && !@params[:meta].is_a?(String)
28
- if @params[:meta].respond_to?('to_json')
29
- @params[:meta] = @params[:meta].to_json
30
- end
31
- end
29
+ self.check_meta
32
30
  return do_request
33
31
  end
34
32
  # Stats
@@ -40,6 +38,9 @@ class StatsMix
40
38
  # Use start_date and end_date as has keys in the third param to scope the date range of stats based on the generated_at timestamp of a stat
41
39
  # Returns: Net::HTTP object
42
40
  def self.list_stats(metric_id, limit = nil, options = {})
41
+ if options.respond_to?('with_indifferent_access')
42
+ options = options.with_indifferent_access
43
+ end
43
44
  self.connect('stats')
44
45
  @request_uri = @url.path + '.' + @format
45
46
  @request = Net::HTTP::Get.new(@request_uri)
@@ -73,26 +74,34 @@ class StatsMix
73
74
  # Optional: value, params[:generated_at, :meta]
74
75
  # Returns: Net::HTTP object
75
76
  def self.create_stat(metric_id, value = nil, params = {})
77
+ if params.respond_to?('with_indifferent_access')
78
+ params = params.with_indifferent_access
79
+ end
76
80
  connect('stats')
77
81
  @request_uri = @url.path + '.' + @format
78
82
  @request = Net::HTTP::Post.new(@request_uri)
79
83
  @params[:metric_id] = metric_id
80
- @params.merge!(params)
81
84
  @params[:value] = value if value
85
+ @params.merge!(params)
86
+ self.check_meta
82
87
  return do_request
83
88
  end
84
89
 
85
90
  # Update stat
86
91
  #
87
92
  # Required: stat_id
88
- # Optional: value, generated_at, meta
93
+ # Optional: value, params[:generated_at, :meta]
89
94
  # Returns: Net::HTTP object
90
95
  def self.update_stat(stat_id, value = nil, params = {})
96
+ if params.respond_to?('with_indifferent_access')
97
+ params = params.with_indifferent_access
98
+ end
91
99
  connect('stats')
92
100
  @request_uri = @url.path + '/' + stat_id.to_s + '.' + @format
93
101
  @request = Net::HTTP::Put.new(@request_uri)
102
+ @params[:value] = value if value
94
103
  @params.merge!(params)
95
- @params[:value] = value if value != nil
104
+ self.check_meta
96
105
  return do_request
97
106
  end
98
107
 
@@ -144,6 +153,9 @@ class StatsMix
144
153
  # Optional: params[:profile_id, :sharing, :include_in_email]
145
154
  # Returns: Net::HTTP object
146
155
  def self.create_metric(name, params = {})
156
+ if params.respond_to?('with_indifferent_access')
157
+ params = params.with_indifferent_access
158
+ end
147
159
  connect('metrics')
148
160
  @params.merge!(params)
149
161
  @params[:name] = name
@@ -157,7 +169,10 @@ class StatsMix
157
169
  # Required: metric_id
158
170
  # Optional: params[:profile_id, :sharing, :include_in_email]
159
171
  # Returns: Net::HTTP object
160
- def self.update_metric(metric_id, params = {})
172
+ def self.update_metric(metric_id, params = {})
173
+ if params.respond_to?('with_indifferent_access')
174
+ params = params.with_indifferent_access
175
+ end
161
176
  connect('metrics')
162
177
  @params = [] if @params.nil?
163
178
  @params.merge!(params)
@@ -179,6 +194,82 @@ class StatsMix
179
194
  return do_request
180
195
  end
181
196
 
197
+ # partner api methods
198
+ # see http://www.statsmix.com/developers/partner_api
199
+
200
+ # list users
201
+ #
202
+ # Optional: none
203
+ # Returns: Net::HTTP object
204
+ def self.list_users
205
+ connect('partners/users')
206
+ @request_uri = @url.path
207
+ @request = Net::HTTP::Get.new(@request_uri)
208
+ return do_request
209
+ end
210
+
211
+ # Get user
212
+ #
213
+ # Required: id_or_api_key - id or api key of the user to get
214
+ # Optional: none
215
+ # Returns: Net::HTTP object
216
+ def self.get_user(id_or_api_key)
217
+ connect('partners/users')
218
+ @request_uri = @url.path + '/' + id_or_api_key.to_s + '.' + @format
219
+ @request = Net::HTTP::Get.new(@request_uri)
220
+ return do_request
221
+ end
222
+
223
+ # Create user
224
+ #
225
+ # Required: {:email => 'user@example.com'} - if the email already exists in our system, we will return the existing user
226
+ # Optional: :email,:name,:plan,:company,:url, :metrics[]
227
+ # Returns: Net::HTTP object
228
+ def self.create_user(params = {})
229
+ connect('partners/users')
230
+ @request_uri = @url.path + '.' + @format
231
+ @request = Net::HTTP::Post.new(@request_uri)
232
+ @params.merge!(params)
233
+ result = do_request
234
+ unless self.error
235
+ @user_api_key = result.scan(/<api_key>[0-9a-zA-Z]*<\/api_key>/)[0].gsub(/<\/?api_key>/,'')
236
+ end
237
+ return result
238
+ end
239
+
240
+ # Update user
241
+ #
242
+ # Required: id_or_api_key - id or api key of the user to get
243
+ # Optional: params with keys :email,:name,:plan,:company,:url
244
+ # Returns: Net::HTTP object
245
+ def self.update_user(id_or_api_key, params = {})
246
+ connect('partners/users')
247
+ @request_uri = @url.path + '/' + id_or_api_key.to_s + '.' + @format
248
+ @request = Net::HTTP::Put.new(@request_uri)
249
+ @params.merge!(params)
250
+ result = do_request
251
+ unless self.error
252
+ @user_api_key = result.scan(/<api_key>[0-9a-zA-Z]*<\/api_key>/)[0].gsub(/<\/?api_key>/,'')
253
+ end
254
+ return result
255
+ end
256
+
257
+ # Delete user
258
+ #
259
+ # Required: id_or_api_key - id or api key of the user to get
260
+ # Optional: none
261
+ # Returns: Net::HTTP object
262
+ def self.delete_user(id_or_api_key)
263
+ connect('partners/users')
264
+ @request_uri = @url.path + '/' + id_or_api_key.to_s + '.' + @format
265
+ @request = Net::HTTP::Delete.new(@request_uri)
266
+ result = do_request
267
+ unless self.error
268
+ @user_api_key = result.scan(/<api_key>[0-9a-zA-Z]*<\/api_key>/)[0].gsub(/<\/?api_key>/,'')
269
+ end
270
+ return result
271
+ end
272
+
182
273
  # Returns: Net::HTTP object
183
274
  def self.response
184
275
  @response
@@ -212,6 +303,11 @@ class StatsMix
212
303
  @ignore
213
304
  end
214
305
 
306
+ #Return: string
307
+ def self.user_api_key
308
+ @user_api_key
309
+ end
310
+
215
311
  def self.test_metric_name=(name)
216
312
  @test_metric_name = name
217
313
  end
@@ -281,13 +377,17 @@ class StatsMix
281
377
  @request = Hash.new
282
378
  @request["User-Agent"] = @user_agent
283
379
  @params = Hash.new
380
+ if @params.respond_to?('with_indifferent_access')
381
+ @params = @params.with_indifferent_access
382
+ end
284
383
  @params[:api_key] = @api_key
285
384
  end
286
385
 
287
386
  def self.do_request
288
387
  @error = false
289
388
  return if @ignore
290
- @request.set_form_data(@params)
389
+ #had to add code to support properly encoding array values. See http://blog.assimov.net/post/653645115/post-put-arrays-with-ruby-net-http-set-form-data
390
+ self.set_form_data(@params)
291
391
  @response = @connection.request(@request)
292
392
  if @response.is_a?(Net::HTTPClientError)
293
393
  if 'xml' == @format
@@ -302,6 +402,32 @@ class StatsMix
302
402
  end
303
403
  @response.body
304
404
  end
405
+ #based on http://blog.assimov.net/post/653645115/post-put-arrays-with-ruby-net-http-set-form-data
406
+ def self.set_form_data(params, sep = '&')
407
+ @request.body = params.map {|k,v|
408
+ if v.instance_of?(Array)
409
+ v.map {|e| "#{self.urlencode(k.to_s)}[]=#{urlencode(e.to_s)}"}.join(sep)
410
+ else
411
+ "#{self.urlencode(k.to_s)}=#{self.urlencode(v.to_s)}"
412
+ end
413
+ }.join(sep)
414
+ @request.content_type = 'application/x-www-form-urlencoded'
415
+ end
416
+
417
+ def self.urlencode(str)
418
+ str.gsub(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0]) }
419
+ end
420
+
421
+ def self.check_meta
422
+ if @params[:meta] && !@params[:meta].is_a?(String) && !@params[:meta].is_a?(Hash)
423
+ raise "Invalid data . :meta should be a hash or a json-encoded string. You passed an object of type: #{@params[:meta].type}"
424
+ end
425
+ if @params[:meta] && !@params[:meta].is_a?(String)
426
+ if @params[:meta].respond_to?('to_json')
427
+ @params[:meta] = @params[:meta].to_json
428
+ end
429
+ end
430
+ end
305
431
  end
306
432
 
307
433
  StatsMix.api_from_env
data/statsmix.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{statsmix}
8
- s.version = "0.1.10"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tom Markiewicz", "Derek Scruggs"]
12
- s.date = %q{2011-08-29}
12
+ s.date = %q{2011-11-07}
13
13
  s.description = %q{A Ruby gem for the StatsMix API - http://www.statsmix.com/developers}
14
14
  s.email = ["tmarkiewicz@gmail.com", "me@derekscruggs.com"]
15
15
  s.extra_rdoc_files = [
@@ -12,10 +12,21 @@ class TestStatsmix < Test::Unit::TestCase
12
12
  # http://www.rubyinside.com/vcr-a-recorder-for-all-your-tests-http-interactions-4169.html
13
13
  # https://github.com/myronmarston/vcr
14
14
 
15
- should "Track a stat and view the results in xml" do
15
+ should "Track a stat and view the result in xml" do
16
16
  StatsMix.api_key = '59f08613db2691f28afe'
17
+ StatsMix.format = 'xml'
18
+ result = StatsMix.track('Ruby Gem Testing')
19
+ if StatsMix.error
20
+ raise "Error in gem: #{StatsMix.error}"
21
+ end
22
+ assert !StatsMix.error
23
+ puts result
24
+ end
25
+
26
+ should "Track a stat and view the result in json" do
27
+ StatsMix.api_key = '59f08613db2691f28afe'
28
+ StatsMix.format = 'json'
17
29
  result = StatsMix.track('Ruby Gem Testing')
18
- assert_response 200
19
30
  if StatsMix.error
20
31
  raise "Error in gem: #{StatsMix.error}"
21
32
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statsmix
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 2
8
9
  - 1
9
- - 10
10
- version: 0.1.10
10
+ version: 0.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tom Markiewicz
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-08-29 00:00:00 -06:00
19
+ date: 2011-11-07 00:00:00 -07:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency