derivation_endpoint 0.1.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: 67a2be6e56ec830384f87110bb3bba6b457f06b732db0af0d606542be49a69cb
4
+ data.tar.gz: f20f4e3e60ef156df75e4927d3ad7313ff16b186379328f9633ba36a08b1309f
5
+ SHA512:
6
+ metadata.gz: 7985a12bd5e8049aeb60202dae65026ded3db1164b2751ca66324f624861b1a38a37158d06e8f83d0e980f50a53d584f8cdff42f06fd0984db7da2f063fd8686
7
+ data.tar.gz: 47a44235ba0a5c1366e208aa46687aa18c68c8eeab32a22bbddc39fae4a2a480070fffdc66f30e7d3bc543ffd3ef385e762a2c79102f9016ff934d3e5560f525
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,43 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ NewCops: enable
4
+ SuggestExtensions: false
5
+ Exclude:
6
+ - "vendor/**/*"
7
+
8
+ Style/FrozenStringLiteralComment:
9
+ Enabled: false
10
+
11
+ Style/StringLiterals:
12
+ EnforcedStyle: double_quotes
13
+
14
+ Style/Documentation:
15
+ Enabled: false
16
+
17
+ Style/WordArray:
18
+ Enabled: false
19
+
20
+ Style/SymbolArray:
21
+ Enabled: false
22
+
23
+ Metrics/CyclomaticComplexity:
24
+ Enabled: true
25
+
26
+ Metrics/MethodLength:
27
+ Max: 20
28
+
29
+ Metrics/BlockLength:
30
+ Exclude:
31
+ - "spec/**/*_spec.rb"
32
+ - "*.gemspec"
33
+
34
+ Layout/LineLength:
35
+ Exclude:
36
+ - "spec/**/*_spec.rb"
37
+ - "*.gemspec"
38
+
39
+ Naming/MemoizedInstanceVariableName:
40
+ EnforcedStyleForLeadingUnderscores: optional
41
+
42
+ Gemspec/RequireMFA:
43
+ Enabled: false
data/.simplecov ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "simplecov_json_formatter"
4
+
5
+ SimpleCov.start
6
+ SimpleCov.formatters = SimpleCov::Formatter::JSONFormatter
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## Changelog
2
+
3
+ ### [0.1.0] - 2023-02-04
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at authiatv@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in derivation_endpoint.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
13
+
14
+ gem "rack", "~> 3.0.4.1"
15
+
16
+ gem "simplecov", "~> 0.22.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,69 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ derivation_endpoint (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.5.0)
11
+ docile (1.4.0)
12
+ json (2.6.3)
13
+ parallel (1.22.1)
14
+ parser (3.2.0.0)
15
+ ast (~> 2.4.1)
16
+ rack (3.0.4.1)
17
+ rainbow (3.1.1)
18
+ rake (13.0.6)
19
+ regexp_parser (2.6.2)
20
+ rexml (3.2.5)
21
+ rspec (3.12.0)
22
+ rspec-core (~> 3.12.0)
23
+ rspec-expectations (~> 3.12.0)
24
+ rspec-mocks (~> 3.12.0)
25
+ rspec-core (3.12.0)
26
+ rspec-support (~> 3.12.0)
27
+ rspec-expectations (3.12.2)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.12.0)
30
+ rspec-mocks (3.12.3)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.12.0)
33
+ rspec-support (3.12.0)
34
+ rubocop (1.44.1)
35
+ json (~> 2.3)
36
+ parallel (~> 1.10)
37
+ parser (>= 3.2.0.0)
38
+ rainbow (>= 2.2.2, < 4.0)
39
+ regexp_parser (>= 1.8, < 3.0)
40
+ rexml (>= 3.2.5, < 4.0)
41
+ rubocop-ast (>= 1.24.1, < 2.0)
42
+ ruby-progressbar (~> 1.7)
43
+ unicode-display_width (>= 2.4.0, < 3.0)
44
+ rubocop-ast (1.24.1)
45
+ parser (>= 3.1.1.0)
46
+ ruby-progressbar (1.11.0)
47
+ simplecov (0.22.0)
48
+ docile (~> 1.1)
49
+ simplecov-html (~> 0.11)
50
+ simplecov_json_formatter (~> 0.1)
51
+ simplecov-html (0.12.3)
52
+ simplecov_json_formatter (0.1.4)
53
+ unicode-display_width (2.4.2)
54
+
55
+ PLATFORMS
56
+ arm64-darwin-21
57
+ x86_64-linux
58
+
59
+ DEPENDENCIES
60
+ bundler
61
+ derivation_endpoint!
62
+ rack (~> 3.0.4.1)
63
+ rake (~> 13.0)
64
+ rspec (~> 3.0)
65
+ rubocop (~> 1.21)
66
+ simplecov (~> 0.22.0)
67
+
68
+ BUNDLED WITH
69
+ 2.3.23
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Victor
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,87 @@
1
+ # DerivationEndpoint
2
+
3
+ [![Build Status](https://github.com/VictorAuthiat/derivation_endpoint/actions/workflows/ci.yml/badge.svg)](https://github.com/VictorAuthiat/derivation_endpoint/actions/workflows/ci.yml)
4
+
5
+ DerivationEndpoint is a gem that provides a Rack app to redirect from an url to another using custom strategies.
6
+ This can be useful when you want to for example expose private files with a public URL.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem "derivation_endpoint"
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle install
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install derivation_endpoint
23
+
24
+ ## Usage
25
+
26
+ Here are the steps to follow to set up an endpoint derivation:
27
+
28
+ **Configuration**
29
+
30
+ ```ruby
31
+ DerivationEndpoint.configure do |config|
32
+ config.host = "http://localhost:3000"
33
+ config.prefix = "derivation_endpoints"
34
+ config.encoder = proc {}
35
+ config.decoder = proc {}
36
+ end
37
+ ```
38
+
39
+ **Mount the endpoint**
40
+
41
+ ```ruby
42
+ mount DerivationEndpoint::Derivation.new => DerivationEndpoint.derivation_path
43
+ ```
44
+
45
+ **Add a derivation endpoint**
46
+
47
+ ```ruby
48
+ class Post < ApplicationRecord
49
+ extend DerivationEndpoint
50
+
51
+ derivation_endpoint :file, options: { foo: :bar }
52
+ end
53
+ ```
54
+
55
+ Two methods will now be available:
56
+
57
+ - `Post#file_derivation_url`
58
+ - `Post#file_redirection_url`
59
+
60
+ In this example `file_derivation_url` returns the derivation endpoint URL.
61
+ This endpoint redirects to the `file_redirection_url` using your encoder and decoder procs.
62
+
63
+ ## Examples
64
+
65
+ - [Shrine](https://github.com/VictorAuthiat/derivation_endpoint/tree/master/docs/shrine.md)
66
+ - [CarrierWave](https://github.com/VictorAuthiat/derivation_endpoint/tree/master/docs/carrierwave.md)
67
+ - ActiveStorage (WIP)
68
+ - PaperClip (WIP)
69
+ - Refile (WIP)
70
+
71
+ ## Development
72
+
73
+ 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.
74
+
75
+ 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).
76
+
77
+ ## Contributing
78
+
79
+ Bug reports and pull requests are welcome on GitHub at https://github.com/victorauthiat/derivation_endpoint. 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/victorauthiat/derivation_endpoint/blob/master/CODE_OF_CONDUCT.md).
80
+
81
+ ## License
82
+
83
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
84
+
85
+ ## Code of Conduct
86
+
87
+ Everyone interacting in the DerivationEndpoint project"s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/victorauthiat/derivation_endpoint/blob/master/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]
@@ -0,0 +1,47 @@
1
+ ## CarrierWave example
2
+
3
+ ### carrierwave-aws
4
+
5
+ **Create initializer**
6
+
7
+ ```ruby
8
+ DerivationEndpoint.configure do |config|
9
+ config.host = "http://localhost:3000"
10
+ config.prefix = "derivation_endpoints"
11
+
12
+ config.encoder = ->(method_value) { method_value.file.path }
13
+ config.decoder = ->(path, options) do
14
+ uploader = options[:uploader].constantize
15
+ storage = CarrierWave::Storage::AWS.new(uploader)
16
+
17
+ CarrierWave::Storage::AWSFile.new(uploader, storage.connection, path).url
18
+ end
19
+ end
20
+ ```
21
+
22
+ **Mount derivation endpoint**
23
+
24
+ ```ruby
25
+ mount DerivationEndpoint::Derivation.new => DerivationEndpoint.derivation_path
26
+ ```
27
+
28
+ **Update model**
29
+
30
+ ```ruby
31
+ class Post < ApplicationRecord
32
+ extend DerivationEndpoint
33
+
34
+ mount_uploader :file, FileUploader
35
+
36
+ derivation_endpoint :file, options: { uploader: FileUploader }
37
+ end
38
+ ```
39
+
40
+ ### Fog [coming soon]
41
+ - AWS
42
+ - Google
43
+ - Rackspace
44
+ - OpenStack
45
+ - AzureRM
46
+
47
+ ### File [coming soon]
data/docs/shrine.md ADDED
@@ -0,0 +1,28 @@
1
+ ## Shrine example
2
+
3
+ **Create initializer**
4
+
5
+ ```ruby
6
+ DerivationEndpoint.configure do |config|
7
+ config.host = "http://localhost:3000"
8
+ config.prefix = "derivation_endpoints"
9
+ config.encoder = ->(method_value) { method_value.id }
10
+ config.decoder = ->(path, options) { Shrine::UploadedFile.new(id: path, storage: options[:storage]).url }
11
+ end
12
+ ```
13
+
14
+ **Mount derivation endpoint**
15
+
16
+ ```ruby
17
+ mount DerivationEndpoint::Derivation.new => DerivationEndpoint.derivation_path
18
+ ```
19
+
20
+ **Update model**
21
+
22
+ ```ruby
23
+ class Post < ApplicationRecord
24
+ extend DerivationEndpoint
25
+
26
+ derivation_endpoint :file, options: { storage: :store }
27
+ end
28
+ ```
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "base64"
4
+ require "json"
5
+
6
+ module DerivationEndpoint
7
+ class Attacher
8
+ attr_reader :object, :method, :prefix, :options
9
+
10
+ def initialize(object, method, prefix, options)
11
+ Validation.check_object_class(prefix, [String, Symbol, NilClass])
12
+ Validation.check_object_class(options, [Hash, NilClass])
13
+
14
+ @object = object
15
+ @method = method
16
+ @prefix = prefix
17
+ @options = options
18
+ end
19
+
20
+ def derivation_path
21
+ DerivationEndpoint::Serializer.encode(serialization_params)
22
+ end
23
+
24
+ def redirection_url
25
+ DerivationEndpoint.config.decoder.call(derivation_path, options)
26
+ end
27
+
28
+ def method_value
29
+ object.public_send(method)
30
+ end
31
+
32
+ def derivation_url
33
+ base_url = DerivationEndpoint.base_url
34
+ final_url = prefix ? "#{base_url}/#{prefix}" : base_url
35
+
36
+ "#{final_url}/#{derivation_path}"
37
+ end
38
+
39
+ private
40
+
41
+ def serialization_params
42
+ {
43
+ path: DerivationEndpoint.config.encoder.call(method_value),
44
+ options: options
45
+ }
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DerivationEndpoint
4
+ module Attachment
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ ATTACHER_DERIVATION_METHODS = ["derivation_url", "redirection_url"].freeze
11
+
12
+ def derivation_endpoint(method, prefix: nil, options: nil)
13
+ Validation.check_object_class(method, [String, Symbol])
14
+ Validation.check_object_class(prefix, [String, Symbol, NilClass])
15
+ Validation.check_object_class(options, [Hash, NilClass])
16
+
17
+ define_derivation_methods(method, prefix, options)
18
+ end
19
+
20
+ private
21
+
22
+ def define_derivation_methods(method, prefix, options)
23
+ ATTACHER_DERIVATION_METHODS.each do |attacher_method|
24
+ define_method(:"#{method}_#{attacher_method}") do
25
+ attacher = Attacher.new(self, method, prefix, options)
26
+ attacher.public_send(attacher_method)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DerivationEndpoint
4
+ class Config
5
+ REQUIRED_ATTRIBUTES = [:host, :prefix, :encoder, :decoder].freeze
6
+
7
+ def self.valid?(config)
8
+ Validation.check_object_class(config, [self])
9
+
10
+ REQUIRED_ATTRIBUTES.all? { |attribute| !!config.public_send(attribute) }
11
+ end
12
+
13
+ attr_reader :host, :prefix, :encoder, :decoder
14
+
15
+ def initialize
16
+ @host = nil
17
+ @prefix = nil
18
+ @encoder = nil
19
+ @decoder = nil
20
+ end
21
+
22
+ def valid?
23
+ self.class.valid?(self)
24
+ end
25
+
26
+ def host=(value)
27
+ Validation.check_object_class(value, [String])
28
+
29
+ @host = value
30
+ end
31
+
32
+ def prefix=(value)
33
+ Validation.check_object_class(value, [String])
34
+
35
+ @prefix = value
36
+ end
37
+
38
+ def encoder=(value)
39
+ Validation.check_object_class(value, [Proc])
40
+
41
+ @encoder = value
42
+ end
43
+
44
+ def decoder=(value)
45
+ Validation.check_object_class(value, [Proc])
46
+
47
+ @decoder = value
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rack"
4
+
5
+ module DerivationEndpoint
6
+ class Derivation
7
+ class << self
8
+ def decode(params)
9
+ decoded_data = DerivationEndpoint::Serializer.decode(params)
10
+ path = decoded_data["path"]
11
+ options = decoded_data["options"].transform_keys(&:to_sym)
12
+
13
+ DerivationEndpoint.config.decoder.call(path, options)
14
+ end
15
+ end
16
+
17
+ def call(env)
18
+ request = Rack::Request.new(env)
19
+
20
+ return handle_request(request) if request.get? || request.head?
21
+
22
+ render_error(405, "Method not allowed")
23
+ end
24
+
25
+ private
26
+
27
+ def handle_request(request)
28
+ params = request.path_info.split("/").last
29
+
30
+ return render_success(params) if params
31
+
32
+ render_error(404, "Source not found")
33
+ rescue ArgumentError
34
+ render_error(405, "Method not allowed")
35
+ end
36
+
37
+ def render_success(params)
38
+ [302, { "Location" => self.class.decode(params) }, []]
39
+ end
40
+
41
+ def render_error(status, message)
42
+ [status, { "Content-Type" => "text/plain" }, [message]]
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "base64"
4
+ require "json"
5
+
6
+ module DerivationEndpoint
7
+ module Serializer
8
+ class << self
9
+ def encode(data)
10
+ Validation.check_object_class(data, [Hash])
11
+
12
+ Base64.urlsafe_encode64(JSON.generate(data), padding: false)
13
+ end
14
+
15
+ def decode(data)
16
+ Validation.check_object_class(data, [String])
17
+
18
+ JSON.parse(Base64.urlsafe_decode64(data))
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DerivationEndpoint
4
+ module Validation
5
+ def self.check_object_class(object, expected_classes = [])
6
+ return if expected_classes.include?(object.class)
7
+
8
+ raise ArgumentError,
9
+ "Expect #{object} class to be #{expected_classes.join(', ')}."
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DerivationEndpoint
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "derivation_endpoint/version"
4
+ require_relative "derivation_endpoint/validation"
5
+
6
+ module DerivationEndpoint
7
+ autoload :Config, "derivation_endpoint/config"
8
+ autoload :Attacher, "derivation_endpoint/attacher"
9
+ autoload :Attachment, "derivation_endpoint/attachment"
10
+ autoload :Derivation, "derivation_endpoint/derivation"
11
+ autoload :Serializer, "derivation_endpoint/serializer"
12
+
13
+ class << self
14
+ def extended(base)
15
+ base.include(DerivationEndpoint::Attachment)
16
+ end
17
+
18
+ def configure
19
+ yield config if block_given?
20
+ end
21
+
22
+ def config
23
+ @_config ||= Config.new
24
+ end
25
+
26
+ def derivation_path
27
+ return unless config.valid?
28
+
29
+ "/#{config.prefix}"
30
+ end
31
+
32
+ def base_url
33
+ return unless config.valid?
34
+
35
+ config.host + derivation_path
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,4 @@
1
+ module DerivationEndpoint
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: derivation_endpoint
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Victor
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-02-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Redirect from an url to another using custom strategies
28
+ email:
29
+ - authiatv@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - ".rubocop.yml"
36
+ - ".simplecov"
37
+ - CHANGELOG.md
38
+ - CODE_OF_CONDUCT.md
39
+ - Gemfile
40
+ - Gemfile.lock
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - docs/carrierwave.md
45
+ - docs/shrine.md
46
+ - lib/derivation_endpoint.rb
47
+ - lib/derivation_endpoint/attacher.rb
48
+ - lib/derivation_endpoint/attachment.rb
49
+ - lib/derivation_endpoint/config.rb
50
+ - lib/derivation_endpoint/derivation.rb
51
+ - lib/derivation_endpoint/serializer.rb
52
+ - lib/derivation_endpoint/validation.rb
53
+ - lib/derivation_endpoint/version.rb
54
+ - sig/derivation_endpoint.rbs
55
+ homepage: https://github.com/victorauthiat/derivation_endpoint
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 2.6.0
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.3.7
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Derivation endpoint
78
+ test_files: []