lab628 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -1
- data/.rubocop.yml +2 -0
- data/.travis.yml +4 -0
- data/CHANGELOG.md +10 -0
- data/Gemfile +3 -0
- data/README.md +35 -0
- data/activejob.gemfile +2 -0
- data/activejob_backport.gemfile +2 -0
- data/lab628-ruby.gemspec +0 -2
- data/lib/lab628.rb +40 -14
- data/lib/lab628/post_job.rb +13 -0
- data/lib/lab628/version.rb +1 -1
- data/test/lab628_test.rb +31 -3
- data/test/test_helper.rb +14 -9
- metadata +6 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f991321872a3ffaa675acbd575032405cb54d9a8
|
4
|
+
data.tar.gz: 9c59aa387ad459cb1be7a5818c231baff1c519ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 15e5248146d72698285b6329ecd5f13700064714a0af8af421924c9c5b6171226a422d2af7120fe00e9885e2e8fbc047931bc5837838ea6c02f2dbc5f551af86
|
7
|
+
data.tar.gz: fbd5dd2e1c76967b5b24ac4406d5aec41d97a52c198000afe5f490d0ec03e541836c551b346dcd398112f384f9ae3e895824a7d995f411739ab625ba8da8484c
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
data/.travis.yml
CHANGED
data/CHANGELOG.md
ADDED
data/Gemfile
CHANGED
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.
|
data/activejob.gemfile
ADDED
data/lab628-ruby.gemspec
CHANGED
@@ -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
|
data/lib/lab628.rb
CHANGED
@@ -2,23 +2,33 @@ require 'lab628/version'
|
|
2
2
|
require 'faraday'
|
3
3
|
require 'json'
|
4
4
|
|
5
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
data/lib/lab628/version.rb
CHANGED
data/test/lab628_test.rb
CHANGED
@@ -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
|
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
|
data/test/test_helper.rb
CHANGED
@@ -7,14 +7,18 @@ require 'lab628'
|
|
7
7
|
require 'minitest/autorun'
|
8
8
|
require 'mocha/mini_test'
|
9
9
|
|
10
|
-
def
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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.
|
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.
|
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-
|
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
|