lab628 1.1.0 → 1.2.0

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: f85d8324d42e8dd7ad70812a28d351565445f8d8
4
- data.tar.gz: 93c817aa260ea017629779075447d0bebae09a57
3
+ metadata.gz: f991321872a3ffaa675acbd575032405cb54d9a8
4
+ data.tar.gz: 9c59aa387ad459cb1be7a5818c231baff1c519ab
5
5
  SHA512:
6
- metadata.gz: cecd467d9a41d1709fb27849d0cc839ffd7997c42b03b39fd4772fed83c322c27348f0741732003ee815512a197896b6036d6e8697ce4ef87b6a7d3825fbf6a9
7
- data.tar.gz: 246ac3d34d6a4b07dcf9f16da67e05cdc4c29d7e84a986edb5756d382e1352acbee7cddc4e918fff4fc0a56618a49b5ec200c6707509ce0ac15ee5264ac7236f
6
+ metadata.gz: 15e5248146d72698285b6329ecd5f13700064714a0af8af421924c9c5b6171226a422d2af7120fe00e9885e2e8fbc047931bc5837838ea6c02f2dbc5f551af86
7
+ data.tar.gz: fbd5dd2e1c76967b5b24ac4406d5aec41d97a52c198000afe5f490d0ec03e541836c551b346dcd398112f384f9ae3e895824a7d995f411739ab625ba8da8484c
data/.gitignore CHANGED
@@ -1,4 +1,4 @@
1
- /Gemfile.lock
1
+ /*.lock
2
2
  /.idea/
3
3
  /*.gem
4
4
  /*.iml
@@ -7,6 +7,8 @@ Metrics/MethodLength:
7
7
  Max: 15
8
8
  Style/CaseIndentation:
9
9
  Enabled: false
10
+ Style/Documentation:
11
+ Enabled: false
10
12
 
11
13
  Metrics/AbcSize:
12
14
  Exclude:
@@ -9,6 +9,10 @@ rvm:
9
9
  - ruby-head
10
10
  - jruby
11
11
  - jruby-head
12
+ gemfile:
13
+ - Gemfile
14
+ - activejob.gemfile
15
+ - activejob_backport.gemfile
12
16
  matrix:
13
17
  allow_failures:
14
18
  - rvm: ruby-head
@@ -0,0 +1,10 @@
1
+ ### 1.2.0
2
+ * Added sync modes
3
+ * Removed suppression of a rare exception (malformed API response)
4
+
5
+ ### 1.1.0
6
+ * Query capability
7
+ * Fixes
8
+
9
+ ### 1.0.0
10
+ * Initial activity tracking capability
data/Gemfile CHANGED
@@ -1,2 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
  gemspec
3
+
4
+ gem 'pry'
5
+ gem 'codeclimate-test-reporter'
data/README.md CHANGED
@@ -34,6 +34,8 @@ tracker = Lab628.new(api_key: 'your api key', secret: 'your shared secret')
34
34
 
35
35
  This is useful if you use multiple keys.
36
36
 
37
+ See also: [`sync` configuration](#asynchronous-activity-tracking).
38
+
37
39
  ### Tracking activities
38
40
 
39
41
  You can track any hash:
@@ -102,3 +104,36 @@ The method returns a response in hash form:
102
104
  ```
103
105
 
104
106
  As with `activity`, the `query` method can also be used on `Lab628` instances.
107
+
108
+ ### Asynchronous activity tracking
109
+
110
+ Set the `sync` mode on the `Lab628` class or an instance:
111
+ ```ruby
112
+ Lab628.sync = :job
113
+ async_tracker = Lab628.new(api_key: 'key', secret: 'secret', sync: :job)
114
+ ```
115
+
116
+ Available modes:
117
+ * `:inline` (default): tracking is performed synchronously, blocking until a response is received.
118
+ * The `activity` method returns `true` or `false` based on request success.
119
+ * `:job`: an ActiveJob for tracking is placed on the `:lab628` queue.
120
+ * You must independently include one of these gems:
121
+ * [`activejob`](https://github.com/rails/rails/tree/master/activejob), included in Rails 4.2+
122
+ * [`activejob_backport`](https://github.com/ankane/activejob_backport), for Rails 4.0 and 4.1
123
+ * The `activity` method returns the ActiveJob instance.
124
+ * `:thread`: tracking is performed in a concurrent thread using `Thread.new`.
125
+ * The `activity` method returns the Thread instance.
126
+
127
+ ### Contribution
128
+
129
+ Test for your current Ruby version with:
130
+
131
+ ```bash
132
+ bundle
133
+ bundle exec rake
134
+ ```
135
+
136
+ Test for all supported environments using [WWTD](https://github.com/grosser/wwtd),
137
+ which requires all Ruby versions in [the Travis file](.travis.yml) to be installed via [RVM](https://rvm.io/).
138
+
139
+ We are open to discontinuing support for older Ruby versions, if required by significant fixes or new features.
@@ -0,0 +1,2 @@
1
+ eval_gemfile 'Gemfile'
2
+ gem 'activejob'
@@ -0,0 +1,2 @@
1
+ eval_gemfile 'Gemfile'
2
+ gem 'activejob_backport'
@@ -23,6 +23,4 @@ Gem::Specification.new do |s|
23
23
  s.add_development_dependency 'rake', ['~> 10.0']
24
24
  s.add_development_dependency 'minitest'
25
25
  s.add_development_dependency 'mocha'
26
- s.add_development_dependency 'pry'
27
- s.add_development_dependency 'codeclimate-test-reporter'
28
26
  end
@@ -2,23 +2,33 @@ require 'lab628/version'
2
2
  require 'faraday'
3
3
  require 'json'
4
4
 
5
- class Lab628 # :nodoc:
5
+ require 'lab628/post_job' if (Gem::Specification.map(&:name) & %w(activejob activejob_backport)).any?
6
+
7
+ class Lab628
6
8
  BASE_URL = 'http://www.lab628.com'.freeze
7
9
  ConfigurationError = Class.new StandardError
8
10
  PayloadError = Class.new StandardError
11
+ ApiError = Class.new StandardError
12
+
13
+ # default sync mode
14
+ @sync = :inline
9
15
 
10
16
  class << self
11
- # default API configuration
12
17
  attr_accessor :api_key
13
18
  attr_accessor :secret
19
+ attr_accessor :sync
14
20
 
15
21
  def activity(data)
16
- send_activity @api_key, @secret, data
22
+ send_activity @api_key, @secret, data, @sync
17
23
  end
18
24
 
19
- def send_activity(key, sec, data)
20
- response = send_command 'track', key, sec, data
21
- response.status == 200
25
+ def send_activity(key, sec, data, sync_mode)
26
+ response = send_command 'track', key, sec, data, sync_mode
27
+ if sync_mode == :inline
28
+ response.status == 200
29
+ else
30
+ response
31
+ end
22
32
  end
23
33
 
24
34
  def query(data)
@@ -28,8 +38,8 @@ class Lab628 # :nodoc:
28
38
  def send_query(key, sec, query)
29
39
  response = send_command 'query', key, sec, query
30
40
  response.status == 200 && JSON.parse(response.body)
31
- rescue JSON::ParserError
32
- nil
41
+ rescue JSON::ParserError => e
42
+ raise ApiError, e.message
33
43
  end
34
44
 
35
45
  def check(key, sec)
@@ -37,11 +47,7 @@ class Lab628 # :nodoc:
37
47
  raise ConfigurationError, 'Lab628 shared secret is not set' unless sec
38
48
  end
39
49
 
40
- private
41
-
42
- def send_command(command, key, sec, data)
43
- check key, sec
44
-
50
+ def post(command, key, sec, data)
45
51
  connection.post do |req|
46
52
  req.url "/api/#{command}/#{key}"
47
53
  req.headers['X-Secret'] = sec
@@ -50,6 +56,25 @@ class Lab628 # :nodoc:
50
56
  end
51
57
  end
52
58
 
59
+ private
60
+
61
+ def send_command(cmd, key, sec, data, sync_mode = :inline)
62
+ check key, sec
63
+
64
+ args = [cmd, key, sec, data]
65
+ post_fn = -> { post(*args) }
66
+ case sync_mode
67
+ when :inline
68
+ post_fn.call
69
+ when :job
70
+ PostJob.perform_later(*args)
71
+ when :thread
72
+ Thread.new { post_fn.call }
73
+ else
74
+ raise ConfigurationError, "#{sync_mode} is not a valid sync mode"
75
+ end
76
+ end
77
+
53
78
  def connection
54
79
  @connection ||= Faraday.new url: BASE_URL do |faraday|
55
80
  faraday.request :url_encoded
@@ -76,11 +101,12 @@ class Lab628 # :nodoc:
76
101
  def initialize(opts = {})
77
102
  @api_key = opts[:api_key] || Lab628.api_key
78
103
  @secret = opts[:secret] || Lab628.secret
104
+ @sync = opts[:sync] || Lab628.sync
79
105
  self.class.check @api_key, @secret
80
106
  end
81
107
 
82
108
  def activity(data)
83
- self.class.send_activity @api_key, @secret, data
109
+ self.class.send_activity @api_key, @secret, data, @sync
84
110
  end
85
111
 
86
112
  def query(query)
@@ -0,0 +1,13 @@
1
+ require 'active_job'
2
+ # activejob_backport needs Hash#with_indifferent_access
3
+ # TODO check dependencies; maybe just require 'active_support/core_ext/hash/indifferent_access'
4
+ require 'active_support/all'
5
+
6
+ class Lab628
7
+ class PostJob < ActiveJob::Base
8
+ queue_as :lab628
9
+ def perform(command, key, sec, data)
10
+ Lab628.post command, key, sec, data
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  class Lab628
2
- VERSION = '1.1.0'.freeze
2
+ VERSION = '1.2.0'.freeze
3
3
  end
@@ -2,16 +2,18 @@ require 'test_helper'
2
2
 
3
3
  class TrackTest < Minitest::Test
4
4
  def test_configs
5
+ assert_equal :inline, Lab628.sync
6
+
5
7
  # no default key and secret have been supplied
6
- assert_raises Lab628::ConfigurationError do
7
- Lab628.new
8
- end
8
+ assert_raises(Lab628::ConfigurationError) { Lab628.new }
9
9
 
10
10
  Lab628.api_key = 'key'
11
11
  Lab628.secret = 'sec'
12
+ Lab628.sync = :active_job
12
13
 
13
14
  assert_equal 'key', Lab628.instance_variable_get(:@api_key)
14
15
  assert_equal 'sec', Lab628.instance_variable_get(:@secret)
16
+ assert_equal :active_job, Lab628.instance_variable_get(:@sync)
15
17
 
16
18
  activity_tracker = Lab628.new
17
19
  assert_equal 'key', activity_tracker.instance_variable_get(:@api_key)
@@ -58,6 +60,32 @@ class TrackTest < Minitest::Test
58
60
  refute Lab628.activity(a: 1)
59
61
  end
60
62
 
63
+ def test_async_activity
64
+ Lab628.api_key = '123'
65
+ Lab628.secret = 'abc'
66
+
67
+ # thread
68
+
69
+ Lab628.expects(:post).with('track', '123', 'abc', 'a' => 1)
70
+
71
+ Lab628.sync = :thread
72
+
73
+ assert thread = Lab628.activity('a' => 1)
74
+ assert thread.is_a? Thread
75
+ thread.join
76
+
77
+ return unless active_job?
78
+
79
+ # job
80
+
81
+ Lab628.expects(:post).with('track', '123', 'abc', 'a' => 2)
82
+
83
+ Lab628.sync = :job
84
+
85
+ assert job = Lab628.activity('a' => 2)
86
+ assert job.is_a? Lab628::PostJob
87
+ end
88
+
61
89
  def test_query
62
90
  # missing default key
63
91
  assert_raises Lab628::ConfigurationError do
@@ -7,14 +7,18 @@ require 'lab628'
7
7
  require 'minitest/autorun'
8
8
  require 'mocha/mini_test'
9
9
 
10
- def test_connection
11
- Faraday.new do |builder|
12
- builder.adapter :test do |stubs|
13
- stubs.post('api/track/123') { [200, {}, ''] }
14
- stubs.post('api/track/failkey') { [422, {}, ''] }
15
- stubs.post('api/query/123') { [200, {}, '{"a":1}'] }
16
- stubs.post('api/query/failkey') { [422, {}, ''] }
17
- end
10
+ def active_job?
11
+ (Gem::Specification.map(&:name) & %w(activejob activejob_backport)).any?
12
+ end
13
+
14
+ ActiveJob::Base.logger.level = Logger::Severity::ERROR if active_job?
15
+
16
+ TEST_CONNECTION = Faraday.new do |builder|
17
+ builder.adapter :test do |stubs|
18
+ stubs.post('api/track/123') { [200, {}, ''] }
19
+ stubs.post('api/track/failkey') { [422, {}, ''] }
20
+ stubs.post('api/query/123') { [200, {}, '{"a":1}'] }
21
+ stubs.post('api/query/failkey') { [422, {}, ''] }
18
22
  end
19
23
  end
20
24
 
@@ -23,7 +27,8 @@ module Minitest
23
27
  def setup
24
28
  Lab628.api_key = nil
25
29
  Lab628.secret = nil
26
- Lab628.stubs(:connection).returns(test_connection)
30
+ Lab628.sync = :inline
31
+ Lab628.stubs(:connection).returns(TEST_CONNECTION)
27
32
  end
28
33
  end
29
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lab628
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lab628
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-13 00:00:00.000000000 Z
11
+ date: 2016-04-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -80,34 +80,6 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: pry
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
- - !ruby/object:Gem::Dependency
98
- name: codeclimate-test-reporter
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '0'
111
83
  description: Official Ruby library for the Lab628 service.
112
84
  email:
113
85
  - support@lab628.com
@@ -118,11 +90,15 @@ files:
118
90
  - ".gitignore"
119
91
  - ".rubocop.yml"
120
92
  - ".travis.yml"
93
+ - CHANGELOG.md
121
94
  - Gemfile
122
95
  - README.md
123
96
  - Rakefile
97
+ - activejob.gemfile
98
+ - activejob_backport.gemfile
124
99
  - lab628-ruby.gemspec
125
100
  - lib/lab628.rb
101
+ - lib/lab628/post_job.rb
126
102
  - lib/lab628/version.rb
127
103
  - test/lab628_test.rb
128
104
  - test/test_helper.rb