two_percent 0.3.0 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9c5a241a697109d794c29fbac31f4ecd363b883dd70f0a2d22bec5a94bdccb98
4
- data.tar.gz: 539239a87f4b1ca23c84b7d3080db2104db74fa09a2f906a41b3cac168e7c91b
3
+ metadata.gz: 533e741957e0c547a769287f8916a72005c839c7db3af317984caf541263bf15
4
+ data.tar.gz: 2b43afab484a19828f373069addae52cdabc3150434f267ff488454a566de4c3
5
5
  SHA512:
6
- metadata.gz: 470b871f02fb8a5d125fb80c371186fe0301b69293b4e8a00ac9353906c8287f137b33310feee56f32c936faf5215e8801f83ed0cb72c9cbd1b5e3de4c7c4c10
7
- data.tar.gz: 9b4957e2a180f5ae6c1fc8314138b134d6b219997ec754846d3296920efe84f00981ab555f494608428f03adab1bf2dff4d35cd8ce56758c61a1bd4050ecec83
6
+ metadata.gz: debd817addfe72f031540b9d14a420bd0f9845880f61e5ed77d0cc31c36650362c725281d5629b5b514d39f9634a98cb35253de85b1d8d55eb21d79e6ccd420f
7
+ data.tar.gz: 8a0dc3faf0d6a4f1d02b1eb612bc37b693ab1ad2d3bf785c4ddf247acae55d78c7d4c3e506cf6b6b788136fbfab70510be47649f583e55cbc8ab42d9aeeac3a3
@@ -2,5 +2,10 @@
2
2
 
3
3
  module TwoPercent
4
4
  class ApplicationController < ActionController::API
5
+ before_action :authenticate
6
+
7
+ def authenticate
8
+ instance_exec(&TwoPercent.config.authenticate)
9
+ end
5
10
  end
6
11
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TwoPercent
4
+ class BulkController < ApplicationController
5
+ def _dispatch = processor.dispatch
6
+
7
+ private
8
+
9
+ def processor
10
+ @processor ||= TwoPercent::BulkProcessor.new(operations)
11
+ end
12
+
13
+ def operations
14
+ params.require(:Operations).map { _1.permit(:method, :path, :bulkId, data: {}).to_h }
15
+ end
16
+ end
17
+ end
@@ -2,8 +2,6 @@
2
2
 
3
3
  module TwoPercent
4
4
  class ScimController < ApplicationController
5
- before_action :authenticate
6
-
7
5
  def create
8
6
  TwoPercent::CreateEvent.create(resource: params[:resource_type], params: scim_params)
9
7
 
@@ -31,11 +29,7 @@ module TwoPercent
31
29
  private
32
30
 
33
31
  def scim_params
34
- params.except(:controller, :action, :resource_type).as_json.deep_symbolize_keys
35
- end
36
-
37
- def authenticate
38
- instance_exec(&TwoPercent.config.authenticate)
32
+ params.except(:controller, :action, :resource_type).as_json.with_indifferent_access
39
33
  end
40
34
  end
41
35
  end
data/config/routes.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  TwoPercent::Engine.routes.draw do
4
+ post "/Bulk" => "bulk#_dispatch"
4
5
  post "/:resource_type" => "scim#create"
5
6
  patch "/:resource_type/:id" => "scim#update"
6
7
  put "/:resource_type/:id" => "scim#replace"
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TwoPercent
4
+ class BulkProcessor
5
+ def initialize(operations)
6
+ @operations = operations
7
+ end
8
+
9
+ def dispatch(event_handler = EventHandler)
10
+ @operations.each do |operation|
11
+ resource, id = parse_path(operation[:path])
12
+ attrs = { resource: resource, id: id, params: operation[:data] }.compact
13
+ event_handler.dispatch(operation[:method], **attrs)
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def parse_path(path)
20
+ _, resource_type, id = path.split("/")
21
+
22
+ [resource_type, id]
23
+ end
24
+ end
25
+ end
@@ -30,6 +30,26 @@ module TwoPercent
30
30
  # end
31
31
  #
32
32
  config_accessor :authenticate do
33
- ->(*) { true }
33
+ ->(*) do
34
+ TwoPercent.logger.warn(<<~MESSAGE)
35
+ TwoPercent authenticate is currently configured using a default and is blocking authenticaiton.
36
+
37
+ To make this wraning go away provide a configuration for `TwoPercent.config.authenticate`.
38
+
39
+ The value should:
40
+ 1. Be callable like a Proc.
41
+ 2. Return true when the request is permitted.
42
+ 3. Return false when the request is not permitted.
43
+ MESSAGE
44
+
45
+ false
46
+ end
34
47
  end
48
+
49
+ #
50
+ # Configures a logger to be used by TwoPercent modules.
51
+ # It can be accessed through `TwoPercent.logger`
52
+ # `TwoPercent.logger` will default to Rails.logger
53
+ #
54
+ config_accessor :logger
35
55
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TwoPercent
4
+ class EventHandler
5
+ def self.dispatch(method, **attrs)
6
+ EventHandler.new(method).dispatch(**attrs)
7
+ end
8
+
9
+ METHOD_EVENT = {
10
+ "POST" => "TwoPercent::CreateEvent",
11
+ "PATCH" => "TwoPercent::UpdateEvent",
12
+ "PUT" => "TwoPercent::ReplaceEvent",
13
+ "DELETE" => "TwoPercent::DeleteEvent",
14
+ }.freeze
15
+
16
+ def initialize(method)
17
+ @event = METHOD_EVENT.fetch(method) do
18
+ raise "Invalid method #{method}"
19
+ end.constantize
20
+ end
21
+
22
+ def dispatch(**attrs)
23
+ @event.create(**attrs)
24
+ end
25
+ end
26
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TwoPercent
4
- VERSION = "0.3.0"
4
+ VERSION = "0.5.0"
5
5
  end
data/lib/two_percent.rb CHANGED
@@ -4,6 +4,12 @@ require "aether_observatory"
4
4
 
5
5
  require "two_percent/version"
6
6
  require "two_percent/configuration"
7
+ require "two_percent/event_handler"
8
+ require "two_percent/bulk_processor"
7
9
 
8
10
  module TwoPercent
11
+ # Logger used by TwoPercent. Defaults to Rails.logger
12
+ def self.logger
13
+ config.logger || Rails.logger
14
+ end
9
15
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: two_percent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carlos Palhares
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2025-05-07 00:00:00.000000000 Z
13
+ date: 2025-05-29 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: aether_observatory
@@ -53,6 +53,7 @@ files:
53
53
  - MIT-LICENSE
54
54
  - Rakefile
55
55
  - app/controllers/two_percent/application_controller.rb
56
+ - app/controllers/two_percent/bulk_controller.rb
56
57
  - app/controllers/two_percent/scim_controller.rb
57
58
  - app/events/two_percent/application_event.rb
58
59
  - app/events/two_percent/create_event.rb
@@ -62,8 +63,10 @@ files:
62
63
  - config/routes.rb
63
64
  - lib/tasks/two_percent_tasks.rake
64
65
  - lib/two_percent.rb
66
+ - lib/two_percent/bulk_processor.rb
65
67
  - lib/two_percent/configuration.rb
66
68
  - lib/two_percent/engine.rb
69
+ - lib/two_percent/event_handler.rb
67
70
  - lib/two_percent/version.rb
68
71
  homepage: https://github.com/powerhome/power-tools
69
72
  licenses: