From 86ae972fc4280ef6a1c4d8fc2c6317823a3b9adf Mon Sep 17 00:00:00 2001 From: Maciej Skierkowski Date: Tue, 29 Jul 2014 11:01:01 -0700 Subject: [PATCH] more rubocop cleanup --- Rakefile | 7 ++++--- factor.gemspec | 31 +++++++++++++++++-------------- lib/commands/base.rb | 25 +++++++++++-------------- lib/commands/workflows.rb | 10 ++++------ lib/factor.rb | 2 +- lib/listener.rb | 14 +++++--------- lib/runtime.rb | 11 +++++------ lib/websocket_manager.rb | 2 +- spec/workflow_spec.rb | 2 +- 9 files changed, 49 insertions(+), 55 deletions(-) diff --git a/Rakefile b/Rakefile index bd73288..17545b3 100644 --- a/Rakefile +++ b/Rakefile @@ -1,11 +1,12 @@ +# encoding: UTF-8 + require 'bundler/gem_tasks' require 'rspec/core/rake_task' -# RSpec::Core::RakeTask.new(:spec) -desc "Run specs" +desc 'Run specs' RSpec::Core::RakeTask.new do |t| t.verbose = false t.rspec_opts = '--color --order random' end -task :default => :spec \ No newline at end of file +task default: :spec diff --git a/factor.gemspec b/factor.gemspec index c0b5221..edfbb45 100644 --- a/factor.gemspec +++ b/factor.gemspec @@ -1,20 +1,23 @@ - $:.push File.expand_path("../lib", __FILE__) - require 'factor/version' +# encoding: UTF-8 +$LOAD_PATH.push File.expand_path('../lib', __FILE__) +require 'factor/version' - Gem::Specification.new do |s| - s.name = "factor" +Gem::Specification.new do |s| + s.name = 'factor' s.version = Factor::VERSION s.platform = Gem::Platform::RUBY - s.authors = ["Maciej Skierkowski"] - s.email = ["maciej@factor.io"] - s.homepage = "https://factor.io" - s.summary = %q{CLI to manager workflows on Factor.io} - s.description = %q{CLI to manager workflows on Factor.io} + s.authors = ['Maciej Skierkowski'] + s.email = ['maciej@factor.io'] + s.homepage = 'https://factor.io' + s.summary = 'CLI to manager workflows on Factor.io' + s.description = 'CLI to manager workflows on Factor.io' - s.files = %x{git ls-files}.split("\n") - s.test_files = %x{git ls-files -- {test,spec,features}/*}.split("\n") - s.executables = %x{git ls-files -- bin/*}.split("\n").map{ |f| File.basename(f) } - s.require_paths = ["lib"] + s.files = `git ls-files`.split("\n") + s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") + s.executables = `git ls-files -- bin/*`.split("\n").map do |f| + File.basename(f) + end + s.require_paths = ['lib'] s.add_runtime_dependency 'commander', '~> 4.2.0' s.add_runtime_dependency 'rest_client', '~> 1.7.3' @@ -24,4 +27,4 @@ s.add_development_dependency 'codeclimate-test-reporter', '~> 0.3.0' s.add_development_dependency 'rspec', '~> 3.0.0' s.add_development_dependency 'rake', '~> 10.3.2' - end \ No newline at end of file +end diff --git a/lib/commands/base.rb b/lib/commands/base.rb index 00570d6..dbd7f6b 100644 --- a/lib/commands/base.rb +++ b/lib/commands/base.rb @@ -7,7 +7,6 @@ module Factor module Commands - # Base command with common methods used by all commands class Command DEFAULT_FILENAME = { @@ -51,11 +50,11 @@ def load_config(options = {}) def load_config_data(config_type, options = {}) relative_path = options[config_type] || DEFAULT_FILENAME[config_type] absolute_path = File.expand_path(relative_path) - info message: "Loading #{config_type.to_s} from #{absolute_path}" + info message: "Loading #{config_type} from #{absolute_path}" data = YAML.load(File.read(absolute_path)) configatron[config_type].configure_from_hash(data) rescue => ex - exception "Couldn't load #{config_type.to_s} from #{absolute_path}", ex + exception "Couldn't load #{config_type} from #{absolute_path}", ex end def log_line(section, options = {}) @@ -78,19 +77,17 @@ def format_section(section) end def tag(options) - tag = '' - if options['workflow_id'] && options['instance_id'] - tag = "[#{options['workflow_id']}:#{options['instance_id']}]" - elsif options['workflow_id'] && !options['instance_id'] - tag = "[#{options['workflow_id']}]" - elsif !options['workflow_id'] && options['instance_id'] - tag = "[#{options['instance_id']}]" - end - tag + primary = options['service_id'] || options['instance_id'] + secondary = if options['service_id'] && options['instance_id'] + ":#{options['instane_id']}" + else + '' + end + primary ? "[#{primary}#{secondary}]" : '' end def time - Time.now.localtime.strftime('%m/%d/%y %T.%L') + Time.now.localtime.strftime('%m/%d/%y %T.%L') end def write(message) @@ -100,4 +97,4 @@ def write(message) end end end -end \ No newline at end of file +end diff --git a/lib/commands/workflows.rb b/lib/commands/workflows.rb index ef5c261..d72f933 100644 --- a/lib/commands/workflows.rb +++ b/lib/commands/workflows.rb @@ -7,15 +7,13 @@ module Factor module Commands - # Workflow is a Command to start the factor runtime from the CLI class Workflow < Factor::Commands::Command - def initialize @workflows = {} end - def server(args, options) + def server(_args, options) config_settings = {} config_settings[:credentials] = options.credentials config_settings[:connectors] = options.connectors @@ -46,7 +44,7 @@ def load_all_workflows(workflow_filename) def block_until_interupt log_message 'status' => 'info', 'message' => 'Ctrl-c to exit' begin - while true + loop do sleep 1 end rescue Interrupt @@ -75,7 +73,7 @@ def load_workflow(filename) connector_settings = configatron.connectors.to_hash credential_settings = configatron.credentials.to_hash runtime = Runtime.new(connector_settings, credential_settings) - runtime.logger = self.method(:log_message) + runtime.logger = method(:log_message) rescue => ex message = "Couldn't setup workflow process for #{workflow_filename}" exception message, ex @@ -111,4 +109,4 @@ def log_message(message_info) end end end -end \ No newline at end of file +end diff --git a/lib/factor.rb b/lib/factor.rb index aeeca07..8a8f6f5 100644 --- a/lib/factor.rb +++ b/lib/factor.rb @@ -19,4 +19,4 @@ c.when_called Factor::Commands::Workflow, :server end -alias_command 's', 'server' \ No newline at end of file +alias_command 's', 'server' diff --git a/lib/listener.rb b/lib/listener.rb index 65a7057..db43fb2 100644 --- a/lib/listener.rb +++ b/lib/listener.rb @@ -4,10 +4,8 @@ require 'websocket_manager' module Factor - # Class Listener for integrating with connector service class Listener - def initialize(url) @url = url end @@ -16,14 +14,12 @@ def definition get("#{@url}/definition") end - def listener(listener_id, &block) - ws = listen("#{@url}/listeners/#{listener_id}") - ws + def listener(listener_id) + listen("#{@url}/listeners/#{listener_id}") end - def action(action_id, &block) - ws = listen("#{@url}/actions/#{action_id}") - ws + def action(action_id) + listen("#{@url}/actions/#{action_id}") end private @@ -45,4 +41,4 @@ def listen(uri_path) WebSocketManager.new(uri_path) end end -end \ No newline at end of file +end diff --git a/lib/runtime.rb b/lib/runtime.rb index ef299dc..5cdaa2d 100644 --- a/lib/runtime.rb +++ b/lib/runtime.rb @@ -41,17 +41,16 @@ def initialize(connectors, credentials) def load(workflow_definition) EM.run do - self.instance_eval(workflow_definition) + instance_eval(workflow_definition) end end def listen(service_id, listener_id, params = {}, &block) - ws = @connectors[service_id.to_sym].listener(listener_id) handle_on_open(service_id, listener_id, 'Listener', ws, params) - ws.on :close do |event| + ws.on :close do error 'Listener disconnected' if @reconnect warn 'Reconnecting...' @@ -103,7 +102,7 @@ def run(service_id, action_id, params = {}, &block) handle_on_open(service_id, action_id, 'Action', ws, params) - ws.on :error do |event| + ws.on :error do error 'Connection dropped while calling action' end @@ -134,7 +133,7 @@ def run(service_id, action_id, params = {}, &block) private def handle_on_open(service_id, action_id, dsl_type, ws, params) - ws.on :open do |event| + ws.on :open do params.merge!(@credentials[service_id.to_sym] || {}) success "#{dsl_type.capitalize} '#{service_id}::#{action_id}' called" ws.send(params.to_json) @@ -172,4 +171,4 @@ def log_message(message_info) @logger.call(message_info) if @logger end end -end \ No newline at end of file +end diff --git a/lib/websocket_manager.rb b/lib/websocket_manager.rb index 1eebbc0..60f2b00 100644 --- a/lib/websocket_manager.rb +++ b/lib/websocket_manager.rb @@ -92,4 +92,4 @@ def connect end end end -end \ No newline at end of file +end diff --git a/spec/workflow_spec.rb b/spec/workflow_spec.rb index 0f0f7e3..4af1895 100644 --- a/spec/workflow_spec.rb +++ b/spec/workflow_spec.rb @@ -8,4 +8,4 @@ end end -end \ No newline at end of file +end