taroko-drucker 0.1.2
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 +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/README.md +72 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/taroko/drucker.rb +15 -0
- data/lib/taroko/drucker/event.rb +79 -0
- data/lib/taroko/drucker/version.rb +5 -0
- data/taroko-drucker.gemspec +38 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2891f4b095d421bfb7ad1507de341aa591e31af3
|
4
|
+
data.tar.gz: dcb64923143649108fe39c24a859987f213f1c65
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8f3981ac3715105a63718941845796c3f56c3579d994be2ec3909fc4957f5284220a262d2a5f090b47f3053e2534db732d30cde128418d94bab42a110880805a
|
7
|
+
data.tar.gz: e0989c698c51c15eae867120f2f1b32762c6241610cc258bd71910ea0fb13730ccec269ca496e9f4611ef6cea3fda98be11446b53748edee1b4bcc711d10a380
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# Taroko::Drucker
|
2
|
+
|
3
|
+
Ruby client for Drucker event tracking system.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'taroko-drucker'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
## Setup
|
18
|
+
|
19
|
+
Setup your application unique ID into Drucker:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
# config/initializers/drucker.rb
|
23
|
+
Taroko::Drucker.setup do |config|
|
24
|
+
config.project_id = 'YOUR_PROJECT_ID' # must be a UUID
|
25
|
+
end
|
26
|
+
```
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
To send an event to Drucker:
|
31
|
+
```ruby
|
32
|
+
Taroko::Drucker::Event.create(event_type: 'purchase', count: 1)
|
33
|
+
# returns `true` if event sent to Drucker successfully, otherwise `false`.
|
34
|
+
|
35
|
+
# or
|
36
|
+
|
37
|
+
event = Taroko::Drucker.new
|
38
|
+
event.create(event_type: 'purchase', count: 1)
|
39
|
+
# returns `true` if event sent to Drucker successfully, otherwise `false`.
|
40
|
+
```
|
41
|
+
|
42
|
+
`Taroko::Drucker::Event.create` accepts following parameters:
|
43
|
+
|
44
|
+
| Name | Required | Data type | Description | Example |
|
45
|
+
|-----------------|----------|---------------|--------------------------------|---------------------------------------------------------|
|
46
|
+
| event_type | yes | string/symbol | Event type | `:renewal`, `'renewal'`, `:purchase`, `'purchase'`, etc |
|
47
|
+
| count | yes | integer | Event happens times | 1, 2, etc |
|
48
|
+
| user_id | no | UUID | Unique ID of the user | e1cc5111-33db-491c-9e73-fca4953bec34 |
|
49
|
+
| user_attributes | no | hash | Data which describes the user | `{ first_name: 'Jon', last_name: 'Snow' }` |
|
50
|
+
| attributes | no | hash | Data which describes the event | `{ session_id: 'adsfsadf' }` |
|
51
|
+
|
52
|
+
Notice that if you send incorrect type of params, it'll raise `ArgumentError` with messages.
|
53
|
+
|
54
|
+
## Debug mode
|
55
|
+
|
56
|
+
Sometimes you want to know the response from Drucker:
|
57
|
+
```ruby
|
58
|
+
event_agent = Taroko::Drucker::Event.new
|
59
|
+
event_agent.create(params) # returns `false` if event is not record by Drucker correctly.
|
60
|
+
event_agent.error #=> { status: 400, body: 'error message from Drucker server' }
|
61
|
+
```
|
62
|
+
|
63
|
+
## Development
|
64
|
+
|
65
|
+
Run all tests:
|
66
|
+
```
|
67
|
+
bundle exec rake
|
68
|
+
```
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
|
72
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/resumecompanion/drucker-client-ruby.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "taroko/drucker"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module Taroko
|
4
|
+
module Drucker
|
5
|
+
class Event
|
6
|
+
REQUIRED_PARAMS = %w(
|
7
|
+
event_type
|
8
|
+
count
|
9
|
+
)
|
10
|
+
|
11
|
+
attr_reader :error
|
12
|
+
|
13
|
+
def self.create(params)
|
14
|
+
new.create(params)
|
15
|
+
end
|
16
|
+
|
17
|
+
def create(params)
|
18
|
+
stringify_keys_for(params)
|
19
|
+
add_info
|
20
|
+
validate
|
21
|
+
send_request
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
attr_reader :params
|
27
|
+
|
28
|
+
def stringify_keys_for(params)
|
29
|
+
@params = params.inject({}) { |memo, (k,v)| memo[k.to_s] = v; memo } # stringify_key
|
30
|
+
end
|
31
|
+
|
32
|
+
def validate
|
33
|
+
missing_keys = REQUIRED_PARAMS - params.keys
|
34
|
+
v4_uuid_regex = /[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}/
|
35
|
+
|
36
|
+
raise ArgumentError, "Missing params: #{missing_keys.join(', ')}" unless missing_keys.empty?
|
37
|
+
raise ArgumentError, '`project_id` is not a valid UUID, please setup correctly in `config/initializers/drucker.rb`' unless params['project_id'] =~ v4_uuid_regex
|
38
|
+
raise ArgumentError, '`event_type` must be a string or symbol' unless (params['event_type'].is_a?(String) || params['event_type'].is_a?(Symbol))
|
39
|
+
raise ArgumentError, '`event_type` cannot be blank' if params['event_type'].length.zero?
|
40
|
+
raise ArgumentError, '`count` must be an integer' unless params['count'].is_a? Integer
|
41
|
+
raise ArgumentError, '`count` must greater than 0' unless params['count'] > 0
|
42
|
+
raise ArgumentError, '`user_id` is not a valid UUID' if params['user_id'] && params['user_id'] !~ v4_uuid_regex
|
43
|
+
raise ArgumentError, '`user_attributes` must be a hash' if params['user_attributes'] && !params['user_attributes'].is_a?(Hash)
|
44
|
+
raise ArgumentError, '`attributes` must be a hash' if params['attributes'] && !params['attributes'].is_a?(Hash)
|
45
|
+
end
|
46
|
+
|
47
|
+
def add_info
|
48
|
+
params['project_id'] = Taroko::Drucker.project_id
|
49
|
+
params['happen_at'] = defined?(Rails) ? Time.current.to_i : Time.now.to_i
|
50
|
+
end
|
51
|
+
|
52
|
+
def send_request
|
53
|
+
response = Faraday.post(url, params)
|
54
|
+
|
55
|
+
case response.status
|
56
|
+
when 200, 201 then true
|
57
|
+
else
|
58
|
+
@error = { status: response.status, body: response.body }
|
59
|
+
false
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def url
|
64
|
+
if production?
|
65
|
+
# TODO: replace to production url
|
66
|
+
raise NotImplementedMethod, 'please put a production url here'
|
67
|
+
else
|
68
|
+
'http://localhost:3000/api/v1/events'
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def production?
|
73
|
+
return true if defined?(Rails) && Rails.env.production?
|
74
|
+
return true if ENV['RACK_ENV'] == 'production'
|
75
|
+
false
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "taroko/drucker/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "taroko-drucker"
|
8
|
+
spec.version = Taroko::Drucker::VERSION
|
9
|
+
spec.authors = ["Tarokosoftware"]
|
10
|
+
spec.email = ["taroko@tarokosoftware.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Drucker client for Ruby.}
|
13
|
+
spec.description = %q{Send event in Rails application to Drucker.}
|
14
|
+
spec.homepage = "http://tarokosoftware.com"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
# if spec.respond_to?(:metadata)
|
19
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
20
|
+
# else
|
21
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
22
|
+
# "public gem pushes."
|
23
|
+
# end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
f.match(%r{^(test|spec|features)/})
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
spec.add_runtime_dependency "faraday", "~> 0.12"
|
33
|
+
|
34
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
35
|
+
spec.add_development_dependency "pry", "~> 0.10.4"
|
36
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
37
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: taroko-drucker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tarokosoftware
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.12'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.15'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.15'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.10.4
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.10.4
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
description: Send event in Rails application to Drucker.
|
84
|
+
email:
|
85
|
+
- taroko@tarokosoftware.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- bin/console
|
97
|
+
- bin/setup
|
98
|
+
- lib/taroko/drucker.rb
|
99
|
+
- lib/taroko/drucker/event.rb
|
100
|
+
- lib/taroko/drucker/version.rb
|
101
|
+
- taroko-drucker.gemspec
|
102
|
+
homepage: http://tarokosoftware.com
|
103
|
+
licenses: []
|
104
|
+
metadata: {}
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 2.6.8
|
122
|
+
signing_key:
|
123
|
+
specification_version: 4
|
124
|
+
summary: Drucker client for Ruby.
|
125
|
+
test_files: []
|
126
|
+
has_rdoc:
|