Skip to content

Commit

Permalink
fixed all rubocop offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanRada committed Aug 8, 2016
1 parent 514540f commit 61434fb
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 46 deletions.
7 changes: 6 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
AllCops:
Exclude:
- washout_builder.gemspec
- celluloid_pubsub.gemspec
- bin/**/*
- Guardfile
- vendor/**/**
- examples/**/**
- spec/**/*
- Gemfile
- Rakefile

TargetRubyVersion: 2.3

ClassLength:
Max: 500

Expand Down
2 changes: 2 additions & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# encoding: utf-8
# frozen_string_literal: true
require 'celluloid_pubsub'
2 changes: 2 additions & 0 deletions lib/celluloid_pubsub.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# encoding: utf-8
# frozen_string_literal: true
require 'rubygems'
require 'bundler'
require 'bundler/setup'
Expand Down
7 changes: 4 additions & 3 deletions lib/celluloid_pubsub/base_actor.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# encoding: utf-8
# frozen_string_literal: true
require_relative './helper'
module CelluloidPubsub
# base actor used for compatibility between celluloid versions
module BaseActor

class << self
include Helper

attr_reader :config

def included(base)
Expand All @@ -31,7 +33,7 @@ def celluloid_logger_class
def celluloid_version
find_loaded_gem_property('celluloid', 'version')
end

def version_less_than_seventeen?
verify_gem_version(celluloid_version, '0.17', operator: '<')
end
Expand All @@ -43,7 +45,6 @@ def setup_actor_supervision(class_name, options)
class_name.supervise(as: options[:actor_name], args: [options[:args]].compact)
end
end

end
end
end
Expand Down
12 changes: 4 additions & 8 deletions lib/celluloid_pubsub/client.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# encoding: utf-8
# frozen_string_literal: true
require_relative './helper'
module CelluloidPubsub
# worker that subscribes to a channel or publishes to a channel
Expand All @@ -15,7 +17,6 @@ module CelluloidPubsub
class Client
include CelluloidPubsub::BaseActor


attr_accessor :actor, :options, :channel
finalizer :shutdown
# receives a list of options that are used to connect to the webserver and an actor to which the callbacks are delegated to
Expand Down Expand Up @@ -88,7 +89,7 @@ def hostname
#
# @api public
def port
@port ||= @options.fetch('port', nil) || CelluloidPubsub::WebServer.find_unused_port
@port ||= @options.fetch('port', nil) || CelluloidPubsub::WebServer.find_unused_port
end

# the method will return the path of the URL on which the servers acccepts the connection
Expand Down Expand Up @@ -253,12 +254,7 @@ def send_action(action, channel = nil, data = {})
#
# @api private
def chat(message)
final_message = nil
if message.is_a?(Hash)
final_message = message.to_json
else
final_message = JSON.dump(action: 'message', message: message)
end
final_message = message.is_a?(Hash) ? message.to_json : JSON.dump(action: 'message', message: message)
log_debug("#{@actor.class} sends JSON #{final_message}")
connection.text final_message
end
Expand Down
4 changes: 3 additions & 1 deletion lib/celluloid_pubsub/helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# encoding: utf-8
# frozen_string_literal: true
module CelluloidPubsub
# class that holds the options that are configurable for this gem
module Helper
Expand All @@ -12,7 +14,7 @@ def succesfull_subscription?(message)
message.is_a?(Hash) && message['client_action'] == 'successful_subscription'
end

module_function
module_function

def find_loaded_gem(name, property = nil)
gem_spec = Gem.loaded_specs.values.find { |repo| repo.name == name }
Expand Down
4 changes: 3 additions & 1 deletion lib/celluloid_pubsub/initializers/reel_colors.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# encoding: utf-8
# frozen_string_literal: true
require 'reel/spy'
Reel::Spy::Colors.class_eval do
alias_method :original_colorize, :colorize

def colorize(n, str)
def colorize(_n, str)
force_utf8_encoding(str)
end

Expand Down
26 changes: 14 additions & 12 deletions lib/celluloid_pubsub/reactor.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# encoding: utf-8
# frozen_string_literal: true
require_relative './registry'
require_relative './helper'
module CelluloidPubsub
Expand Down Expand Up @@ -165,18 +167,18 @@ def handle_parsed_websocket_message(json_data)
def delegate_action(json_data)
channel = json_data.fetch('channel', nil)
case json_data['client_action']
when 'unsubscribe_all'
unsubscribe_all
when 'unsubscribe_clients'
async.unsubscribe_clients(channel)
when 'unsubscribe'
async.unsubscribe(channel)
when 'subscribe'
async.start_subscriber(channel, json_data)
when 'publish'
async.publish_event(channel, json_data['data'].to_json)
else
handle_unknown_action(json_data)
when 'unsubscribe_all'
unsubscribe_all
when 'unsubscribe_clients'
async.unsubscribe_clients(channel)
when 'unsubscribe'
async.unsubscribe(channel)
when 'subscribe'
async.start_subscriber(channel, json_data)
when 'publish'
async.publish_event(channel, json_data['data'].to_json)
else
handle_unknown_action(json_data)
end
end

Expand Down
2 changes: 2 additions & 0 deletions lib/celluloid_pubsub/registry.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# encoding: utf-8
# frozen_string_literal: true
module CelluloidPubsub
# class used to register new channels and save them in memory
class Registry
Expand Down
2 changes: 2 additions & 0 deletions lib/celluloid_pubsub/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# encoding: utf-8
# frozen_string_literal: true
# Returns the version of the gem as a <tt>Gem::Version</tt>
module CelluloidPubsub
# it prints the gem version as a string
Expand Down
37 changes: 17 additions & 20 deletions lib/celluloid_pubsub/web_server.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# encoding: utf-8
# frozen_string_literal: true
require_relative './reactor'
require_relative './helper'
module CelluloidPubsub
Expand All @@ -20,7 +22,7 @@ class WebServer < Reel::Server::HTTP
HOST = '0.0.0.0'
# The request path that the webserver accepts by default
PATH = '/ws'
# The name of the default adapter
# The name of the default adapter
CLASSIC_ADAPTER = 'classic'

attr_accessor :server_options, :subscribers, :mutex
Expand Down Expand Up @@ -55,12 +57,12 @@ def initialize(options = {})
#
# @api public
def self.open_socket_on_unused_port
infos = ::Socket::getaddrinfo("localhost", nil, Socket::AF_UNSPEC, Socket::SOCK_STREAM, 0, Socket::AI_PASSIVE)
families = Hash[*infos.collect { |af, *_| af }.uniq.zip([]).flatten]
infos = ::Socket::getaddrinfo('localhost', nil, Socket::AF_UNSPEC, Socket::SOCK_STREAM, 0, Socket::AI_PASSIVE)
families = Hash[*infos.map { |af, *_| af }.uniq.zip([]).flatten]

return ::TCPServer.open('0.0.0.0', 0) if families.has_key?('AF_INET')
return ::TCPServer.open('::', 0) if families.has_key?('AF_INET6')
return ::TCPServer.open(0)
return ::TCPServer.open('0.0.0.0', 0) if families.key?('AF_INET')
return ::TCPServer.open('::', 0) if families.key?('AF_INET6')
::TCPServer.open(0)
end

# the method get from the socket connection that is already opened the port used.
Expand All @@ -70,7 +72,7 @@ def self.open_socket_on_unused_port
#
# @api public
def self.find_unused_port
@@unused_port ||= begin
@unused_port ||= begin
socket = open_socket_on_unused_port
port = socket.addr[1]
socket.close
Expand Down Expand Up @@ -105,7 +107,7 @@ def adapter
#
# @api public
def debug_enabled?
@debug_enabled = @server_options.fetch('enable_debug', false)
@debug_enabled = @server_options.fetch('enable_debug', true)
@debug_enabled == true
end

Expand Down Expand Up @@ -180,7 +182,6 @@ def backlog
@backlog = @server_options.fetch('backlog', 1024)
end


# callback that will execute when receiving new conections
# If the connections is a websocket will call method {#route_websocket}
# and if the connection is HTTP will call method {#route_request}
Expand Down Expand Up @@ -263,13 +264,13 @@ def route_request(connection, request)
#
# @api public
def route_websocket(reactor, socket)
url = socket.url
if url == path || url == "/?"
url = socket.url
if url == path || url == '/?'
reactor.async.work(socket, Actor.current)
else
log_debug "Received invalid WebSocket request for: #{url}"
socket.close
end
else
log_debug "Received invalid WebSocket request for: #{url}"
socket.close
end
end

# If the message can be parsed into a Hash it will respond to the reactor's websocket connection with the same message in JSON format
Expand All @@ -284,11 +285,7 @@ def route_websocket(reactor, socket)
def handle_dispatched_message(reactor, data)
log_debug "#{self.class} trying to dispatch message #{data.inspect}"
message = reactor.parse_json_data(data)
if message.present? && message.is_a?(Hash)
final_data = message.to_json
else
final_data = data.to_json
end
final_data = message.present? && message.is_a?(Hash) ? message.to_json : data.to_json
reactor.websocket << final_data
end
end
Expand Down

0 comments on commit 61434fb

Please sign in to comment.