actor_sync 0.1.3

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e5f9f5654cdec9203414a628d5201e85dde64d52f4abaf9295e344af868b436b
4
+ data.tar.gz: 9a4e76d74e49f1439a92f1227fa6c8836d758a4bf52f6c472088e960222cff58
5
+ SHA512:
6
+ metadata.gz: 899e7fd172a818377a4de0d2b914edbe350be8eac609dd977d3305572ac8b49d4d2aa3777593c6820eb99dc8713fd053a5bd3017c08e20eb8a992a2b487e5335
7
+ data.tar.gz: 4bd1456b2a145087dda48c42efd2945022f63fb534117fd38e88412b3d7b09b03cb04cb0c890d8d2b1cc16d98d9911a8031de49df1063fa199b87f918d645045
@@ -0,0 +1,35 @@
1
+ name: Unit testing
2
+ on:
3
+ pull_request:
4
+ branches:
5
+ - main
6
+
7
+ jobs:
8
+ test:
9
+ name: Ruby 2.7.2
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - name: Check out Repository
13
+ uses: actions/checkout@v2
14
+
15
+ - name: Set up Ruby
16
+ uses: ruby/setup-ruby@v1
17
+ with:
18
+ ruby-version: 2.7.2
19
+
20
+ - name: Set up gems cache
21
+ uses: actions/cache@preview
22
+ with:
23
+ path: vendor/bundle
24
+ key: ${{ runner.os }}-ruby-${{ hashFiles('**/Gemfile.lock') }}
25
+ restore-keys: |
26
+ ${{ runner.os }}-ruby-
27
+
28
+ - name: Install Ruby dependencies
29
+ run: |
30
+ gem install bundler
31
+ bundle config path vendor/bundle
32
+ bundle install --without rubocop --jobs 4 --retry 3
33
+
34
+ - name: Run tests
35
+ run: bundle exec rake test
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ Gemfile.lock
14
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in actor_sync.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 12.0'
9
+ gem 'zeitwerk', '~> 2.4.2'
10
+
11
+ group :development do
12
+ gem 'webmock', '>= 3.8.0'
13
+ end
14
+
15
+ # TODO: Ideally this should not live in the main gem.
16
+ # Adapters
17
+ gem 'mixpanel-ruby'
18
+ gem 'sendgrid-ruby'
19
+ gem 'analytics-ruby'
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Subomi Oluwalana
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # Actor Sync
2
+ Automatically synchronise actor information to your third party systems e.g. Mixpanel, Sendgrid, Segment etc.
3
+
4
+ ## Getting Started
5
+ `config/initializers/actor_sync.rb`
6
+
7
+ ```ruby
8
+ ActorSync.configure do |config|
9
+ config.sync = true
10
+ config.mixpanel = { project_token: '' }
11
+ config.mailchimp = { api_key: '' }
12
+ end
13
+ ```
14
+
15
+ `app/models/user.rb`
16
+ ```ruby
17
+ class User < ApplicationRecord
18
+ include ActorSync
19
+
20
+ actor_sync :mixpanel, user_profile: true
21
+ actor_sync :mailchimp, audience: monthly_mailing_lists
22
+
23
+ def data_to_export_to_mixpanel
24
+ {
25
+ '$first_name': profile.first_name,
26
+ '$last_name': profile.last_name,
27
+ '$email': email
28
+ }
29
+ end
30
+ end
31
+ ```
32
+
33
+ `app/models/company.rb`
34
+ ```ruby
35
+ class Company < ApplicationRecord
36
+ include ActorSync
37
+
38
+ actor_sync :mixpanel, group_profile: true
39
+
40
+ def data_to_export_to_mixpanel
41
+ {
42
+ '$name': name,
43
+ 'products': products_count
44
+ }
45
+ end
46
+ end
47
+
48
+ ```
49
+
50
+ # TODO
51
+ - Write tests.
52
+ - Build plugin system.
53
+ - Add Instrumentation
54
+ - Add support for other activejob backends - Resque etc.
55
+ - Add support for NoSQL Databases
56
+ - Add support for Sinatra (Non-Rails environment)
57
+ - More documentation
58
+ - Add rake tasks to manually sync data
59
+ - Add support for mixpanel lookup tables
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task default: :test
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/actor_sync/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'actor_sync'
7
+ spec.version = ActorSync::VERSION
8
+ spec.authors = ['Subomi']
9
+ spec.email = ['subomioluwalana71@gmail.com']
10
+
11
+ spec.summary = 'Automatically synchronise actor information to your third party systems e.g. Mixpanel, Sendgrid, etc.'
12
+ spec.homepage = 'https://github.com/Subomi/ActorSync'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
14
+
15
+ spec.metadata['homepage_uri'] = spec.homepage
16
+ spec.metadata['source_code_uri'] = spec.homepage
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = 'exe'
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.add_runtime_dependency 'mixpanel-ruby'
28
+ spec.add_runtime_dependency 'activesupport'
29
+
30
+ spec.add_development_dependency 'pry'
31
+ spec.add_development_dependency 'listen', '~> 3.0'
32
+ spec.add_development_dependency 'minitest', '~> 5.14'
33
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "actor_sync"
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActorSync
4
+ module Adapters
5
+ class Intercom
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActorSync
4
+ module Adapters
5
+ module Mixpanel
6
+
7
+ class UnknownActionError < StandardError
8
+ def message
9
+ 'action provided in options isn\'t legal'
10
+ end
11
+ end
12
+
13
+ class DataMethodNotFoundError < StandardError
14
+ def message
15
+ 'actor must define data_to_export_to_mixpanel'
16
+ end
17
+ end
18
+
19
+ class User
20
+ def initialize(distinct_id, tracker, data, action)
21
+ @distinct_id = distinct_id
22
+ @tracker = tracker
23
+ @data = data
24
+ @action = action
25
+ end
26
+
27
+ def call
28
+ create if [:create, :update].include? @action
29
+ destroy if @action == :destroy
30
+ end
31
+
32
+ def create
33
+ @tracker.people.set(@distinct_id, @data)
34
+ end
35
+
36
+ def destroy
37
+ @tracker.people.delete_user(@distinct_id)
38
+ end
39
+ end
40
+
41
+ class Group
42
+ def initialize(group_key, group_id, tracker, data, action)
43
+ @group_key = group_key
44
+ @group_id = group_id
45
+ @tracker = tracker
46
+ @data = data
47
+ @action = action
48
+ end
49
+
50
+ def call
51
+ create if [:create, :update].include? @action
52
+ destroy if @action == :destroy
53
+ end
54
+
55
+ def create
56
+ @tracker.groups.set(@group_key, @group_id, @data)
57
+ end
58
+
59
+ def destroy
60
+ @tracker.groups.delete_group(@group_key, @group_id)
61
+ end
62
+ end
63
+
64
+ class << self
65
+
66
+ def send(actor, options, config)
67
+ @config = config
68
+ action = retrieve_action options
69
+ data = retrieve_data(actor)
70
+
71
+ case
72
+ when !options[:group].nil?
73
+ Group.new(actor.class.name, actor.id, tracker, data, action).call
74
+ else # Default to user profiles.
75
+ User.new(actor.id, tracker, data, action).call
76
+ end
77
+ end
78
+
79
+ private
80
+
81
+ def tracker
82
+ @tracker ||= ::Mixpanel::Tracker.new(@config.mixpanel[:project_token])
83
+ end
84
+
85
+ def retrieve_data(actor)
86
+ raise DataMethodNotFoundError unless actor.respond_to?(:data_to_export_to_mixpanel)
87
+ actor.data_to_export_to_mixpanel
88
+ end
89
+
90
+ def retrieve_action(options)
91
+ action = options[:callback_type]
92
+ raise UnknownActionError unless %i(create update destroy).include?(action)
93
+ action
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActorSync
4
+ module Adapters
5
+ class Segment
6
+ def initialize(actor)
7
+ @actor = actor
8
+ end
9
+
10
+ def send
11
+ analytics = Segment::Analytics.new(write_key: ENV['SEGMENT_WRITE_KEY'])
12
+ analytics.identify(@actor.id, traits: data)
13
+ end
14
+
15
+ private
16
+
17
+ def data
18
+ @actor.data_to_segment
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActorSync
4
+ module Adapters
5
+ class Sendgrid
6
+ def initialize(actor)
7
+ @actor = actor
8
+ end
9
+
10
+ def send
11
+ sg = Sendgrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
12
+ sg.client.marketing.contacts.put(request_body: data)
13
+ end
14
+
15
+ def data
16
+ @actor.data_to_sendgrid
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/string'
4
+
5
+ module ActorSync
6
+ module Adapters
7
+ class << self
8
+ def get_adapter_klass(destination)
9
+ klass = "ActorSync::Adapters::#{destination.to_s.camelize}"
10
+ klass.constantize
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ module ActorSync
2
+ class Configuration
3
+
4
+ class << self
5
+ attr_accessor :sync, :mixpanel, :sengrid
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActorSync
4
+ class Sync
5
+ def initialize(actor, destination, opts = {})
6
+ @actor = actor
7
+ @destination = destination
8
+ @options = opts
9
+ end
10
+
11
+ def call
12
+ Worker.perform_later(@actor.class, @actor.id, @destination, @options) if config.sync
13
+ end
14
+
15
+ private
16
+
17
+ def config
18
+ Configuration
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActorSync
4
+ VERSION = "0.1.3"
5
+ end
@@ -0,0 +1,12 @@
1
+ module ActorSync
2
+ class Worker < ::ApplicationJob
3
+ queue_as :default
4
+
5
+ def perform(actor_klass, actor_id, destination, options)
6
+ actor = actor_klass.find(actor_id)
7
+ adapter_klass = Adapters.get_adapter_klass(destination)
8
+
9
+ adapter_klass.send(actor, options, Configuration)
10
+ end
11
+ end
12
+ end
data/lib/actor_sync.rb ADDED
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mixpanel-ruby'
4
+ require 'zeitwerk'
5
+
6
+ GEM_PATH = File.expand_path('..', __dir__)
7
+
8
+ loader = Zeitwerk::Loader.for_gem
9
+ loader.enable_reloading
10
+ loader.setup
11
+
12
+ module ActorSync
13
+ class Error < StandardError; end
14
+
15
+ def self.included(base)
16
+ base.extend ClassMethods
17
+ end
18
+
19
+ def self.configure
20
+ yield Configuration if block_given?
21
+ end
22
+
23
+ module ClassMethods
24
+ # Generate an instance method to be passed to after_commit
25
+ # Call class method after_commit & pass earlier defined instance method.
26
+ def actor_sync(destination, opts = {})
27
+ callback_types = %i(create update destroy)
28
+
29
+ callback_types.each do |callback_name|
30
+ method_name = "export_to_#{destination}_on_#{callback_name}"
31
+
32
+ instance_eval do
33
+ define_method method_name do
34
+ opts[:callback_type] = callback_name
35
+ Sync.new(self, destination, opts).call
36
+ end
37
+ end
38
+
39
+ class_eval <<-METHODS, __FILE__, __LINE__ + 1
40
+ after_commit :#{method_name}, on: :#{callback_name}
41
+ METHODS
42
+ end
43
+ end
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: actor_sync
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Subomi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-06-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mixpanel-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
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'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: listen
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.14'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.14'
83
+ description:
84
+ email:
85
+ - subomioluwalana71@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".github/workflows/unit-tests.yml"
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE
96
+ - README.md
97
+ - Rakefile
98
+ - actor_sync.gemspec
99
+ - bin/console
100
+ - bin/setup
101
+ - lib/actor_sync.rb
102
+ - lib/actor_sync/adapters.rb
103
+ - lib/actor_sync/adapters/intercom.rb
104
+ - lib/actor_sync/adapters/mixpanel.rb
105
+ - lib/actor_sync/adapters/segment.rb
106
+ - lib/actor_sync/adapters/sendgrid.rb
107
+ - lib/actor_sync/configuration.rb
108
+ - lib/actor_sync/sync.rb
109
+ - lib/actor_sync/version.rb
110
+ - lib/actor_sync/worker.rb
111
+ homepage: https://github.com/Subomi/ActorSync
112
+ licenses: []
113
+ metadata:
114
+ homepage_uri: https://github.com/Subomi/ActorSync
115
+ source_code_uri: https://github.com/Subomi/ActorSync
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: 2.3.0
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubygems_version: 3.1.4
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Automatically synchronise actor information to your third party systems e.g.
135
+ Mixpanel, Sendgrid, etc.
136
+ test_files: []