Skip to content

Commit

Permalink
Fix Style/MethodDefParentheses offense
Browse files Browse the repository at this point in the history
  • Loading branch information
tagliala committed Sep 13, 2024
1 parent 2df81a9 commit 5bc8e7f
Show file tree
Hide file tree
Showing 23 changed files with 72 additions and 78 deletions.
6 changes: 0 additions & 6 deletions .rubocop_todo.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions lib/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ class App < Sinatra::Base

helpers do
# Renders all responses (including errors) in a standard JSON format.
def respond status, message, extra = {}
def respond(status, message, extra = {})
case status
when Colore::Error
status = status.http_code
Expand All @@ -270,7 +270,7 @@ def respond status, message, extra = {}
}.merge(extra).to_json
end

def respond_with_error error
def respond_with_error(error)
log = ''
log << "While processing #{request.request_method} #{request.path} with params:\n"
log << request.params.pretty_inspect
Expand All @@ -284,7 +284,7 @@ def respond_with_error error
end

# Renders all responses (including errors) in a standard JSON format.
def legacy_error status, message, extra = {}
def legacy_error(status, message, extra = {})
case status
when Error
status = status.http_code
Expand Down
2 changes: 1 addition & 1 deletion lib/autoheathen/config.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module AutoHeathen
module Config
def load_config defaults = {}, config_file = nil, overwrites = {}
def load_config(defaults = {}, config_file = nil, overwrites = {})
cfg = symbolize_keys(defaults)
if config_file && File.exist?(config_file)
cfg.merge! symbolize_keys(YAML::load_file config_file)
Expand Down
16 changes: 8 additions & 8 deletions lib/autoheathen/email_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class EmailProcessor
# text_template: 'config/response.text.haml' Template for text part of response email (mode in [:return_to_sender,:email])
# html_template: 'config/response.html.haml' Template for HTML part of response email (ditto)
# logger: nil Optional logger object
def initialize cfg = {}, config_file = nil
def initialize(cfg = {}, config_file = nil)
@cfg = load_config({ # defaults
deliver: true,
language: 'en',
Expand All @@ -45,14 +45,14 @@ def initialize cfg = {}, config_file = nil
@logger.level = @cfg[:verbose] ? Logger::DEBUG : Logger::INFO
end

def process_rts email
def process_rts(email)
process email, email.from, true
end

# Processes the given email, submits attachments to the Heathen server, delivers responses as configured
# @param email [String] The encoded email (suitable to be decoded using Mail.read(input))
# @return [Hash] a hash of the decoded attachments (or the reason why they could not be decoded)
def process email, mail_to, is_rts = false
def process(email, mail_to, is_rts = false)
documents = []

unless email.has_attachments?
Expand Down Expand Up @@ -105,7 +105,7 @@ def process email, mail_to, is_rts = false
end

# Forward the email to sender, with decoded documents replacing the originals
def deliver_onward email, documents, mail_to
def deliver_onward(email, documents, mail_to)
logger.info "Sending response mail to #{mail_to}"
email.cc [] # No CCing, just send to the recipient
email.to mail_to
Expand Down Expand Up @@ -136,7 +136,7 @@ def deliver_onward email, documents, mail_to
end

# Send decoded documents back to sender
def deliver_rts email, documents, mail_to
def deliver_rts(email, documents, mail_to)
logger.info "Sending response mail to #{mail_to}"
mail = Mail.new
mail.from @cfg[:from]
Expand Down Expand Up @@ -172,7 +172,7 @@ def deliver_rts email, documents, mail_to
end

# Convenience method allowing us to stub out actual mail delivery in RSpec
def deliver mail
def deliver(mail)
if @cfg[:deliver]
mail.deliver!
logger.debug "Files were emailed to #{mail.to}"
Expand All @@ -182,7 +182,7 @@ def deliver mail
end

# Opens and reads a file, first given the filename, then tries from the project base directory
def read_file filename
def read_file(filename)
f = filename
unless File.exist? f
f = Pathname.new(__FILE__).realpath.parent.parent.parent + f
Expand All @@ -192,7 +192,7 @@ def read_file filename

# Returns the correct conversion action based on the content type
# @raise RuntimeError if there is no conversion action for the content type
def get_action content_type
def get_action(content_type)
ct = content_type.gsub(/;.*/, '')
op = {
'application/pdf' => 'ocr',
Expand Down
6 changes: 3 additions & 3 deletions lib/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Colore
# The Colore Converter is a glue class to allow Colore to access the Heathen conversion
# system.
class Converter
def initialize storage_dir: C_.storage_directory, logger: Logger.new(C_.conversion_log || STDOUT)
def initialize(storage_dir: C_.storage_directory, logger: Logger.new(C_.conversion_log || STDOUT))
@storage_dir = storage_dir
@logger = logger
end
Expand All @@ -17,7 +17,7 @@ def initialize storage_dir: C_.storage_directory, logger: Logger.new(C_.conversi
# @param filename [String] the name of the file to convert
# @param action [String] the conversion to perform
# @return the converted file name
def convert doc_key, version, filename, action
def convert(doc_key, version, filename, action)
doc = Document.load @storage_dir, doc_key
ignore, orig_content = doc.get_file(version, filename)
language = 'en' # TODO - add to spec and upload
Expand All @@ -36,7 +36,7 @@ def convert doc_key, version, filename, action
# @param orig_content [String] the body of the file to convert
# @param language [String] the file's language
# @return [String] the converted file body
def convert_file action, orig_content, language = 'en'
def convert_file(action, orig_content, language = 'en')
Heathen::Converter.new(logger: @logger).convert(action, orig_content, language)
rescue Heathen::TaskNotFound => e
raise InvalidAction.new(e.message)
Expand Down
6 changes: 3 additions & 3 deletions lib/doc_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class DocKey
attr_accessor :doc_id

# Constructs [DocKey] from the input string, which should be the output from [#to_s].
def self.parse doc_key_str
def self.parse(doc_key_str)
self.new *(doc_key_str.split '/')
end

# Constructor. The app and doc_id may only be comprised of alphanumeric letters and
# the special characters "_" and "-".
# @param app [String] the app name
# @param doc_id [String] the document identifier
def initialize app, doc_id
def initialize(app, doc_id)
validate(app)
validate(doc_id)
@app = app
Expand All @@ -47,7 +47,7 @@ def subdirectory
private

# Validates the parameter (app or doc_id)
def validate val
def validate(val)
raise InvalidParameter.new unless val =~ /^[A-Za-z0-9_-]+$/
end
end
Expand Down
28 changes: 14 additions & 14 deletions lib/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ class Document
# @param base_dir [String] The base path to the storage area
# @param doc_key [DocKey] The document identifier
# @return [Pathname]
def self.directory base_dir, doc_key
def self.directory(base_dir, doc_key)
Pathname.new(base_dir) + doc_key.path
end

# Returns true if the document exists
# @param base_dir [String] The base path to the storage area
# @param doc_key [DocKey] The document identifier
# @return [Bool]
def self.exists? base_dir, doc_key
def self.exists?(base_dir, doc_key)
File.exist? directory(base_dir, doc_key)
end

# Creates the document directory. Raises [DocumentExists] if the document already exists.
# @param base_dir [String] The base path to the storage area
# @param doc_key [DocKey] The document identifier
# @return [Document]
def self.create base_dir, doc_key
def self.create(base_dir, doc_key)
doc_dir = directory base_dir, doc_key
raise DocumentExists.new if File.exist? doc_dir

Expand All @@ -55,7 +55,7 @@ def self.create base_dir, doc_key
# @param base_dir [String] The base path to the storage area
# @param doc_key [DocKey] The document identifier
# @return [Document]
def self.load base_dir, doc_key
def self.load(base_dir, doc_key)
raise DocumentNotFound.new unless exists? base_dir, doc_key

doc = self.new base_dir, doc_key
Expand All @@ -65,7 +65,7 @@ def self.load base_dir, doc_key
# @param base_dir [String] The base path to the storage area
# @param doc_key [DocKey] The document identifier
# @return [void].
def self.delete base_dir, doc_key
def self.delete(base_dir, doc_key)
return unless exists? base_dir, doc_key

FileUtils.rm_rf directory(base_dir, doc_key)
Expand All @@ -74,7 +74,7 @@ def self.delete base_dir, doc_key
# Constructor.
# @param base_dir [String] The base path to the storage area
# @param doc_key [DocKey] The document identifier
def initialize base_dir, doc_key
def initialize(base_dir, doc_key)
@base_dir = base_dir
@doc_key = doc_key
end
Expand All @@ -92,7 +92,7 @@ def title
end

# Sets the document title.
def title= new_title
def title=(new_title)
return if new_title.to_s.empty?

File.open(directory + 'title', 'w') { |f| f.puts new_title }
Expand All @@ -105,7 +105,7 @@ def versions
end

# Returns true if the document has the specified version.
def has_version? version
def has_version?(version)
versions.include?(version) || version == CURRENT
end

Expand All @@ -122,7 +122,7 @@ def next_version_number

# Creates a new version, ready to store documents in
# Work is performed in a flock block to avoid concurrent race condition
def new_version &block
def new_version(&block)
lockfile = directory + '.lock'
nvn = nil
lockfile.open 'w' do |f|
Expand All @@ -140,7 +140,7 @@ def new_version &block
# @param body [String or IO] the file contents (binary string or IO)
# @param author [String] the author of the file (optional)
# @return [void]
def add_file version, filename, body, author = nil
def add_file(version, filename, body, author = nil)
raise VersionNotFound.new unless File.exist?(directory + version)

body = StringIO.new(body) unless body.respond_to?(:read) # string -> IO
Expand All @@ -149,7 +149,7 @@ def add_file version, filename, body, author = nil
end

# Sets the specified version as current.
def set_current version
def set_current(version)
raise VersionNotFound.new unless File.exist?(directory + version)
raise InvalidVersion.new unless version =~ /^v\d+$/

Expand All @@ -159,7 +159,7 @@ def set_current version
end

# Deletes the given version, including its files.
def delete_version version
def delete_version(version)
return unless File.exist?(directory + version)
raise VersionIsCurrent.new if version == CURRENT
raise VersionIsCurrent.new if (directory + CURRENT).realpath == (directory + version).realpath
Expand All @@ -172,7 +172,7 @@ def delete_version version
#
# f = "http://colore:1234/#{doc.file_path 'v001', 'fred.docx'}"
#
def file_path version, filename
def file_path(version, filename)
# TODO: don't like this hard-code
# it should really be in the app, but the hash is generated here
"/document/#{@doc_key.app}/#{@doc_key.doc_id}/#{version}/#{filename}"
Expand All @@ -183,7 +183,7 @@ def file_path version, filename
# @param filename [String] the name of the file
# @return [String] mime type
# @return [String] the file body
def get_file version, filename
def get_file(version, filename)
path = directory + version + filename
raise FileNotFound unless File.exist? path

Expand Down
8 changes: 4 additions & 4 deletions lib/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ module Colore
class Error < StandardError
attr_accessor :http_code

def initialize http_code, message
def initialize(http_code, message)
super message
@http_code = http_code
end
end

class InvalidParameter < Error
def initialize param = nil; super 400, "Invalid parameter #{param}"; end
def initialize(param = nil); super 400, "Invalid parameter #{param}"; end
end

class DocumentExists < Error
Expand All @@ -33,15 +33,15 @@ def initialize; super 400, 'Version is current, change current version first'; e
end

class InvalidAction < Error
def initialize message = nil; super 400, (message || 'Invalid action'); end
def initialize(message = nil); super 400, (message || 'Invalid action'); end
end

class FileNotFound < Error
def initialize; super 400, 'Requested file not found'; end
end

class ConversionError < Error
def initialize heathen_error
def initialize(heathen_error)
super 500, "Conversion error: #{heathen_error.message}"
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/heathen/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize(logger: Logger.new(nil))
# @param content [String] the document body to be converted
# @param language [String] the document language (defaults to 'en')
# @return [String] the converted document body
def convert action, content, language = 'en'
def convert(action, content, language = 'en')
job = Job.new action, content, language
processor = Heathen::Processor.new job: job, logger: @logger
begin
Expand Down
Loading

0 comments on commit 5bc8e7f

Please sign in to comment.