ikibana 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 159f0b53cd7206dff8ab920b5c568a60a05c569278b8c2e0242b099196da4545
4
+ data.tar.gz: 44c22b18228c278a12f5e1612f98d5d8274f4421a9daf6bb63b42a2f2779a206
5
+ SHA512:
6
+ metadata.gz: bf3822dad6c13583cc4acdd700f73cfc31e0bc3609f00829ab6e0121e6c0eb35997e14ee5611a95ca570048802792ff14395733e590e9d1bb0b5bb921848a1aa
7
+ data.tar.gz: bae1f4c53108edda9554f63beb1a3d74333a50af08ccd2f3dc9671c56f5c9b9512de8abea1d5b0ece490aee8f106b966c62abe6955b80c85d1b246700d96dc4e
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,9 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+ NewCops: enable
4
+
5
+ Style/StringLiterals:
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ EnforcedStyle: double_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-11-25
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Aram
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,43 @@
1
+ # Ikibana
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ 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/ikibana`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ ```bash
14
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```bash
20
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```
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 the created tag, 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/[USERNAME]/ikibana. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/ikibana/blob/main/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Ikibana project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ikibana/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/exe/ikibana ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "ikibana"
5
+
6
+ puts "Hello, world!"
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ikibana
4
+ class ConsumerGenerator < Rails::Generators::NamedBase
5
+ source_root File.expand_path("../../templates", __dir__)
6
+
7
+ def create_worker
8
+ template "consumer.rb.erb", File.join("app/ikibana", class_path, "#{file_name}_consumer.rb")
9
+ end
10
+
11
+ def create_worker_spec
12
+ template "consumer_spec.rb.erb", File.join("spec/ikibana", class_path, "#{file_name}_consumer_spec.rb")
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ikibana
4
+ module Generators
5
+ # This class is responsible for creating the initial files in your app folder
6
+ class InstallGenerator < Rails::Generators::Base
7
+ source_root File.expand_path("../../templates", __dir__)
8
+
9
+ def create_directories
10
+ empty_directory "app/ikibana"
11
+ create_file "app/ikibana/.keep"
12
+ empty_directory "spec/ikibana"
13
+ create_file "spec/ikibana/.keep"
14
+ template "nats.yaml.erb", "config/nats.yaml"
15
+ template "configure.rb.erb", "config/initializers/ikibana.rb"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ require 'ikibana/config'
2
+
3
+ Ikibana::Config.configure do |cfg|
4
+ # cfg.logger = Logger.new("log/ikibana.log") or Rails.logger
5
+ end
@@ -0,0 +1,8 @@
1
+ #frozen_string_literal: true
2
+
3
+ class <%= class_name %>Consumer < Ikibana::ApplicationConsumer
4
+ private
5
+ def perform(msg)
6
+ # Do something
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ #frozen_string_literal: true
2
+
3
+ Rspec.describe <%= class_name %>Consumer, type: :consumer do
4
+ pending "add some examples to (or delete) #{__FILE__}"
5
+ end
6
+
@@ -0,0 +1,8 @@
1
+ connection:
2
+ url: <%%= ENV.fetch('NATS_URL', 'nats://localhost:4222') %>
3
+ streams: []
4
+ # Here you define your streams and their subjects
5
+ # https://docs.nats.io/nats-concepts/jetstream/streams all the possible keys to the elements are described here
6
+ # - name: 'EXAMPLE'
7
+ # subjects: ['example.*', 'example.a.*']
8
+ # max_age: 100 * 1000000 # which is 100ms
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "consumer"
4
+
5
+ module Ikibana
6
+ class ApplicationConsumer
7
+ private_class_method def self.inherited(subclass)
8
+ subclass.include Ikibana::Consumer
9
+ end
10
+
11
+ def initialize
12
+ super
13
+ ObjectSpace.define_finalizer(self, self.class.method(:destructor).to_proc)
14
+ end
15
+
16
+ def self.call
17
+ new.call
18
+ end
19
+
20
+ def call
21
+ return if locked?
22
+ return run_in_sync if sync?
23
+
24
+ run_async
25
+ end
26
+
27
+ private
28
+
29
+ def run_async
30
+ Thread.new do
31
+ loop do
32
+ sub.fetch(1).each do |msg|
33
+ perform(msg)
34
+ msg.ack
35
+ end
36
+ rescue NATS::IO::Timeout
37
+ puts "Awaiting messages for #{convert_namespace_to_path}..."
38
+ end
39
+ end
40
+ end
41
+
42
+ def run_in_sync
43
+ # code here
44
+ end
45
+
46
+ def sub
47
+ @sub ||= js.pull_subscribe(convert_namespace_to_path, self.class.to_s.sub("::", "_"))
48
+ end
49
+
50
+ def convert_namespace_to_path
51
+ self.class.to_s.split("::").map(&:downcase).join(".").sub("consumer", "")
52
+ end
53
+
54
+ def logger
55
+ @logger = Ikibana::Config.instance.logger
56
+ end
57
+
58
+ def js
59
+ @js ||= Ikibana::Config.instance.js
60
+ end
61
+
62
+ def locked? = cache.read("#{self.class}_locked")
63
+
64
+ def sync? = cache.read("#{self.class}_sync")
65
+
66
+ def cache
67
+ @cache = Ikibana::Config.instance.cache
68
+ end
69
+
70
+ def perform(msg)
71
+ raise NotImplementedError, "Subclasses must implement a `perform` method"
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+ require "erb"
5
+ require "singleton"
6
+ require "nats/client"
7
+
8
+ module Ikibana
9
+ class ICache
10
+ def write(...) = raise NotImplementedError, "Subclasses must implement a `write` method"
11
+ def read(...) = raise NotImplementedError, "Subclasses must implement a `read` method"
12
+ end
13
+
14
+ # Configuration class for NATS
15
+ class Config
16
+ include Singleton
17
+
18
+ attr_reader :config, :connection_string, :nats, :js
19
+ attr_accessor :logger, :consumers, :cache
20
+
21
+ def initialize(config_file = "config/nats.yaml")
22
+ @config = load_config(config_file)
23
+ @connection_string = @config["connection"]["url"]
24
+ @logger = Logger.new($stdout)
25
+ connect
26
+ create_streams
27
+ rescue NATS::IO::Timeout
28
+ @logger.error("NATS server not responding")
29
+ rescue StandardError => e
30
+ @logger.error("#{e.class}: Error connecting to NATS server: #{e.message}")
31
+ end
32
+
33
+ def self.configure(config_file = "config/nats.yaml")
34
+ yield instance if block_given?
35
+ end
36
+
37
+ private
38
+
39
+ def create_streams
40
+ @config["streams"].each do |stream|
41
+ @js.add_stream(**stream.transform_keys(&:to_sym))
42
+ end
43
+ end
44
+
45
+ def connect
46
+ @nats ||= NATS.connect(@connection_string)
47
+ @js ||= @nats.jetstream
48
+ @logger.debug("Connected to NATS server at #{nats_host}")
49
+ end
50
+
51
+ def nats_host
52
+ @nats_host ||= URI.parse(@connection_string).host
53
+ end
54
+
55
+ def load_config(file)
56
+ raise "Configuration file #{file} not found" unless File.exist?(file)
57
+
58
+ erb = ERB.new(File.read(file))
59
+ YAML.safe_load(erb.result, aliases: true)
60
+ rescue Psych::SyntaxError => e
61
+ raise "YAML syntax error occurred while parsing #{file}: #{e.message}"
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ikibana
4
+ module Consumer
5
+ def self.included(mod)
6
+ mod.extend self
7
+ mod.extend ClassMethods
8
+ mod.include ClassMethods
9
+ end
10
+
11
+ module ClassMethods
12
+ def at_most_once
13
+ cache.write("#{self}_locked", true)
14
+ end
15
+
16
+ def sync(...)
17
+ cache.write("#{self}_locked", false)
18
+ cache.write("#{self}_sync", true)
19
+ end
20
+
21
+ def destructor(...)
22
+ cache.write("#{self}_locked", false)
23
+ end
24
+
25
+ def cache
26
+ Ikibana::Config.instance.cache
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/railtie"
4
+
5
+ module Ikibana
6
+ # This class is a Railtie that allows the gem to be used in a Rails application.
7
+ class Railtie < Rails::Railtie
8
+ railtie_name :active_transaction
9
+
10
+ rake_tasks do
11
+ load "tasks/ikibana_tasks.rake"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ikibana
4
+ VERSION = "0.1.0"
5
+ end
data/lib/ikibana.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "ikibana/version"
4
+ require_relative "ikibana/application_consumer"
5
+ require_relative "ikibana/railtie" if defined?(Rails)
6
+
7
+ module Ikibana
8
+ class Error < StandardError; end
9
+ # Your code goes here...
10
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :active_transaction do
4
+ desc "version"
5
+ task :version do
6
+ puts "Ikibana version: #{ActiveTransaction::VERSION}"
7
+ end
8
+ end
data/sig/ikibana.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Ikibana
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ikibana
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aram
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-11-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nats-pure
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
+ description: |
28
+ Ikibana is a Rails integration with NATS
29
+ It provides a way to use NATS in Rails applications using Rails jobs as producers and workers as consumers.
30
+ email:
31
+ - aramhrptn@hotmail.com
32
+ executables:
33
+ - ikibana
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - ".rspec"
38
+ - ".rubocop.yml"
39
+ - CHANGELOG.md
40
+ - CODE_OF_CONDUCT.md
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - exe/ikibana
45
+ - lib/generators/ikibana/consumer/consumer_generator.rb
46
+ - lib/generators/ikibana/install/install_generator.rb
47
+ - lib/generators/templates/configure.rb.erb
48
+ - lib/generators/templates/consumer.rb.erb
49
+ - lib/generators/templates/consumer_spec.rb.erb
50
+ - lib/generators/templates/nats.yaml.erb
51
+ - lib/ikibana.rb
52
+ - lib/ikibana/application_consumer.rb
53
+ - lib/ikibana/config.rb
54
+ - lib/ikibana/consumer.rb
55
+ - lib/ikibana/railtie.rb
56
+ - lib/ikibana/version.rb
57
+ - lib/tasks/ikibana_tasks.rake
58
+ - sig/ikibana.rbs
59
+ homepage: https://github.com/svck-dev/ikibana
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ homepage_uri: https://github.com/svck-dev/ikibana
64
+ source_code_uri: https://github.com/svck-dev/ikibana
65
+ rubygems_mfa_required: 'true'
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 3.0.0
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubygems_version: 3.4.10
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Ikibana is a Rails integration with NATS
85
+ test_files: []