logux_rails 0.1.0 → 0.2.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.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -0
  3. data/Appraisals +4 -0
  4. data/CHANGELOG.md +13 -0
  5. data/Gemfile +3 -0
  6. data/README.md +7 -2
  7. data/app/helpers/logux_helper.rb +1 -2
  8. data/config/routes.rb +1 -1
  9. data/lib/logux/{actions.rb → action.rb} +1 -1
  10. data/lib/logux/model/proxy.rb +1 -1
  11. data/lib/logux/model/updates_deprecator.rb +12 -8
  12. data/lib/logux/version.rb +1 -1
  13. data/lib/logux_rails.rb +22 -1
  14. data/lib/tasks/logux_tasks.rake +10 -37
  15. data/logux_rails.gemspec +3 -6
  16. metadata +14 -77
  17. data/app/controllers/logux_controller.rb +0 -41
  18. data/lib/logux.rb +0 -107
  19. data/lib/logux/action_caller.rb +0 -42
  20. data/lib/logux/action_controller.rb +0 -6
  21. data/lib/logux/add.rb +0 -37
  22. data/lib/logux/auth.rb +0 -6
  23. data/lib/logux/base_controller.rb +0 -37
  24. data/lib/logux/channel_controller.rb +0 -24
  25. data/lib/logux/class_finder.rb +0 -61
  26. data/lib/logux/client.rb +0 -21
  27. data/lib/logux/error_renderer.rb +0 -40
  28. data/lib/logux/meta.rb +0 -36
  29. data/lib/logux/node.rb +0 -37
  30. data/lib/logux/policy.rb +0 -14
  31. data/lib/logux/policy_caller.rb +0 -34
  32. data/lib/logux/process.rb +0 -9
  33. data/lib/logux/process/action.rb +0 -60
  34. data/lib/logux/process/auth.rb +0 -27
  35. data/lib/logux/process/batch.rb +0 -59
  36. data/lib/logux/response.rb +0 -18
  37. data/lib/logux/stream.rb +0 -25
  38. data/lib/logux/test.rb +0 -35
  39. data/lib/logux/test/helpers.rb +0 -75
  40. data/lib/logux/test/matchers.rb +0 -10
  41. data/lib/logux/test/matchers/base.rb +0 -25
  42. data/lib/logux/test/matchers/response_chunks.rb +0 -48
  43. data/lib/logux/test/matchers/send_to_logux.rb +0 -51
  44. data/lib/logux/test/store.rb +0 -21
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Logux
4
- class Policy
5
- class UnauthorizedError < StandardError; end
6
-
7
- attr_reader :action, :meta
8
-
9
- def initialize(action:, meta:)
10
- @action = action
11
- @meta = meta
12
- end
13
- end
14
- end
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Logux
4
- class PolicyCaller
5
- attr_reader :action, :meta
6
-
7
- delegate :logger, :configuration, to: :Logux
8
-
9
- def initialize(action:, meta:)
10
- @action = action
11
- @meta = meta
12
- end
13
-
14
- def call!
15
- logger.debug('Searching policy for Logux action:' \
16
- " #{action}, meta: #{meta}")
17
- policy.public_send("#{action.action_type}?")
18
- rescue Logux::UnknownActionError, Logux::UnknownChannelError => e
19
- raise e if configuration.verify_authorized
20
-
21
- logger.warn(e)
22
- end
23
-
24
- private
25
-
26
- def class_finder
27
- @class_finder ||= Logux::ClassFinder.new(action: action, meta: meta)
28
- end
29
-
30
- def policy
31
- class_finder.find_policy_class.new(action: action, meta: meta)
32
- end
33
- end
34
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Logux
4
- module Process
5
- autoload :Batch, 'logux/process/batch'
6
- autoload :Auth, 'logux/process/auth'
7
- autoload :Action, 'logux/process/action'
8
- end
9
- end
@@ -1,60 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Logux
4
- module Process
5
- class Action
6
- attr_reader :stream, :chunk
7
- attr_accessor :stop_process
8
-
9
- def initialize(stream:, chunk:)
10
- @stream = stream
11
- @chunk = chunk
12
- end
13
-
14
- def call
15
- process_authorization!
16
- process_action!
17
- end
18
-
19
- def action_from_chunk
20
- @action_from_chunk ||= chunk[:action]
21
- end
22
-
23
- def meta_from_chunk
24
- @meta_from_chunk ||= chunk[:meta]
25
- end
26
-
27
- def stop_process?
28
- @stop_process ||= false
29
- end
30
-
31
- def stop_process!
32
- @stop_process = true
33
- end
34
-
35
- private
36
-
37
- def process_action!
38
- return if stop_process?
39
-
40
- action_caller = Logux::ActionCaller.new(
41
- action: action_from_chunk,
42
- meta: meta_from_chunk
43
- )
44
-
45
- stream.write(action_caller.call!.format)
46
- end
47
-
48
- def process_authorization!
49
- policy_caller = Logux::PolicyCaller.new(action: action_from_chunk,
50
- meta: meta_from_chunk)
51
- policy_check = policy_caller.call!
52
- status = policy_check ? :approved : :forbidden
53
- stream.write([status, meta_from_chunk.id])
54
- return stream.write(',') if policy_check
55
-
56
- stop_process!
57
- end
58
- end
59
- end
60
- end
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Logux
4
- module Process
5
- class Auth
6
- attr_reader :stream, :chunk
7
-
8
- def initialize(stream:, chunk:)
9
- @stream = stream
10
- @chunk = chunk
11
- end
12
-
13
- def call
14
- authed = Logux.configuration.auth_rule.call(user_id, chunk.credentials)
15
- return stream.write(['authenticated', chunk.auth_id]) if authed
16
-
17
- stream.write(['denied', chunk.auth_id])
18
- end
19
-
20
- private
21
-
22
- def user_id
23
- chunk.node_id.split(':').first
24
- end
25
- end
26
- end
27
- end
@@ -1,59 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Logux
4
- module Process
5
- class Batch
6
- attr_reader :stream, :batch
7
-
8
- def initialize(stream:, batch:)
9
- @stream = stream
10
- @batch = batch
11
- end
12
-
13
- def call
14
- last_chunk = batch.size - 1
15
- preprocessed_batch.map.with_index do |chunk, index|
16
- case chunk[:type]
17
- when :action
18
- process_action(chunk: chunk.slice(:action, :meta))
19
- when :auth
20
- process_auth(chunk: chunk[:auth])
21
- end
22
- stream.write(',') if index != last_chunk
23
- end
24
- end
25
-
26
- def process_action(chunk:)
27
- Logux::Process::Action.new(stream: stream, chunk: chunk).call
28
- end
29
-
30
- def process_auth(chunk:)
31
- Logux::Process::Auth.new(stream: stream, chunk: chunk).call
32
- end
33
-
34
- def preprocessed_batch
35
- @preprocessed_batch ||= batch.map do |chunk|
36
- case chunk[0]
37
- when 'action'
38
- preprocess_action(chunk)
39
- when 'auth'
40
- preprocess_auth(chunk)
41
- end
42
- end
43
- end
44
-
45
- def preprocess_action(chunk)
46
- { type: :action,
47
- action: Logux::Actions.new(chunk[1]),
48
- meta: Logux::Meta.new(chunk[2]) }
49
- end
50
-
51
- def preprocess_auth(chunk)
52
- { type: :auth,
53
- auth: Logux::Auth.new(node_id: chunk[1],
54
- credentials: chunk[2],
55
- auth_id: chunk[3]) }
56
- end
57
- end
58
- end
59
- end
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Logux
4
- class Response
5
- attr_reader :status, :action, :meta, :custom_data
6
-
7
- def initialize(status, action:, meta:, custom_data: nil)
8
- @status = status
9
- @action = action
10
- @meta = meta
11
- @custom_data = custom_data
12
- end
13
-
14
- def format
15
- [status, custom_data || meta.id]
16
- end
17
- end
18
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Logux
4
- class Stream
5
- attr_reader :stream
6
-
7
- delegate :close, to: :stream
8
-
9
- def initialize(stream)
10
- @stream = stream
11
- end
12
-
13
- def write(payload)
14
- processed_payload = process(payload)
15
- Logux.logger.debug("Write to Logux response: #{processed_payload}")
16
- stream.write(processed_payload)
17
- end
18
-
19
- private
20
-
21
- def process(payload)
22
- payload.is_a?(::String) ? payload : payload.to_json
23
- end
24
- end
25
- end
@@ -1,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Logux
4
- module Test
5
- class << self
6
- attr_accessor :http_requests_enabled
7
-
8
- def enable_http_requests!
9
- raise ArgumentError unless block_given?
10
-
11
- begin
12
- self.http_requests_enabled = true
13
- yield
14
- ensure
15
- self.http_requests_enabled = false
16
- end
17
- end
18
- end
19
-
20
- module Client
21
- def post(params)
22
- if Logux::Test.http_requests_enabled
23
- super(params)
24
- else
25
- Logux::Test::Store.instance.add(params.to_json)
26
- end
27
- end
28
- end
29
-
30
- autoload :Helpers, 'logux/test/helpers'
31
- autoload :Store, 'logux/test/store'
32
- autoload :Matchers, 'logux/test/matchers'
33
- end
34
- end
35
- Logux::Client.prepend Logux::Test::Client
@@ -1,75 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Logux
4
- module Test
5
- module Helpers
6
- extend ActiveSupport::Concern
7
-
8
- included do
9
- before do
10
- Logux::Test::Store.instance.reset!
11
- end
12
- end
13
-
14
- def logux_store
15
- Logux::Test::Store.instance.data
16
- end
17
-
18
- def send_to_logux(*commands)
19
- Logux::Test::Matchers::SendToLogux.new(*commands)
20
- end
21
-
22
- def a_logux_meta_with(attributes = {})
23
- RSpec::Matchers::BuiltIn::Include.new(attributes.stringify_keys)
24
- end
25
- alias a_logux_meta a_logux_meta_with
26
-
27
- def a_logux_action_with(attributes = {})
28
- RSpec::Matchers::BuiltIn::Include.new(attributes.stringify_keys)
29
- end
30
- alias a_logux_action a_logux_action_with
31
-
32
- def logux_approved(meta = nil)
33
- Logux::Test::Matchers::ResponseChunks.new(
34
- meta: meta, includes: ['approved'], excludes: %w[forbidden error]
35
- )
36
- end
37
-
38
- def logux_processed(meta = nil)
39
- Logux::Test::Matchers::ResponseChunks.new(
40
- meta: meta, includes: ['processed'], excludes: %w[forbidden error]
41
- )
42
- end
43
-
44
- def logux_forbidden(meta = nil)
45
- Logux::Test::Matchers::ResponseChunks.new(
46
- meta: meta, includes: ['forbidden']
47
- )
48
- end
49
-
50
- def logux_errored(meta = nil)
51
- Logux::Test::Matchers::ResponseChunks.new(
52
- meta: meta, includes: ['error']
53
- )
54
- end
55
-
56
- def logux_authenticated(meta = nil)
57
- Logux::Test::Matchers::ResponseChunks.new(
58
- meta: meta, includes: ['authenticated']
59
- )
60
- end
61
-
62
- def logux_unauthorized(meta = nil)
63
- Logux::Test::Matchers::ResponseChunks.new(
64
- meta: meta, includes: ['unauthorized']
65
- )
66
- end
67
-
68
- def logux_denied(meta = nil)
69
- Logux::Test::Matchers::ResponseChunks.new(
70
- meta: meta, includes: ['denied']
71
- )
72
- end
73
- end
74
- end
75
- end
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Logux
4
- module Test
5
- module Matchers
6
- autoload :SendToLogux, 'logux/test/matchers/send_to_logux'
7
- autoload :ResponseChunks, 'logux/test/matchers/response_chunks'
8
- end
9
- end
10
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Logux
4
- module Test
5
- module Matchers
6
- class Base
7
- attr_reader :expected
8
-
9
- def initialize(*expected)
10
- @expected = expected
11
- end
12
-
13
- def supports_block_expectations?
14
- true
15
- end
16
-
17
- private
18
-
19
- def pretty(obj)
20
- JSON.pretty_generate(obj)
21
- end
22
- end
23
- end
24
- end
25
- end
@@ -1,48 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'base'
4
-
5
- module Logux
6
- module Test
7
- module Matchers
8
- class ResponseChunks < Base
9
- attr_reader :includes, :excludes, :meta
10
-
11
- def initialize(meta:, includes:, excludes: [])
12
- @meta = meta
13
- @includes = includes
14
- @excludes = excludes
15
- end
16
-
17
- def matches?(actual)
18
- @actual = JSON.parse(actual.body)
19
-
20
- match_includes? && match_excludes?
21
- end
22
-
23
- def failure_message
24
- data = "expected that #{pretty(@actual)} to has " \
25
- "#{includes.join(', ')} chunks"
26
- !excludes.empty? && data += " and doesn't" \
27
- " has #{excludes.join(', ')} chunks"
28
- data
29
- end
30
-
31
- private
32
-
33
- def match_includes?
34
- @actual.any? do |command|
35
- command.first.in?(includes) &&
36
- (meta.nil? || (meta.present? && command[1] == meta))
37
- end
38
- end
39
-
40
- def match_excludes?
41
- @actual.empty? || @actual.none? do |command|
42
- command.first.in?(excludes)
43
- end
44
- end
45
- end
46
- end
47
- end
48
- end