Skip to content

Commit

Permalink
Fix safe offenses in production code (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
tagliala authored Jan 17, 2025
1 parent 0410d49 commit 85285d1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 108 deletions.
91 changes: 3 additions & 88 deletions .rubocop_todo.yml

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

2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ RSpec::Core::RakeTask.new
require 'yard'
YARD::Rake::YardocTask.new

task :default => [:spec, :yard]
task default: %i[spec yard]
4 changes: 2 additions & 2 deletions colore-client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Gem::Specification.new do |spec|
spec.version = Colore::Client::VERSION
spec.authors = ['Joe Blackman']
spec.email = ['[email protected]']
spec.description = %q(Ruby client to consume Colore services)
spec.summary = %q(Ruby client to consume Colore services)
spec.description = 'Ruby client to consume Colore services'
spec.summary = 'Ruby client to consume Colore services'

spec.homepage = 'https://github.com/ifad/colore-client'
spec.license = 'MIT'
Expand Down
19 changes: 9 additions & 10 deletions lib/colore/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ module Colore
CURRENT = 'current'

class Client
attr_accessor :backtrace
attr_accessor :logger
attr_accessor :backtrace, :logger

# Generates a document id that is reasonably guaranteed to be unique for your app
def self.generate_doc_id
Expand All @@ -34,7 +33,7 @@ def self.uri_parser
# @param app [String] The name of your application (all documents will be stored under this name)
# @param logger [Logger] An optional logger, which will log all requests and responses
# @param backtrace [Bool] Used for debugging purposes, to extract backtraces from Colore
def initialize(base_uri: 'http://localhost:9240/', app:, logger: Logger.new(nil), backtrace: false)
def initialize(app:, base_uri: 'http://localhost:9240/', logger: Logger.new(nil), backtrace: false)
@base_uri = base_uri
@app = app
@backtrace = backtrace
Expand All @@ -50,7 +49,7 @@ def generate_doc_id
# Tests the connection with Colore. Will raise an error if the connection cannot be
# established.
def ping
response, body = send_request :get, '', :binary
send_request :get, '', :binary
true
end

Expand Down Expand Up @@ -131,7 +130,7 @@ def update_title(doc_id:, title:)
# have something listening on this URL, ready to take a JSON object with the
# results of the conversion in it.
# @return [Hash] a standard response
def request_conversion(doc_id:, version: CURRENT, filename:, action:, callback_url: nil)
def request_conversion(doc_id:, filename:, action:, version: CURRENT, callback_url: nil)
params = {}
params[:callback_url] = callback_url if callback_url
params[:backtrace] = @backtrace if @backtrace
Expand Down Expand Up @@ -165,7 +164,7 @@ def delete_version(doc_id:, version:)
# @param version [String] the version to delete
# @param filename [String] the name of the file to retrieve
# @return [String] the file contents
def get_document(doc_id:, version: CURRENT, filename:)
def get_document(doc_id:, filename:, version: CURRENT)
params = {}
params[:backtrace] = @backtrace if @backtrace
send_request :get, path_for(doc_id, filename, version), {}, :binary
Expand All @@ -192,7 +191,7 @@ def convert(content:, action:, language: 'en')
params[:action] = action
params[:language] = language if language
params[:backtrace] = @backtrace if @backtrace
response = send_request :post, "convert", params, :binary
response = send_request :post, 'convert', params, :binary
end
response
end
Expand All @@ -203,11 +202,11 @@ def path_for(doc_id, filename, version = 'current')

protected

def url_for_base doc_id
def url_for_base(doc_id)
"/document/#{@app}/#{doc_id}"
end

def send_request type, path, params = {}, expect = :binary
def send_request(type, path, params = {}, expect = :binary)
url = URI.join(@base_uri, path).to_s
logger.debug("Send #{type}: #{url}")
logger.debug(" params: #{params.inspect}")
Expand Down Expand Up @@ -252,7 +251,7 @@ def send_request type, path, params = {}, expect = :binary
# Saves the content into a tempfile, rather than trying to read it all into memory
# This allows us to handle passing an IO for a 300MB file without crashing.
#
def with_tempfile content, &block
def with_tempfile(content)
Tempfile.open('colore') do |tf|
tf.binmode
if content.respond_to?(:read)
Expand Down
12 changes: 5 additions & 7 deletions lib/colore/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ module Colore
module Errors
class ColoreUnavailable < StandardError
def initialize
super "The Colore storage system is unavailable"
super('The Colore storage system is unavailable')
end
end

class APIError < StandardError
attr_accessor :http_code
attr_accessor :response_body
attr_accessor :rsp_backtrace
attr_accessor :http_code, :response_body, :rsp_backtrace

def initialize http_code, message, rsp_backtrace = nil, response_body = nil
super message
def initialize(http_code, message, rsp_backtrace = nil, response_body = nil)
super(message)
@http_code = http_code
@response_body = response_body
@rsp_backtrace = rsp_backtrace
Expand All @@ -22,7 +20,7 @@ def initialize http_code, message, rsp_backtrace = nil, response_body = nil
class ClientError < APIError; end
class ServerError < APIError; end

def self.from hash, body
def self.from(hash, body)
if hash.nil?
ServerError.new(0, 'Unknown error (see response_body)', nil, body)
else
Expand Down

0 comments on commit 85285d1

Please sign in to comment.