userlist 0.2.2 → 0.3.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
- SHA1:
3
- metadata.gz: f6f2d40e30040c90510fd29c62e3b7738a323c11
4
- data.tar.gz: 3c4a710406464f67038df488ee9d796bb18fe220
2
+ SHA256:
3
+ metadata.gz: 0daab0d50220fa6b5c0d48e80d7f111a8210bf056e0bc3d6a6cdfaf83eaaa3ee
4
+ data.tar.gz: d0affd71246a055a7d9df690e46b367d51843374fa997c8d8966b5305e0f2699
5
5
  SHA512:
6
- metadata.gz: 096e7606aa25d4bdfdb52b96e4f17d0a10fb187d087e90d21dc70c8acf24acf221657e0cf68f0983ed53831083116357279ca9a591a6f3d14de2e48f761c8020
7
- data.tar.gz: e6716c672ca4dd340dd57cc14c5a808e5af74630b5b18de48b8921167392418c5ece53d7f07a94f6535d3dc264ecd6ef11c3be6df7388d996055f99cf0a4c085
6
+ metadata.gz: 38384feae2887a004965247299dd904e98c00171a124e6c0dd3dbdba8cf35dde5bf1bc585a00191d625232ea84b18a0efac9ac850bbde988d949b5e00d1478a0
7
+ data.tar.gz: 4048869ac0a9b1c5f267df46bb33bda34fb6aeb3eac7c0de2af8b88512b9fc16ed60ff2b32193a790696c71cb7285cbd3fd8794c8ceed3cb6f4a3488e6537407
data/.travis.yml CHANGED
@@ -1,5 +1,7 @@
1
- sudo: false
2
1
  language: ruby
3
2
  rvm:
4
- - 2.4.1
5
- before_install: gem install bundler -v 1.15.4
3
+ - 2.2
4
+ - 2.3
5
+ - 2.4
6
+ - 2.5
7
+ - 2.6
data/Gemfile CHANGED
@@ -6,3 +6,4 @@ gemspec
6
6
 
7
7
  gem 'guard-rspec', '~> 4.7'
8
8
  gem 'guard-rubocop', '~> 1.3'
9
+ gem 'rubocop', '= 0.68'
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Userlist
1
+ # Userlist [![Build Status](https://travis-ci.com/userlistio/userlist-ruby.svg?branch=master)](https://travis-ci.com/userlistio/userlist-ruby)
2
2
 
3
3
  Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/userlist`. To experiment with that code, run `bin/console` for an interactive prompt.
4
4
 
@@ -12,8 +12,20 @@ module Userlist
12
12
  @config = Userlist.config.merge(config)
13
13
  end
14
14
 
15
- def post(endpoint, payload = {})
16
- request(endpoint, payload)
15
+ def get(endpoint)
16
+ request(Net::HTTP::Get, endpoint)
17
+ end
18
+
19
+ def post(endpoint, payload = nil)
20
+ request(Net::HTTP::Post, endpoint, payload)
21
+ end
22
+
23
+ def put(endpoint, payload = nil)
24
+ request(Net::HTTP::Put, endpoint, payload)
25
+ end
26
+
27
+ def delete(endpoint)
28
+ request(Net::HTTP::Delete, endpoint)
17
29
  end
18
30
 
19
31
  private
@@ -33,12 +45,12 @@ module Userlist
33
45
  end
34
46
  end
35
47
 
36
- def request(path, payload = {})
37
- request = Net::HTTP::Post.new(path)
48
+ def request(method, path, payload = nil)
49
+ request = method.new(path)
38
50
  request['Accept'] = 'application/json'
39
51
  request['Authorization'] = "Push #{token}"
40
52
  request['Content-Type'] = 'application/json; charset=UTF-8'
41
- request.body = JSON.dump(payload)
53
+ request.body = JSON.dump(payload) if payload
42
54
 
43
55
  logger.debug "Sending #{request.method} to #{URI.join(endpoint, request.path)} with body #{request.body}"
44
56
 
@@ -0,0 +1,16 @@
1
+ module Userlist
2
+ class Push
3
+ class Company < Resource
4
+ def self.endpoint
5
+ '/companies'
6
+ end
7
+
8
+ def initialize(attributes = {})
9
+ raise ArgumentError, 'Missing required attributes hash' unless attributes
10
+ raise ArgumentError, 'Missing required parameter :identifier' unless attributes[:identifier]
11
+
12
+ super
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ module Userlist
2
+ class Push
3
+ class Event < Resource
4
+ def initialize(attributes = {})
5
+ raise ArgumentError, 'Missing required attributes hash' unless attributes
6
+ raise ArgumentError, 'Missing required parameter :name' unless attributes[:name]
7
+ raise ArgumentError, 'Missing required parameter :user' unless attributes[:user]
8
+
9
+ attributes[:occured_at] ||= Time.now
10
+
11
+ super
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,20 @@
1
+ module Userlist
2
+ class Push
3
+ module Operations
4
+ module Create
5
+ module ClassMethods
6
+ def create(payload = {})
7
+ resource = from_payload(payload)
8
+ strategy.call(:post, endpoint, resource.attributes)
9
+ end
10
+
11
+ alias push create
12
+ end
13
+
14
+ def included(base)
15
+ base.extend(ClassMethods)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ module Userlist
2
+ class Push
3
+ module Operations
4
+ module Delete
5
+ module ClassMethods
6
+ def delete(identifier)
7
+ strategy.call(:delete, "#{endpoint}/#{identifier}")
8
+ end
9
+ end
10
+
11
+ def included(base)
12
+ base.extend(ClassMethods)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,28 @@
1
+ module Userlist
2
+ class Push
3
+ class Relation
4
+ def initialize(scope, type, operations = [])
5
+ @scope = scope
6
+ @type = type
7
+
8
+ operations.each { |operation| singleton_class.send(:include, operation::ClassMethods) }
9
+ end
10
+
11
+ attr_reader :scope, :type
12
+
13
+ private
14
+
15
+ def from_payload(payload)
16
+ type.new(payload)
17
+ end
18
+
19
+ def endpoint
20
+ type.endpoint
21
+ end
22
+
23
+ def strategy
24
+ scope.strategy
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,40 @@
1
+ module Userlist
2
+ class Push
3
+ class Resource
4
+ class << self
5
+ def resource_name
6
+ name.split('::')[-1]
7
+ end
8
+
9
+ def endpoint
10
+ "/#{resource_name.downcase}s"
11
+ end
12
+ end
13
+
14
+ attr_reader :attributes
15
+
16
+ def initialize(attributes = {})
17
+ @attributes = attributes
18
+ end
19
+
20
+ def respond_to_missing?(method, include_private = false)
21
+ attribute = method.to_s.sub(/=$/, '')
22
+
23
+ attributes.key?(attribute.to_sym) || super
24
+ end
25
+
26
+ private
27
+
28
+ def method_missing(method, *args, &block)
29
+ if method.to_s =~ /=$/
30
+ attribute = method.to_s.sub(/=$/, '')
31
+ attributes[attribute.to_sym] = args.first
32
+ elsif attributes.key?(method.to_sym)
33
+ attributes[method.to_sym]
34
+ else
35
+ super
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -19,12 +19,12 @@ module Userlist
19
19
 
20
20
  loop do
21
21
  begin
22
- method, url, payload = *queue.pop
22
+ method, *args = *queue.pop
23
23
  break if method == :stop
24
24
 
25
- client.public_send(method, url, payload)
26
- rescue StandardError => exception
27
- logger.error "Failed to deliver payload: [#{exception.class.name}] #{exception.message}"
25
+ client.public_send(method, *args)
26
+ rescue StandardError => e
27
+ logger.error "Failed to deliver payload: [#{e.class.name}] #{e.message}"
28
28
  end
29
29
  end
30
30
 
@@ -0,0 +1,12 @@
1
+ module Userlist
2
+ class Push
3
+ class User < Resource
4
+ def initialize(attributes = {})
5
+ raise ArgumentError, 'Missing required attributes hash' unless attributes
6
+ raise ArgumentError, 'Missing required parameter :identifier' unless attributes[:identifier]
7
+
8
+ super
9
+ end
10
+ end
11
+ end
12
+ end
data/lib/userlist/push.rb CHANGED
@@ -1,10 +1,20 @@
1
1
  require 'userlist/push/client'
2
2
  require 'userlist/push/strategies'
3
3
 
4
+ require 'userlist/push/resource'
5
+ require 'userlist/push/relation'
6
+
7
+ require 'userlist/push/operations/create'
8
+ require 'userlist/push/operations/delete'
9
+
10
+ require 'userlist/push/user'
11
+ require 'userlist/push/company'
12
+ require 'userlist/push/event'
13
+
4
14
  module Userlist
5
15
  class Push
6
16
  class << self
7
- [:event, :track, :user, :identify, :company].each do |method|
17
+ [:event, :track, :user, :identify, :company, :users, :events, :companies].each do |method|
8
18
  define_method(method) { |*args| default_push_instance.send(method, *args) }
9
19
  end
10
20
 
@@ -15,53 +25,38 @@ module Userlist
15
25
  end
16
26
  end
17
27
 
18
- def initialize(config = {})
19
- @config = Userlist.config.merge(config)
20
- @mutex = Mutex.new
28
+ def initialize(configuration = {})
29
+ @config = Userlist.config.merge(configuration)
30
+ @strategy = Userlist::Push::Strategies.strategy_for(config.push_strategy, config)
21
31
  end
22
32
 
23
- def event(payload = {})
24
- with_mutex do
25
- raise ArgumentError, 'Missing required payload hash' unless payload
26
- raise ArgumentError, 'Missing required parameter :name' unless payload[:name]
27
- raise ArgumentError, 'Missing required parameter :user' unless payload[:user]
28
-
29
- payload[:occured_at] ||= Time.now
33
+ attr_reader :config, :strategy
30
34
 
31
- strategy.call(:post, '/events', payload)
32
- end
35
+ def events
36
+ @events ||= Relation.new(self, Event, [Operations::Create])
33
37
  end
34
- alias track event
35
-
36
- def user(payload = {})
37
- with_mutex do
38
- raise ArgumentError, 'Missing required payload hash' unless payload
39
- raise ArgumentError, 'Missing required parameter :identifier' unless payload[:identifier]
40
38
 
41
- strategy.call(:post, '/users', payload)
42
- end
39
+ def users
40
+ @users ||= Relation.new(self, User, [Operations::Create, Operations::Delete])
43
41
  end
44
- alias identify user
45
42
 
46
- def company(payload = {})
47
- with_mutex do
48
- raise ArgumentError, 'Missing required payload hash' unless payload
49
- raise ArgumentError, 'Missing required parameter :identifier' unless payload[:identifier]
50
-
51
- strategy.call(:post, '/companies', payload)
52
- end
43
+ def companies
44
+ @companies ||= Relation.new(self, Company, [Operations::Create, Operations::Delete])
53
45
  end
54
46
 
55
- private
56
-
57
- attr_reader :config
47
+ def event(payload = {})
48
+ events.create(payload)
49
+ end
58
50
 
59
- def strategy
60
- @strategy ||= Userlist::Push::Strategies.strategy_for(config.push_strategy, config)
51
+ def user(payload = {})
52
+ users.create(payload)
61
53
  end
62
54
 
63
- def with_mutex(&block)
64
- @mutex.synchronize(&block)
55
+ def company(payload = {})
56
+ companies.create(payload)
65
57
  end
58
+
59
+ alias track event
60
+ alias identify user
66
61
  end
67
62
  end
@@ -1,3 +1,3 @@
1
1
  module Userlist
2
- VERSION = '0.2.2'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
data/userlist.gemspec CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.required_ruby_version = '>= 2.2'
23
23
 
24
- spec.add_development_dependency 'bundler', '~> 1.15'
24
+ spec.add_development_dependency 'bundler', '>= 1.15'
25
25
  spec.add_development_dependency 'rake', '~> 10.0'
26
26
  spec.add_development_dependency 'rspec', '~> 3.0'
27
27
  spec.add_development_dependency 'webmock', '~> 1.22'
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: userlist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benedikt Deicke
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-03-18 00:00:00.000000000 Z
11
+ date: 2019-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.15'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.15'
27
27
  - !ruby/object:Gem::Dependency
@@ -90,11 +90,18 @@ files:
90
90
  - lib/userlist/logging.rb
91
91
  - lib/userlist/push.rb
92
92
  - lib/userlist/push/client.rb
93
+ - lib/userlist/push/company.rb
94
+ - lib/userlist/push/event.rb
95
+ - lib/userlist/push/operations/create.rb
96
+ - lib/userlist/push/operations/delete.rb
97
+ - lib/userlist/push/relation.rb
98
+ - lib/userlist/push/resource.rb
93
99
  - lib/userlist/push/strategies.rb
94
100
  - lib/userlist/push/strategies/direct.rb
95
101
  - lib/userlist/push/strategies/null.rb
96
102
  - lib/userlist/push/strategies/threaded.rb
97
103
  - lib/userlist/push/strategies/threaded/worker.rb
104
+ - lib/userlist/push/user.rb
98
105
  - lib/userlist/version.rb
99
106
  - userlist.gemspec
100
107
  homepage: http://github.com/userlistio/userlist-ruby
@@ -116,8 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
123
  - !ruby/object:Gem::Version
117
124
  version: '0'
118
125
  requirements: []
119
- rubyforge_project:
120
- rubygems_version: 2.4.5.2
126
+ rubygems_version: 3.0.3
121
127
  signing_key:
122
128
  specification_version: 4
123
129
  summary: Ruby wrapper for the Userlist.io API