lab628 1.0.0 → 1.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7637fb458789554095236875089f242cf13652c5
4
- data.tar.gz: 97296a84e24b9cd0b5283ca184ddefb487f252b3
3
+ metadata.gz: f85d8324d42e8dd7ad70812a28d351565445f8d8
4
+ data.tar.gz: 93c817aa260ea017629779075447d0bebae09a57
5
5
  SHA512:
6
- metadata.gz: 5bdb56f142951e25df06b065fbb231fac801f68fd0b37240d935b08cd46b65f33e1322d57debdaf5f9dd0796a987d08f6531e2b59c2eb8264570c41a702b47b2
7
- data.tar.gz: a9d569ee8635ad9727409388df291257aca689f824d0d2bdf870b25054d7a5a8c996ba921eadec71fd4072a19fc1fe9c22ebe796720f227cd1164d10ad4fba74
6
+ metadata.gz: cecd467d9a41d1709fb27849d0cc839ffd7997c42b03b39fd4772fed83c322c27348f0741732003ee815512a197896b6036d6e8697ce4ef87b6a7d3825fbf6a9
7
+ data.tar.gz: 246ac3d34d6a4b07dcf9f16da67e05cdc4c29d7e84a986edb5756d382e1352acbee7cddc4e918fff4fc0a56618a49b5ec200c6707509ce0ac15ee5264ac7236f
data/.gitignore CHANGED
@@ -3,3 +3,4 @@
3
3
  /*.gem
4
4
  /*.iml
5
5
  /bin/
6
+ /coverage
data/.rubocop.yml CHANGED
@@ -3,7 +3,11 @@ AllCops:
3
3
 
4
4
  Metrics/LineLength:
5
5
  Max: 120
6
- Metrics/AbcSize:
7
- Max: 18
8
6
  Metrics/MethodLength:
9
7
  Max: 15
8
+ Style/CaseIndentation:
9
+ Enabled: false
10
+
11
+ Metrics/AbcSize:
12
+ Exclude:
13
+ - 'test/**/*'
data/.travis.yml CHANGED
@@ -12,8 +12,12 @@ rvm:
12
12
  matrix:
13
13
  allow_failures:
14
14
  - rvm: ruby-head
15
+ - rvm: jruby-head
15
16
  fast_finish: true
16
17
  before_install:
17
18
  - gem install bundler
18
19
  before_script:
19
20
  - bundle update
21
+ addons:
22
+ code_climate:
23
+ repo_token: 89440dfae67f9e66f4d10863601d0976d99b2d78ec5732a9895aef4dfa38ba9c
data/README.md CHANGED
@@ -1,32 +1,39 @@
1
1
  # lab628-ruby
2
- [![Build Status](https://travis-ci.org/lab628/lab628-ruby.svg?branch=master)](https://travis-ci.org/lab628/lab628-ruby) [![Dependency Status](https://gemnasium.com/badges/github.com/lab628/lab628-ruby.svg)](https://gemnasium.com/github.com/lab628/lab628-ruby) [![Code Climate](https://codeclimate.com/github/lab628/lab628-ruby/badges/gpa.svg)](https://codeclimate.com/github/lab628/lab628-ruby)
2
+ [![Gem Version](http://img.shields.io/gem/v/lab628.svg?style=flat-square)](https://rubygems.org/gems/lab628)
3
+ [![Build Status](http://img.shields.io/travis/lab628/lab628-ruby.svg?style=flat-square)](https://travis-ci.org/lab628/lab628-ruby)
4
+ [![Dependency Status](http://img.shields.io/gemnasium/lab628/lab628-ruby.svg?style=flat-square)](https://gemnasium.com/lab628/lab628-ruby)
5
+ [![Code Climate](http://img.shields.io/codeclimate/github/lab628/lab628-ruby.svg?style=flat-square)](https://codeclimate.com/github/lab628/lab628-ruby)
6
+ [![Code Climate](http://img.shields.io/codeclimate/coverage/github/lab628/lab628-ruby.svg?style=flat-square)](https://codeclimate.com/github/lab628/lab628-ruby)
3
7
 
4
8
  This is the official Lab628 ruby integration library.
5
9
 
6
- ## Installation
10
+ ### Installation
7
11
 
8
- Add the gem to your Gemfile:
12
+ Add to your Gemfile:
9
13
 
10
14
  ```ruby
11
15
  gem 'lab628'
12
16
  ```
13
17
 
14
- ## Usage
15
-
16
18
  ### Initialization
17
19
 
18
- For configuring a single access key globaly, put this in an initializer (e.g. for Rails, `config/initializers/lab628.rb`):
20
+ Configure a single access key globally (e.g. for Rails in `config/initializers/lab628.rb`):
21
+
19
22
  ```ruby
20
23
  Lab628.api_key = 'your api key'
21
24
  Lab628.secret = 'your shared secret'
22
25
  ```
23
- Both of these values are available in the [**Access Keys** section of the Lab628 dashboard](http://www.lab628.com/credentials), for each of your keys.
24
26
 
25
- To use multiple keys, an instance can be used to supply or override these globals:
27
+ API keys and secrets are available in the [**Access Keys** section of the Lab628 dashboard](http://www.lab628.com/credentials).
28
+
29
+ An instance can be used to supply or override these globals.
30
+
26
31
  ```ruby
27
32
  tracker = Lab628.new(api_key: 'your api key', secret: 'your shared secret')
28
33
  ```
29
34
 
35
+ This is useful if you use multiple keys.
36
+
30
37
  ### Tracking activities
31
38
 
32
39
  You can track any hash:
@@ -35,9 +42,9 @@ You can track any hash:
35
42
  Lab628.activity(user_id: 7, topic: 'excitement')
36
43
  ```
37
44
 
38
- The hash will be converted to JSON, which means Ruby objects that don't correspond to JSON types will be converted to strings.
45
+ The hash will be converted to JSON. Ruby objects in the hash that don't correspond to JSON types will be converted to strings via `.to_s`.
39
46
 
40
- If you have an object that is already in JSON form, you can pass it directly:
47
+ You can also track an activity in JSON form:
41
48
 
42
49
  ```ruby
43
50
  Lab628.activity("{\"this\":\"json\",\"is\":[\"already\",\"encoded\"]}")
@@ -46,5 +53,52 @@ Lab628.activity("{\"this\":\"json\",\"is\":[\"already\",\"encoded\"]}")
46
53
  The same method can be used on instances:
47
54
 
48
55
  ```ruby
49
- Lab628.new(api_key: 'your api key', secret: 'your shared secret').activity(user_id: 7, topic: 'excitement')
56
+ Lab628.new(api_key: 'key', secret: 'secret').activity(user_id: 7, topic: 'excitement')
50
57
  ```
58
+
59
+ ### Querying activities
60
+
61
+ Query using a hash expressed in the Lab628 query language ([**documentation**](http://www.lab628.com/docs/api_access)):
62
+
63
+ ```ruby
64
+ Lab628.query(
65
+ query: [
66
+ { l628_timestamp: '$last_day' },
67
+ { page: 'checkout' }
68
+ ],
69
+ summary: [
70
+ { cart_size: 'avg' },
71
+ { total_price: 'sum' }
72
+ ]
73
+ )
74
+ ```
75
+
76
+ The method returns a response in hash form:
77
+
78
+ ```ruby
79
+ {
80
+ "total" => 152,
81
+ "results" => [
82
+ {
83
+ "event" => "checkout",
84
+ "page" => "checkout",
85
+ "user_id" => "74338979973",
86
+ "region" => "us",
87
+ "cart_size" => 4,
88
+ "total_price" => 128,
89
+ "event_depth" => 1
90
+ },
91
+ ...
92
+ ],
93
+ "summary" => {
94
+ "cart_size" => {
95
+ "avg" => 5.2272727272727275
96
+ },
97
+ "total_price" => {
98
+ "sum" => 6382.0
99
+ }
100
+ }
101
+ }
102
+ ```
103
+
104
+ As with `activity`, the `query` method can also be used on `Lab628` instances.
data/lab628-ruby.gemspec CHANGED
@@ -21,7 +21,8 @@ Gem::Specification.new do |s|
21
21
 
22
22
  s.add_development_dependency 'bundler', ['~> 1.10']
23
23
  s.add_development_dependency 'rake', ['~> 10.0']
24
- s.add_development_dependency 'minitest', ['~> 0']
25
- s.add_development_dependency 'mocha', ['~> 0']
26
- s.add_development_dependency 'pry', ['~> 0']
24
+ s.add_development_dependency 'minitest'
25
+ s.add_development_dependency 'mocha'
26
+ s.add_development_dependency 'pry'
27
+ s.add_development_dependency 'codeclimate-test-reporter'
27
28
  end
@@ -1,3 +1,3 @@
1
1
  class Lab628
2
- VERSION = '1.0.0'.freeze
2
+ VERSION = '1.1.0'.freeze
3
3
  end
data/lib/lab628.rb CHANGED
@@ -17,21 +17,39 @@ class Lab628 # :nodoc:
17
17
  end
18
18
 
19
19
  def send_activity(key, sec, data)
20
+ response = send_command 'track', key, sec, data
21
+ response.status == 200
22
+ end
23
+
24
+ def query(data)
25
+ send_query @api_key, @secret, data
26
+ end
27
+
28
+ def send_query(key, sec, query)
29
+ response = send_command 'query', key, sec, query
30
+ response.status == 200 && JSON.parse(response.body)
31
+ rescue JSON::ParserError
32
+ nil
33
+ end
34
+
35
+ def check(key, sec)
20
36
  raise ConfigurationError, 'Lab628 api key is not set' unless key
21
37
  raise ConfigurationError, 'Lab628 shared secret is not set' unless sec
38
+ end
39
+
40
+ private
22
41
 
23
- response = connection.post do |req|
24
- req.url "/api/track/#{key}"
42
+ def send_command(command, key, sec, data)
43
+ check key, sec
44
+
45
+ connection.post do |req|
46
+ req.url "/api/#{command}/#{key}"
25
47
  req.headers['X-Secret'] = sec
26
48
  req.headers['Content-Type'] = 'application/json'
27
49
  req.body = validate_payload data
28
50
  end
29
-
30
- response.status == 200
31
51
  end
32
52
 
33
- private
34
-
35
53
  def connection
36
54
  @connection ||= Faraday.new url: BASE_URL do |faraday|
37
55
  faraday.request :url_encoded
@@ -40,29 +58,32 @@ class Lab628 # :nodoc:
40
58
  end
41
59
 
42
60
  def validate_payload(payload)
61
+ return JSON.generate payload if payload.is_a? Hash
43
62
  if payload.is_a? String
44
63
  unless JSON.parse(payload).is_a? Hash
45
- raise PayloadError, "Activity payload '#{payload}' is not a single JSON object."
64
+ raise PayloadError, "Payload '#{payload}' is not a single JSON object."
46
65
  end
47
66
  return payload
48
67
  end
49
- return JSON.generate payload if payload.is_a? Hash
50
- raise PayloadError, "Activity payload '#{payload}' is not a JSON string or hash."
68
+ raise PayloadError, "Payload '#{payload}' is not a JSON string or hash."
51
69
  rescue JSON::ParserError => e
52
- raise PayloadError, "Activity payload '#{payload}' is a non-JSON string: #{e.message}"
70
+ raise PayloadError, "Payload '#{payload}' is a non-JSON string: #{e.message}"
53
71
  rescue => e
54
- raise PayloadError, "Error converting activity payload '#{payload}' to JSON: #{e.message}"
72
+ raise PayloadError, "Error converting payload '#{payload}' to JSON: #{e.message}"
55
73
  end
56
74
  end
57
75
 
58
76
  def initialize(opts = {})
59
77
  @api_key = opts[:api_key] || Lab628.api_key
60
- raise ConfigurationError, 'Lab628 api key is not set' unless @api_key
61
78
  @secret = opts[:secret] || Lab628.secret
62
- raise ConfigurationError, 'Lab628 shared secret is not set' unless @secret
79
+ self.class.check @api_key, @secret
63
80
  end
64
81
 
65
82
  def activity(data)
66
- self.class.send_activity(@api_key, @secret, data)
83
+ self.class.send_activity @api_key, @secret, data
84
+ end
85
+
86
+ def query(query)
87
+ self.class.send_query @api_key, @secret, query
67
88
  end
68
89
  end
data/test/lab628_test.rb CHANGED
@@ -51,9 +51,27 @@ class TrackTest < Minitest::Test
51
51
  Lab628.secret = 'abc'
52
52
 
53
53
  assert Lab628.activity(a: 1)
54
+ assert Lab628.new.activity(a: 1)
54
55
 
55
56
  Lab628.api_key = 'failkey'
56
57
 
57
58
  refute Lab628.activity(a: 1)
58
59
  end
60
+
61
+ def test_query
62
+ # missing default key
63
+ assert_raises Lab628::ConfigurationError do
64
+ Lab628.query(query: { a: 1 })
65
+ end
66
+
67
+ Lab628.api_key = '123'
68
+ Lab628.secret = 'abc'
69
+
70
+ assert_equal({ 'a' => 1 }, Lab628.query(query: { a: 1 }))
71
+ assert_equal({ 'a' => 1 }, Lab628.new.query(query: { a: 1 }))
72
+
73
+ Lab628.api_key = 'failkey'
74
+
75
+ refute Lab628.query(query: { a: 1 })
76
+ end
59
77
  end
data/test/test_helper.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require 'codeclimate-test-reporter'
2
+ CodeClimate::TestReporter.start
3
+
1
4
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
5
  require 'lab628'
3
6
 
@@ -7,12 +10,10 @@ require 'mocha/mini_test'
7
10
  def test_connection
8
11
  Faraday.new do |builder|
9
12
  builder.adapter :test do |stubs|
10
- stubs.post 'api/track/123' do
11
- [200, {}, '']
12
- end
13
- stubs.post 'api/track/failkey' do
14
- [422, {}, '']
15
- end
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, {}, ''] }
16
17
  end
17
18
  end
18
19
  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.0.0
4
+ version: 1.1.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-12 00:00:00.000000000 Z
11
+ date: 2016-04-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -56,42 +56,56 @@ dependencies:
56
56
  name: minitest
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: mocha
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: pry
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - "~>"
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - "~>"
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
+ - - ">="
95
109
  - !ruby/object:Gem::Version
96
110
  version: '0'
97
111
  description: Official Ruby library for the Lab628 service.