action-handle 0.0.3

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: 48a4c26c3c5566cc92a9cec48140a9bfafef6d6926dacb218a50faec79f418ea
4
+ data.tar.gz: adf6551a892d39eb6903607d41c02ff6c1edcf9b5c1b8d8d581be834f19022b0
5
+ SHA512:
6
+ metadata.gz: 8048648e171fe29348afe4f4d47149e6dcb688bd6d2a94beebb6ff7b7c198bcaced0bf14a20e80dfc5f6336fb0bbbccdec1fe31ca8f7ecc9c56ce2c9e700c9ba
7
+ data.tar.gz: bd311b31640de76deea56e6d2ee647db696896e08003b3112b98e84086d319704d7ddf9c1d130d35555d05352efa4614d7db2f7b01d157cda9ce0ad0e6ef0607
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require ./spec/spec_helper
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at inbox@rbviz.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,39 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ action-handle (0.0.3)
5
+ connection_pool
6
+ redis
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ connection_pool (2.2.2)
12
+ diff-lcs (1.3)
13
+ rake (10.5.0)
14
+ redis (4.1.0)
15
+ rspec (3.9.0)
16
+ rspec-core (~> 3.9.0)
17
+ rspec-expectations (~> 3.9.0)
18
+ rspec-mocks (~> 3.9.0)
19
+ rspec-core (3.9.0)
20
+ rspec-support (~> 3.9.0)
21
+ rspec-expectations (3.9.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.9.0)
24
+ rspec-mocks (3.9.0)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.9.0)
27
+ rspec-support (3.9.0)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ action-handle!
34
+ bundler (~> 2.0)
35
+ rake (~> 10.0)
36
+ rspec
37
+
38
+ BUNDLED WITH
39
+ 2.1.0.pre.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 rbviz
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,35 @@
1
+ # ActionHandle
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'action-handle'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install action-handle
18
+
19
+ ## Development
20
+
21
+ 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 interaction prompt that will allow you to experiment.
22
+
23
+ To install this gem onto your local machine, run `bundle exec rake install`.
24
+
25
+ ## Contributing
26
+
27
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rbviz/action-handle. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
28
+
29
+ ## License
30
+
31
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
32
+
33
+ ## Code of Conduct
34
+
35
+ Everyone interacting in the ActionHandle project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/action-handle/blob/master/CODE_OF_CONDUCT.md).
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
@@ -0,0 +1,37 @@
1
+ File.expand_path('lib', __dir__).tap do |lib|
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ end
4
+
5
+ require 'action_handle/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'action-handle'
9
+ spec.version = ActionHandle::VERSION
10
+ spec.authors = ['rbviz']
11
+ spec.email = ['inbox@rbviz.com']
12
+
13
+ spec.summary = 'Backend agnostic resource handle'
14
+ spec.description = 'Allows distributed handle/lock creation and managament'
15
+ spec.homepage = 'https://github.com/rbviz/action-handle'
16
+ spec.license = 'MIT'
17
+
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = spec.homepage
21
+ end
22
+
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ f.match(%r{^(test|spec|features)/})
26
+ end
27
+ end
28
+
29
+ spec.require_paths = %w[lib]
30
+
31
+ spec.add_dependency 'connection_pool'
32
+ spec.add_dependency 'redis'
33
+
34
+ spec.add_development_dependency 'bundler', '~> 2.0'
35
+ spec.add_development_dependency 'rake', '~> 10.0'
36
+ spec.add_development_dependency 'rspec'
37
+ end
data/bin/console ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'action_handle'
5
+
6
+ require 'irb'
7
+
8
+ IRB.start(__FILE__)
data/bin/rspec ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'pathname'
5
+
6
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', Pathname.new(__FILE__).realpath)
7
+
8
+ bundle_binstub = File.expand_path('bundle', __dir__)
9
+
10
+ if File.file?(bundle_binstub)
11
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
12
+ load(bundle_binstub)
13
+ else
14
+ abort <<~TEXT
15
+ Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
16
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.
17
+ TEXT
18
+ end
19
+ end
20
+
21
+ require 'rubygems'
22
+ require 'bundler/setup'
23
+
24
+ load Gem.bin_path('rspec-core', 'rspec')
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ # frozen_string_literal: true
3
+
4
+ set -euo pipefail
5
+ IFS=$'\n\t'
6
+ set -vx
7
+
8
+ bundle install
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'action_handle/version'
4
+ require 'action_handle/configuration'
5
+ require 'action_handle/base'
6
+
7
+ require 'action_handle/adapters/base'
8
+ require 'action_handle/adapters/rails_cache'
9
+ require 'action_handle/adapters/redis_pool'
10
+
11
+ module ActionHandle
12
+ def self.configure
13
+ yield(Configuration)
14
+ end
15
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ module ActionHandle
6
+ module Adapters
7
+ class Base
8
+ extend Forwardable
9
+
10
+ def_delegators Configuration, :logger, :silence_errors
11
+
12
+ def taken?(_key)
13
+ raise NotImplementedError
14
+ end
15
+
16
+ def current?(_key, _value)
17
+ raise NotImplementedError
18
+ end
19
+
20
+ def info(_key)
21
+ raise NotImplementedError
22
+ end
23
+
24
+ def claim(_key, _value, _ttl)
25
+ raise NotImplementedError
26
+ end
27
+
28
+ def expire(_key)
29
+ raise NotImplementedError
30
+ end
31
+
32
+ private
33
+
34
+ def perform_with_expectation(expected_response, &block)
35
+ safely_perform(&block) == expected_response
36
+ end
37
+
38
+ def safely_perform
39
+ yield
40
+ rescue StandardError => e
41
+ logger.call(e) if logger.respond_to?(:call)
42
+
43
+ raise unless silence_errors == true
44
+
45
+ false
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionHandle
4
+ module Adapters
5
+ class RailsCache < Base
6
+ def taken?(key)
7
+ perform_with_expectation(true) do
8
+ client.exist?(key)
9
+ end
10
+ end
11
+
12
+ def current?(key, value)
13
+ perform_with_expectation(value.to_s) do
14
+ client.read(key)
15
+ end
16
+ end
17
+
18
+ def info(key)
19
+ safely_perform { client.read(key) }
20
+ end
21
+
22
+ def claim(key, value, ttl)
23
+ perform_with_expectation(true) do
24
+ client.write(key, value, expires_in: ttl)
25
+ end
26
+ end
27
+
28
+ def expire(key)
29
+ perform_with_expectation(true) do
30
+ client.delete(key)
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def client
37
+ ::Rails.cache
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'redis'
4
+ require 'connection_pool'
5
+
6
+ module ActionHandle
7
+ module Adapters
8
+ class RedisPool < Base
9
+ CurrentRedisWapper = Class.new do
10
+ def with
11
+ yield(Redis.current)
12
+ end
13
+ end
14
+
15
+ def initialize(pool = nil)
16
+ @pool = pool || CurrentRedisWapper.new
17
+ end
18
+
19
+ def taken?(key)
20
+ perform_with_expectation(true) do
21
+ @pool.with { |client| client.exist(key) }
22
+ end
23
+ end
24
+
25
+ def current?(key, value)
26
+ perform_with_expectation(value.to_s) do
27
+ @pool.with { |client| client.get(key) }
28
+ end
29
+ end
30
+
31
+ def info(key)
32
+ safely_perform do
33
+ @pool.with { |client| client.get(key) }
34
+ end
35
+ end
36
+
37
+ def claim(key, value, ttl)
38
+ perform_with_expectation('OK') do
39
+ @pool.with do |client|
40
+ client.set(key, value, ex: ttl)
41
+ end
42
+ end
43
+ end
44
+
45
+ def expire(key)
46
+ perform_with_expectation(1) do
47
+ @pool.with { |client| client.del(key) }
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ module ActionHandle
6
+ class Base
7
+ extend Forwardable
8
+
9
+ def_delegator Configuration, :adapter
10
+
11
+ def taken?
12
+ adapter.taken?(key)
13
+ end
14
+
15
+ def current?
16
+ adapter.current?(key, value)
17
+ end
18
+
19
+ def info
20
+ adapter.info(key)
21
+ end
22
+
23
+ def claim
24
+ adapter.claim(key, value, ttl)
25
+ end
26
+
27
+ def expire
28
+ adapter.expire(key)
29
+ end
30
+
31
+ def ttl
32
+ defined?(self.class::TTL) ? self.class::TTL : 100
33
+ end
34
+
35
+ def value
36
+ true
37
+ end
38
+
39
+ def key
40
+ raise NotImplementedError, 'must define `key`'
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionHandle
4
+ module Configuration
5
+ class << self
6
+ attr_accessor :adapter, :logger, :silence_errors
7
+ end
8
+
9
+ module_function
10
+
11
+ def adapter
12
+ @adapter ||= Adapters::RedisPool.new
13
+ end
14
+
15
+ def silence_errors
16
+ @silence_errors ||= :no
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionHandle
4
+ VERSION = '0.0.3'.freeze
5
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: action-handle
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - rbviz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: connection_pool
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: redis
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: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
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: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Allows distributed handle/lock creation and managament
84
+ email:
85
+ - inbox@rbviz.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - CODE_OF_CONDUCT.md
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - active-handle.gemspec
99
+ - bin/console
100
+ - bin/rspec
101
+ - bin/setup
102
+ - lib/action_handle.rb
103
+ - lib/action_handle/adapters/base.rb
104
+ - lib/action_handle/adapters/rails_cache.rb
105
+ - lib/action_handle/adapters/redis_pool.rb
106
+ - lib/action_handle/base.rb
107
+ - lib/action_handle/configuration.rb
108
+ - lib/action_handle/version.rb
109
+ homepage: https://github.com/rbviz/action-handle
110
+ licenses:
111
+ - MIT
112
+ metadata:
113
+ homepage_uri: https://github.com/rbviz/action-handle
114
+ source_code_uri: https://github.com/rbviz/action-handle
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubygems_version: 3.1.0.pre3
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Backend agnostic resource handle
134
+ test_files: []