feature_channel 0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 971e8ce1d7ec81c7cd48106ebaa3164e03f301192812dfb0ad20051c318d9863
4
+ data.tar.gz: 07c4594c6ebce6ee2a96b2353c55cfbdb59899a02fff29d7526ae91de7ce0192
5
+ SHA512:
6
+ metadata.gz: 64552c9a965c24e777ab20dd7ae8c3769406d40343453ed58b9e6905522f1edd335a5e5dca34c2bf616668618a9be3955d756f6dbc6aa346d36b22eef3eaadaf
7
+ data.tar.gz: 3d034c6596e3f29717b79a1bb847438bdb9e6a233352a14d35add5cc696eead60acf2ede48703d0bce7b4da8f59300c7e2524fc3399c65fbed797df89be1e7a6
data/.gitignore ADDED
@@ -0,0 +1,11 @@
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
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.5.3
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in feature_channel.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 12.0'
9
+ gem 'rspec', '~> 3.0'
data/Gemfile.lock ADDED
@@ -0,0 +1,36 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ feature_channel (0.1.1)
5
+ msgpack (~> 1.3, >= 1.3.3)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.3)
11
+ msgpack (1.3.3)
12
+ rake (12.3.3)
13
+ rspec (3.9.0)
14
+ rspec-core (~> 3.9.0)
15
+ rspec-expectations (~> 3.9.0)
16
+ rspec-mocks (~> 3.9.0)
17
+ rspec-core (3.9.1)
18
+ rspec-support (~> 3.9.1)
19
+ rspec-expectations (3.9.1)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.9.0)
22
+ rspec-mocks (3.9.1)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.9.0)
25
+ rspec-support (3.9.2)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ feature_channel!
32
+ rake (~> 12.0)
33
+ rspec (~> 3.0)
34
+
35
+ BUNDLED WITH
36
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Daniel Vinciguerra
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # FeatureChannel
2
+
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/feature_channel`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'feature_channel'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install feature_channel
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dvinciguerra/feature_channel.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "feature_channel"
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,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/feature_channel/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'feature_channel'
7
+ spec.version = FeatureChannel::VERSION
8
+ spec.authors = ['Daniel Vinciguerra']
9
+ spec.email = ['daniel.vinciguerra@bivee.com.br']
10
+
11
+ spec.summary = 'Connect your features and services'
12
+ spec.description = '
13
+ FeatureChannel is a generic componnet to connect your features and services using diferent strategies
14
+ '
15
+ spec.homepage = 'https://github.com/dvinciguerra'
16
+ spec.license = 'MIT'
17
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
18
+
19
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
20
+
21
+ spec.metadata['homepage_uri'] = spec.homepage
22
+ spec.metadata['source_code_uri'] = spec.homepage
23
+ # spec.metadata['changelog_uri'] = "TODO: Put your gem's CHANGELOG.md URL here."
24
+
25
+ # Specify which files should be added to the gem when it is released.
26
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
28
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
29
+ end
30
+ spec.bindir = 'exe'
31
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ['lib']
33
+
34
+ spec.add_dependency 'msgpack', '~> 1.3', '>= 1.3.3'
35
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'feature_channel/version'
4
+ require 'feature_channel/publisher'
5
+ require 'feature_channel/subscriber'
6
+
7
+ module FeatureChannel
8
+ class Error < StandardError; end
9
+
10
+ class << self
11
+ attr_accessor :topic, :features, :services, :service_name,
12
+ :delivery_strategy
13
+
14
+ def configure
15
+ yield(self)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'msgpack'
4
+
5
+ module FeatureChannel
6
+ module Publisher
7
+ class << self
8
+ def send_message(feature:, id:, type:, payload: nil)
9
+ FeatureChannel.delivery_strategy.publish(
10
+ FeatureChannel.topic,
11
+ MessagePack.pack(
12
+ service: FeatureChannel.service_name,
13
+ feature: feature,
14
+ id: id.to_s,
15
+ type: type,
16
+ payload: payload
17
+ )
18
+ )
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'msgpack'
4
+ require 'feature_channel/subscriber/message_processor'
5
+
6
+ module FeatureChannel
7
+ module Subscriber
8
+ class << self
9
+ def process(topic, message)
10
+ payload = message_decode(message)
11
+ model = feature_model(payload['feature'])
12
+
13
+ return unless accept_message? model
14
+
15
+ processor =
16
+ Subscriber::MessageProcessor.new(message: payload, model: model)
17
+
18
+ processor.run!
19
+ end
20
+
21
+ private
22
+
23
+ def accept_message?(model_klass)
24
+ !!model_klass
25
+ end
26
+
27
+ def feature_model(feature)
28
+ features = FeatureChannel.features
29
+ features[feature] ? Object.const_get(features[feature]) : nil
30
+ end
31
+
32
+ def message_decode(message)
33
+ MessagePack.unpack(message)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FeatureChannel
4
+ module Subscriber
5
+ class MessageProcessor
6
+ OPERATIONS = {
7
+ 'CREATE' => :create_entity,
8
+ 'UPDATE' => :update_entity,
9
+ 'DELETE' => :delete_entity
10
+ }.freeze
11
+
12
+ def initialize(message:, model:)
13
+ @model = model
14
+ @message = message
15
+ end
16
+
17
+ def run!
18
+ method(operation_callback).call(model: @model, params: entity_params)
19
+ end
20
+
21
+ private
22
+
23
+ def entity_params
24
+ @message['payload'].merge('id' => @message['id'])
25
+ end
26
+
27
+ def operation_callback
28
+ OPERATIONS[@message['type']]
29
+ end
30
+
31
+ def create_entity(model:, params:)
32
+ model.create(params.slice(*model.column_names))
33
+ end
34
+
35
+ def update_entity(model:, params:)
36
+ entity = model.find(params['id'])
37
+ entity&.update(params.slice(*model.column_names))
38
+ end
39
+
40
+ def delete_entity(model:, params:)
41
+ entity = model.find(params['id'])
42
+ entity&.delete
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module FeatureChannel
2
+ VERSION = "0.2.0"
3
+ end
data/playground.rb ADDED
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ # publish message
4
+
5
+ require 'feature_channel'
6
+
7
+ features = YAML.safe_load(<<~YAML)
8
+ assets: Asset
9
+ emails: Email
10
+ subscriber: Subscriber
11
+ YAML
12
+
13
+ # feature_channel configuration
14
+ FeatureChannel.configure do |config|
15
+ # define topic name
16
+ config.topic = 'feature_channel'
17
+
18
+ # features configuration
19
+ config.features = features
20
+
21
+ # on receive message
22
+ config.on_receive :message do |feature, payload|
23
+
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: feature_channel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Vinciguerra
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: msgpack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.3.3
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.3.3
33
+ description: "\n FeatureChannel is a generic componnet to connect your features
34
+ and services using diferent strategies\n "
35
+ email:
36
+ - daniel.vinciguerra@bivee.com.br
37
+ executables: []
38
+ extensions: []
39
+ extra_rdoc_files: []
40
+ files:
41
+ - ".gitignore"
42
+ - ".rspec"
43
+ - ".travis.yml"
44
+ - Gemfile
45
+ - Gemfile.lock
46
+ - LICENSE.txt
47
+ - README.md
48
+ - Rakefile
49
+ - bin/console
50
+ - bin/setup
51
+ - feature_channel.gemspec
52
+ - lib/feature_channel.rb
53
+ - lib/feature_channel/publisher.rb
54
+ - lib/feature_channel/subscriber.rb
55
+ - lib/feature_channel/subscriber/message_processor.rb
56
+ - lib/feature_channel/version.rb
57
+ - playground.rb
58
+ homepage: https://github.com/dvinciguerra
59
+ licenses:
60
+ - MIT
61
+ metadata:
62
+ homepage_uri: https://github.com/dvinciguerra
63
+ source_code_uri: https://github.com/dvinciguerra
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 2.3.0
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.7.6
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Connect your features and services
84
+ test_files: []