Skip to content

Commit

Permalink
Merge pull request #30 from DmitryTsepelev/happy_rubocop
Browse files Browse the repository at this point in the history
Fix some Rubocop offences + minor refactoring
  • Loading branch information
wilddima committed Nov 6, 2018
2 parents fec57b2 + 52c7f02 commit 9e43a64
Show file tree
Hide file tree
Showing 19 changed files with 101 additions and 85 deletions.
11 changes: 3 additions & 8 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ Metrics/BlockLength:
- 'logux_rails.gemspec'
- 'lib/logux/test/helpers.rb'

Metrics/AbcSize:
Exclude:
- 'app/controllers/logux_controller.rb'

Metrics/MethodLength:
Exclude:
- 'app/controllers/logux_controller.rb'

RSpec/DescribeClass:
Exclude:
- 'spec/logux/tasks/*.rb'
Expand All @@ -35,3 +27,6 @@ RSpec/ExpectChange:

Style/RescueStandardError:
EnforcedStyle: implicit

Style/DateTime:
Enabled: false
10 changes: 0 additions & 10 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
Metrics/AbcSize:
Max: 19

# Offense count: 17
Style/Documentation:
Enabled: false
Expand All @@ -19,9 +15,3 @@ Style/MixinUsage:
Exclude:
- 'spec/dummy/bin/setup'
- 'spec/dummy/bin/update'

# Offense count: 5
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 85
22 changes: 12 additions & 10 deletions app/controllers/logux_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,35 @@ def create
Logux.verify_request_meta_data(meta_params)
logux_stream.write('[')
Logux.process_batch(stream: logux_stream, batch: command_params)
rescue => e
begin
Logux.configuration.on_error.call(e)
Logux.logger.error("#{e}\n#{e.backtrace.join("\n")}")
ensure
logux_stream.write(Logux::ErrorRenderer.new(e).message)
end
rescue => ex
handle_processing_errors(ex)
ensure
logux_stream.write(']')
logux_stream.close
end

private

def logux_params
def unsafe_params
params.to_unsafe_h
end

def command_params
logux_params.dig('commands')
unsafe_params.dig('commands')
end

def meta_params
logux_params&.slice(:version, :password)
unsafe_params&.slice(:version, :password)
end

def logux_stream
@logux_stream ||= Logux::Stream.new(response.stream)
end

def handle_processing_errors(exception)
Logux.configuration.on_error.call(exception)
Logux.logger.error("#{e}\n#{e.backtrace.join("\n")}")
ensure
logux_stream.write(Logux::ErrorRenderer.new(exception).message)
end
end
4 changes: 3 additions & 1 deletion lib/logux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ def initialize(msg, meta: nil)
config.logux_host = 'localhost:1338'
config.verify_authorized = true
config.logger = ActiveSupport::Logger.new(STDOUT)
config.logger = Rails.logger if defined?(Rails) && Rails.respond_to?(:logger)
if defined?(Rails) && Rails.respond_to?(:logger)
config.logger = Rails.logger
end
config.on_error = proc {}
config.auth_rule = proc { false }
end
Expand Down
21 changes: 12 additions & 9 deletions lib/logux/action_caller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,37 @@

module Logux
class ActionCaller
attr_reader :action, :meta, :action_controller
attr_reader :action, :meta

delegate :logger, to: :Logux

def initialize(action:, meta:)
@action = action
@meta = meta
end

def call!
Logux.logger
.info("Searching action for Logux action: #{action}, meta: #{meta}")
action_class = class_finder.find_action_class
@action_controller = action_class.new(action: action, meta: meta)
logger.info("Searching action for Logux action: #{action}, meta: #{meta}")
format(action_controller.public_send(action.action_type))
rescue Logux::UnknownActionError, Logux::UnknownChannelError => e
Logux.logger.warn(e)
logger.warn(e)
format(nil)
end

private

def format(response)
return response if response.is_a? Logux::Response
Logux::Response
.new(:processed, action: action, meta: meta)
return response if response.is_a?(Logux::Response)

Logux::Response.new(:processed, action: action, meta: meta)
end

def class_finder
@class_finder ||= Logux::ClassFinder.new(action: action, meta: meta)
end

def action_controller
class_finder.find_action_class.new(action: action, meta: meta)
end
end
end
28 changes: 14 additions & 14 deletions lib/logux/class_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,19 @@ def initialize(action:, meta:)
def find_action_class
"#{class_namespace}::#{class_name}".constantize
rescue NameError
message = %(
Unable to find action #{class_name.camelize}
Should be in app/logux/#{class_namespace.downcase}/#{class_path}.rb
)
raise Logux::UnknownActionError.new(message, meta: meta) if action?
raise Logux::UnknownChannelError.new(message, meta: meta)
message =
"Unable to find action #{class_name.camelize}.\n" \
"Should be in app/logux/#{class_namespace.downcase}/#{class_path}.rb"
raise_error_for_failed_find(message)
end

def find_policy_class
"Policies::#{class_namespace}::#{class_name}".constantize
rescue NameError
message = %(
Unable to find action policy #{class_name.camelize}
Should be in app/logux/policies/#{class_namespace.downcase}/#{class_path}.rb
)
raise Logux::UnknownActionError.new(message, meta: meta) if action?
raise Logux::UnknownChannelError.new(message, meta: meta)
message =
"Unable to find action policy #{class_name.camelize}.\n" \
"Should be in app/logux/#{class_namespace.downcase}/#{class_path}.rb"
raise_error_for_failed_find(message)
end

def class_name
Expand All @@ -42,8 +38,7 @@ def class_name
private

def class_namespace
return 'Channels' if subscribe?
'Actions'
subscribe? ? 'Channels' : 'Actions'
end

def subscribe?
Expand All @@ -57,5 +52,10 @@ def action?
def class_path
"#{class_namespace}::#{class_name}".underscore
end

def raise_error_for_failed_find(message)
exception_class = action? ? UnknownActionError : UnknownChannelError
raise exception_class.new(message, meta: meta)
end
end
end
20 changes: 13 additions & 7 deletions lib/logux/policy_caller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,32 @@

module Logux
class PolicyCaller
attr_reader :action, :meta, :policy
attr_reader :action, :meta

delegate :logger, :configuration, to: :Logux

def initialize(action:, meta:)
@action = action
@meta = meta
end

def call!
Logux.logger
.info("Searching policy for Logux action: #{action}, meta: #{meta}")
policy_class = class_finder.find_policy_class
@policy = policy_class.new(action: action, meta: meta)
logger.info("Searching policy for Logux action: #{action}, meta: #{meta}")
policy.public_send("#{action.action_type}?")
rescue Logux::UnknownActionError, Logux::UnknownChannelError => e
raise e if Logux.configuration.verify_authorized
Logux.logger.warn(e)
raise e if configuration.verify_authorized

logger.warn(e)
end

private

def class_finder
@class_finder ||= Logux::ClassFinder.new(action: action, meta: meta)
end

def policy
class_finder.find_policy_class.new(action: action, meta: meta)
end
end
end
13 changes: 8 additions & 5 deletions lib/logux/process/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ def stop_process!

def process_action!
return if stop_process?
stream.write(Logux::ActionCaller
.new(action: action_from_chunk,
meta: meta_from_chunk)
.call!
.format)

action_caller = Logux::ActionCaller.new(
action: action_from_chunk,
meta: meta_from_chunk
)

stream.write(action_caller.call!.format)
end

def process_authorization!
Expand All @@ -50,6 +52,7 @@ def process_authorization!
status = policy_check ? :approved : :forbidden
stream.write([status, meta_from_chunk.id])
return stream.write(',') if policy_check

stop_process!
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/logux/process/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def initialize(stream:, chunk:)
def call
authed = Logux.configuration.auth_rule.call(user_id, chunk.credentials)
return stream.write(['authenticated', chunk.auth_id]) if authed

stream.write(['denied', chunk.auth_id])
end

Expand Down
3 changes: 1 addition & 2 deletions lib/logux/stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ def write(payload)
private

def process(payload)
return payload if payload.is_a?(::String)
payload.to_json
payload.is_a?(::String) ? payload : payload.to_json
end
end
end
18 changes: 12 additions & 6 deletions lib/logux/test/matchers/send_to_logux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ module Test
module Matchers
class SendToLogux < Base
def matches?(actual)
before_state = Logux::Test::Store.instance.data.dup
actual.call
after_state = Logux::Test::Store.instance.data
@difference = (after_state - before_state)
.map { |d| JSON.parse(d) }
.map(&:deep_symbolize_keys)
@difference = state_changes_inside { actual.call }
@difference.find do |state|
state.merge(expected || {}).deep_symbolize_keys == state
end
Expand All @@ -21,6 +16,17 @@ def matches?(actual)
def failure_message
"expected that #{pretty(@difference)} to include #{pretty(expected)}"
end

private

def state_changes_inside
before_state = Logux::Test::Store.instance.data.dup
yield
after_state = Logux::Test::Store.instance.data

(after_state - before_state).map { |d| JSON.parse(d) }
.map(&:deep_symbolize_keys)
end
end
end
end
Expand Down
5 changes: 2 additions & 3 deletions lib/tasks/logux_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ namespace :logux do

desc 'Lists all Logux channels'
task channels: :environment do
Dir[Rails.root.join('app', 'logux', 'channels', '**', '*.rb')].each do |file|
require file
end
path = Rails.root.join('app', 'logux', 'channels', '**', '*.rb')
Dir[path].each { |file| require file }

output = [%w[channel Class]]
Logux::ChannelController.descendants.map(&:name).sort.each do |klass_name|
Expand Down
2 changes: 1 addition & 1 deletion logux_rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec-live_controllers'
spec.add_development_dependency 'rspec-rails'
spec.add_development_dependency 'rubocop', '~> 0.57.0'
spec.add_development_dependency 'rubocop', '~> 0.60.0'
spec.add_development_dependency 'rubocop-rspec', '~> 1.27.0'
spec.add_development_dependency 'simplecov'
spec.add_development_dependency 'sqlite3'
Expand Down
3 changes: 2 additions & 1 deletion spec/dummy/config/environments/development.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# frozen_string_literal: true

Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# Settings specified here will take precedence over those in
# config/application.rb.

# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
Expand Down
3 changes: 2 additions & 1 deletion spec/dummy/config/environments/test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# frozen_string_literal: true

Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# Settings specified here will take precedence over those in
# config/application.rb.

# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
Expand Down
6 changes: 3 additions & 3 deletions spec/factories/logux_actions_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
end

factory :logux_actions_unknown do
type 'unknown/action'
type { 'unknown/action' }
end

factory :logux_actions_unknown_subscribe do
type 'logux/subscribe'
channel 'unknown/channel'
type { 'logux/subscribe' }
channel { 'unknown/channel' }
end
end
end
4 changes: 3 additions & 1 deletion spec/logux/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
require 'spec_helper'

describe Logux::Actions do
let(:actions) { described_class.new(type: 'user/add', channel: 'project/123') }
let(:actions) do
described_class.new(type: 'user/add', channel: 'project/123')
end

describe '#action_type' do
subject { actions.action_type }
Expand Down
4 changes: 3 additions & 1 deletion spec/logux/class_finder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
describe '#class_name' do
subject(:class_name) { finder.class_name }

let(:action) { create(:logux_actions_add, type: 'test/test/name/try/user/add') }
let(:action) do
create(:logux_actions_add, type: 'test/test/name/try/user/add')
end

it 'returns nested classes' do
expect(class_name).to eq('Test::Test::Name::Try::User')
Expand Down
Loading

0 comments on commit 9e43a64

Please sign in to comment.