ruby_slime 0.0.1 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 016751bd1a7ff4786175ba1de34a94c8358c71145f6b24bed359e54a5b2f64d9
4
- data.tar.gz: 2a2f73d7f2fd439badd416d433879ef28097aabe1de92a8f92177fc1fe331707
3
+ metadata.gz: d97fde901999a9c590e81817687191021260b13e1ef6fc825886ded4b4e211e0
4
+ data.tar.gz: 735dfa78ce4ccf683982ba8950b318c00a4515f06be6a2a1b2d7e2698c58700d
5
5
  SHA512:
6
- metadata.gz: 484554d5c87bbac120624f97029a35a80937aa05aa3ee70ee186f60431679e368c404d638466022e269696351634dd1327fc9c21bb2b4ed1d7bdce008291f8b2
7
- data.tar.gz: 4b3ff146857e90975dd948937f09b1b01b4aca5457b561f4cd23cccb131a398499cbd552b7c770b2c73ba543d08d76dbef29cba4310604c231399cc36fe0b871
6
+ metadata.gz: 96760cd3e0762f269684738f7dae2fce7d47d416716f806e4138e6135188a50fcad05a4f646f77edbb0c077eaaaad605822ef33f818dd86be1d9faa258dc989b
7
+ data.tar.gz: f4f0a5f06d4808eefaa4f1c04b1b182a6180740cb85ec480cd40c15f758e8483b7176441dd21688daf2bf585d1de9f425b187ee06e65ddd8f099bd77adf1f7c1
@@ -3,34 +3,26 @@
3
3
  module RubySlime
4
4
  module Internals
5
5
  module EventMetadataHandler
6
+ include ObjectSharing
7
+
6
8
  private
7
9
 
8
10
  def handle_handler_arguments(handler, events, metadata)
9
11
  handler.call(events, **metadata)
10
12
  rescue ::ArgumentError => e
11
- cache = EventMetadataHandler_SingletonCache.instance
12
- should_retry = cache.meta_patterns.any? do |pattern|
13
+ should_retry = shared_objects[:meta_patterns].any? do |pattern|
13
14
  pattern =~ e.message
14
15
  end
15
16
  raise e unless should_retry
16
17
  handler.call(events)
17
18
  end
18
19
 
19
- class EventMetadataHandler_SingletonCache
20
- include ::Singleton
21
- attr_reader :meta_patterns
22
-
23
- def initialize
24
- @meta_patterns = [
25
- /wrong\s+number\s+of\s+arguments/i,
26
- /unknown\s+keywords/i,
27
- ]
28
- super
29
- freeze
30
- end
31
- end
32
-
33
- private_constant :EventMetadataHandler_SingletonCache
20
+ share_objects({
21
+ meta_patterns: [
22
+ /wrong\s+number\s+of\s+arguments/i,
23
+ /unknown\s+keywords/i,
24
+ ],
25
+ })
34
26
  end
35
27
  end
36
28
  end
@@ -3,7 +3,7 @@
3
3
  module RubySlime
4
4
  module JWTKit
5
5
  class APIVerifier
6
- extend ::Forwardable
6
+ include ObjectSharing
7
7
 
8
8
  def initialize(
9
9
  fetch_secret:,
@@ -14,24 +14,22 @@ module RubySlime
14
14
  end
15
15
 
16
16
  def call(token)
17
- token_result = token_schema.call({
17
+ token_result = shared_objects[:token_schema].call({
18
18
  token: token,
19
19
  })
20
20
  raise TokenError.new(token_result.errors) unless token_result.success?
21
+ validated_cache = {}
21
22
  ::JWT.decode(token_result[:token], nil, true, {
22
23
  algorithm: 'HS256',
23
24
  required_claims: %w[aud iat sub],
24
- }) do |header|
25
- header_result = header_schema.call(header)
26
- raise TokenError.new(header_result.errors) unless header_result.success?
27
- @fetch_secret.call(header_result.to_h)
25
+ }) do |header_section|
26
+ validated_cache[:header] ||= check_jwt_section(header_section, :header_schema)
27
+ @fetch_secret.call(validated_cache[:header])
28
28
  end.then do |sections|
29
29
  payload_section, header_section = sections
30
- header_result = header_schema.call(header_section)
31
- raise TokenError.new(header_result.errors) unless header_result.success?
32
- payload_result = payload_schema.call(payload_section)
33
- raise TokenError.new(payload_result.errors) unless payload_result.success?
34
- [payload_result, header_result].map(&:to_h)
30
+ validated_cache[:header] ||= check_jwt_section(header_section, :header_schema)
31
+ validated_cache[:payload] ||= check_jwt_section(payload_section, :payload_schema)
32
+ validated_cache.values_at(:payload, :header)
35
33
  end.then do |sections|
36
34
  @handle_result.call(*sections)
37
35
  end
@@ -39,43 +37,28 @@ module RubySlime
39
37
 
40
38
  private
41
39
 
42
- def_delegators :singleton_cache,
43
- :token_schema,
44
- :header_schema,
45
- :payload_schema
46
-
47
- def singleton_cache
48
- APIVerifier_SingletonCache.instance
49
- end
50
-
51
- class APIVerifier_SingletonCache
52
- include ::Singleton
53
- attr_reader \
54
- :token_schema,
55
- :header_schema,
56
- :payload_schema
57
-
58
- def initialize
59
- @token_schema = ::Dry::Schema.Params do
60
- required(:token).filled(:string)
61
- end
62
- @header_schema = ::Dry::Schema.Params do
63
- optional(:typ).filled(:string).value(eql?: 'JWT')
64
- required(:alg).filled(:string).value(eql?: 'HS256')
65
- required(:kid).filled(:string)
66
- end
67
- @payload_schema = ::Dry::Schema.Params do
68
- required(:aud).filled(:string)
69
- required(:iat).value(:integer, gt?: 0)
70
- required(:sub).filled(:string)
71
- optional(:data).filled(:hash)
72
- end
73
- super
74
- freeze
75
- end
40
+ def check_jwt_section(section, schema_name)
41
+ section_result = shared_objects[schema_name].call(section)
42
+ raise TokenError.new(section_result.errors) unless section_result.success?
43
+ section_result.to_h
76
44
  end
77
45
 
78
- private_constant :APIVerifier_SingletonCache
46
+ share_objects({
47
+ token_schema: ::Dry::Schema.Params do
48
+ required(:token).filled(:string)
49
+ end,
50
+ header_schema: ::Dry::Schema.Params do
51
+ optional(:typ).filled(:string).value(eql?: 'JWT')
52
+ required(:alg).filled(:string).value(eql?: 'HS256')
53
+ required(:kid).filled(:string)
54
+ end,
55
+ payload_schema: ::Dry::Schema.Params do
56
+ required(:aud).filled(:string)
57
+ required(:iat).value(:integer, gt?: 0)
58
+ required(:sub).filled(:string)
59
+ optional(:data).hash
60
+ end,
61
+ })
79
62
  end
80
63
  end
81
64
  end
@@ -3,6 +3,8 @@
3
3
  module RubySlime
4
4
  module JWTKit
5
5
  module HandlingHelper
6
+ extend ::Forwardable
7
+
6
8
  private
7
9
 
8
10
  def_delegators :@controller,
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubySlime
4
+ module ObjectSharing
5
+ extend ::ActiveSupport::Concern
6
+
7
+ class_methods do
8
+ attr_reader :_object_sharing_cache
9
+
10
+ private def share_objects(objects_hash)
11
+ (@_object_sharing_cache ||= {}).merge!(objects_hash)
12
+ end
13
+ end
14
+
15
+ included do
16
+ private def shared_objects
17
+ (self.class._object_sharing_cache || {})
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubySlime
4
+ module Rake
5
+ module Context
6
+ def initialize_context(context)
7
+ @_rake_context = context
8
+ end
9
+
10
+ private
11
+
12
+ def desc(*args, **kwargs, &block)
13
+ @_rake_context.call(:desc, *args, **kwargs, &block)
14
+ end
15
+
16
+ def task(*args, **kwargs, &block)
17
+ @_rake_context.call(:task, *args, **kwargs, &block)
18
+ end
19
+
20
+ def file(*args, **kwargs, &block)
21
+ @_rake_context.call(:file, *args, **kwargs, &block)
22
+ end
23
+
24
+ def sh(*args, **kwargs, &block)
25
+ @_rake_context.call(:sh, *args, **kwargs, &block)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubySlime
4
+ module Rake
5
+ class DevelopmentSync
6
+ include Context
7
+
8
+ def initialize(
9
+ rake_context:,
10
+ path_prefix: '',
11
+ path_namespace:,
12
+ rclone_conf: 'sync.rclone.conf',
13
+ rclone_remote: 'sync'
14
+ )
15
+ initialize_context(rake_context)
16
+ @rclone_conf = rclone_conf
17
+ @rclone_remote = rclone_remote
18
+ @path_namespace = [path_prefix, path_namespace].select(&:present?).then do |path_sections|
19
+ ::File.join(*path_sections)
20
+ end
21
+ @check_rclone_conf = lambda do
22
+ raise "\"#{@rclone_conf}\" not found!" unless ::File.file?(@rclone_conf)
23
+ end
24
+ end
25
+
26
+ def declare
27
+ desc('rclone sync up')
28
+ task('sync-up', &method(:sync_up))
29
+
30
+ desc('rclone sync down')
31
+ task('sync-down', &method(:sync_down))
32
+ end
33
+
34
+ private
35
+
36
+ def sync_up(*_args, **_kwargs)
37
+ include_options = `git ls-files -io --exclude-per-directory=.gitignore`
38
+ .each_line
39
+ .map(&:strip)
40
+ .select(&:present?)
41
+ .reject do |filename|
42
+ filename.split(%r{[/\\]}).then do |path_sections|
43
+ path_sections.include?('.terraform') || \
44
+ path_sections.include?('.idea') || \
45
+ path_sections.last.then do |file_name|
46
+ file_name.end_with?('rclone.conf') || \
47
+ file_name.end_with?('tfstate.backup')
48
+ end
49
+ end
50
+ end.map do |file_path|
51
+ "--include=#{file_path}"
52
+ end
53
+ @check_rclone_conf.call
54
+ sh(
55
+ 'rclone',
56
+ 'copy',
57
+ "--config=#{@rclone_conf}",
58
+ *include_options,
59
+ '.',
60
+ "#{@rclone_remote}:#{@path_namespace}/",
61
+ )
62
+ end
63
+
64
+ def sync_down(*_args, **_kwargs)
65
+ @check_rclone_conf.call
66
+ sh(
67
+ 'rclone',
68
+ 'copy',
69
+ "--config=#{@rclone_conf}",
70
+ "#{@rclone_remote}:#{@path_namespace}/",
71
+ '.',
72
+ )
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubySlime
4
+ module Rake
5
+ end
6
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubySlime
4
- VERSION = '0.0.1'
4
+ VERSION = '0.0.4'
5
5
  end
data/lib/ruby_slime.rb CHANGED
@@ -13,6 +13,16 @@ loader.inflector.inflect(
13
13
  'api_verifier' => 'APIVerifier',
14
14
  'jwt_kit' => 'JWTKit',
15
15
  )
16
+ ::File.join(__dir__, 'ruby_slime').then do |base_path|
17
+ %w[jwt_kit mongoid rake].flat_map do |autoload_module|
18
+ [
19
+ ::File.join(base_path, "#{autoload_module}.rb"),
20
+ ::File.join(base_path, autoload_module),
21
+ ]
22
+ end
23
+ end.tap do |autoload_paths|
24
+ loader.do_not_eager_load(*autoload_paths)
25
+ end
16
26
  loader.setup
17
27
 
18
28
  module RubySlime
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_slime
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sarun Rattanasiri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-26 00:00:00.000000000 Z
11
+ date: 2023-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -119,7 +119,11 @@ files:
119
119
  - lib/ruby_slime/mongoid/interactor.rb
120
120
  - lib/ruby_slime/mongoid/stale_record.rb
121
121
  - lib/ruby_slime/mongoid/state.rb
122
+ - lib/ruby_slime/object_sharing.rb
122
123
  - lib/ruby_slime/process_helper.rb
124
+ - lib/ruby_slime/rake.rb
125
+ - lib/ruby_slime/rake/context.rb
126
+ - lib/ruby_slime/rake/development_sync.rb
123
127
  - lib/ruby_slime/schema_violation.rb
124
128
  - lib/ruby_slime/unknown_command.rb
125
129
  - lib/ruby_slime/version.rb