Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jun 4, 2021
1 parent 00cc04f commit 91f225d
Show file tree
Hide file tree
Showing 152 changed files with 1,919 additions and 1,355 deletions.
141 changes: 140 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -655,4 +655,143 @@ Rails/TimeZone:

Rails/Validation:
Description: 'Use validates :attribute, hash of validations.'
Enabled: false
Enabled: false

Gemspec/DateAssignment: # (new in 1.10)
Enabled: true
Layout/SpaceBeforeBrackets: # (new in 1.7)
Enabled: true
Lint/AmbiguousAssignment: # (new in 1.7)
Enabled: true
Lint/DeprecatedConstants: # (new in 1.8)
Enabled: true
Lint/DuplicateBranch: # (new in 1.3)
Enabled: true
Lint/DuplicateRegexpCharacterClassElement: # (new in 1.1)
Enabled: true
Lint/EmptyBlock: # (new in 1.1)
Enabled: true
Lint/EmptyClass: # (new in 1.3)
Enabled: true
Lint/EmptyInPattern: # (new in 1.16)
Enabled: true
Lint/LambdaWithoutLiteralBlock: # (new in 1.8)
Enabled: true
Lint/NoReturnInBeginEndBlocks: # (new in 1.2)
Enabled: true
Lint/NumberedParameterAssignment: # (new in 1.9)
Enabled: true
Lint/OrAssignmentToConstant: # (new in 1.9)
Enabled: true
Lint/RedundantDirGlobSort: # (new in 1.8)
Enabled: true
Lint/SymbolConversion: # (new in 1.9)
Enabled: true
Lint/ToEnumArguments: # (new in 1.1)
Enabled: true
Lint/TripleQuotes: # (new in 1.9)
Enabled: true
Lint/UnexpectedBlockArity: # (new in 1.5)
Enabled: true
Lint/UnmodifiedReduceAccumulator: # (new in 1.1)
Enabled: true
Style/ArgumentsForwarding: # (new in 1.1)
Enabled: true
Style/CollectionCompact: # (new in 1.2)
Enabled: true
Style/DocumentDynamicEvalDefinition: # (new in 1.1)
Enabled: true
Style/EndlessMethod: # (new in 1.8)
Enabled: true
Style/HashConversion: # (new in 1.10)
Enabled: true
Style/HashExcept: # (new in 1.7)
Enabled: true
Style/IfWithBooleanLiteralBranches: # (new in 1.9)
Enabled: true
Style/InPatternThen: # (new in 1.16)
Enabled: true
Style/MultilineInPatternThen: # (new in 1.16)
Enabled: true
Style/NegatedIfElseCondition: # (new in 1.2)
Enabled: true
Style/NilLambda: # (new in 1.3)
Enabled: true
Style/QuotedSymbols: # (new in 1.16)
Enabled: true
Style/RedundantArgument: # (new in 1.4)
Enabled: true
Style/StringChars: # (new in 1.12)
Enabled: true
Style/SwapValues: # (new in 1.1)
Enabled: true
Rails/ActiveRecordCallbacksOrder: # (new in 2.7)
Enabled: true
Rails/AfterCommitOverride: # (new in 2.8)
Enabled: true
Rails/AttributeDefaultBlockValue: # (new in 2.9)
Enabled: true
Rails/EnvironmentVariableAccess: # (new in 2.10)
Enabled: true
Rails/FindById: # (new in 2.7)
Enabled: true
Rails/Inquiry: # (new in 2.7)
Enabled: true
Rails/MailerName: # (new in 2.7)
Enabled: true
Rails/MatchRoute: # (new in 2.7)
Enabled: true
Rails/NegateInclude: # (new in 2.7)
Enabled: true
Rails/Pluck: # (new in 2.7)
Enabled: true
Rails/PluckInWhere: # (new in 2.7)
Enabled: true
Rails/RenderInline: # (new in 2.7)
Enabled: true
Rails/RenderPlainText: # (new in 2.7)
Enabled: true
Rails/ShortI18n: # (new in 2.7)
Enabled: true
Rails/SquishedSQLHeredocs: # (new in 2.8)
Enabled: true
Rails/TimeZoneAssignment: # (new in 2.10)
Enabled: true
Rails/WhereEquals: # (new in 2.9)
Enabled: true
Rails/WhereExists: # (new in 2.7)
Enabled: true
Rails/WhereNot: # (new in 2.8)
Enabled: true
Performance/AncestorsInclude: # (new in 1.7)
Enabled: true
Performance/BigDecimalWithNumericArgument: # (new in 1.7)
Enabled: true
Performance/BlockGivenWithExplicitBlock: # (new in 1.9)
Enabled: true
Performance/CollectionLiteralInLoop: # (new in 1.8)
Enabled: true
Performance/ConstantRegexp: # (new in 1.9)
Enabled: true
Performance/MapCompact: # (new in 1.11)
Enabled: true
Performance/MethodObjectAsBlock: # (new in 1.9)
Enabled: true
Performance/RedundantEqualityComparisonBlock: # (new in 1.10)
Enabled: true
Performance/RedundantSortBlock: # (new in 1.7)
Enabled: true
Performance/RedundantSplitRegexpArgument: # (new in 1.10)
Enabled: true
Performance/RedundantStringChars: # (new in 1.7)
Enabled: true
Performance/ReverseFirst: # (new in 1.7)
Enabled: true
Performance/SortReverse: # (new in 1.7)
Enabled: true
Performance/Squeeze: # (new in 1.7)
Enabled: true
Performance/StringInclude: # (new in 1.7)
Enabled: true
Performance/Sum: # (new in 1.8)
Enabled: true
15 changes: 10 additions & 5 deletions app/controllers/agents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,39 @@ def crossref
authorize! :import, Crossref
total = Crossref.import

render json: { message: "[Crossref Agent] Queued import for #{total} DOIs." }.to_json, status: :ok
render json: { message: "[Crossref Agent] Queued import for #{total} DOIs." }.to_json,
status: :ok
end

def crossref_orcid
authorize! :import, CrossrefOrcid
total = CrossrefOrcid.import

render json: { message: "[Crossref-ORCID Agent] Queued import for #{total} DOIs." }.to_json, status: :ok
render json: { message: "[Crossref-ORCID Agent] Queued import for #{total} DOIs." }.to_json,
status: :ok
end

def crossref_funder
authorize! :import, CrossrefFunder
total = CrossrefFunder.import

render json: { message: "[Crossref-Funder Agent] Queued import for #{total} DOIs." }.to_json, status: :ok
render json: { message: "[Crossref-Funder Agent] Queued import for #{total} DOIs." }.to_json,
status: :ok
end

def crossref_related
authorize! :import, CrossrefRelated
total = CrossrefRelated.import

render json: { message: "[Crossref-Related Agent] Queued import for #{total} DOIs." }.to_json, status: :ok
render json: { message: "[Crossref-Related Agent] Queued import for #{total} DOIs." }.to_json,
status: :ok
end

def crossref_import
authorize! :import, CrossrefImport
total = CrossrefImport.import

render json: { message: "[Crossref-Import Agent] Queued import for #{total} DOIs." }.to_json, status: :ok
render json: { message: "[Crossref-Import Agent] Queued import for #{total} DOIs." }.to_json,
status: :ok
end
end
42 changes: 21 additions & 21 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class ApplicationController < ActionController::API
include Authenticable
include CanCan::ControllerAdditions
include ErrorSerializable
require 'facets/string/snakecase'
require "facets/string/snakecase"

# include helper module for caching infrequently changing resources
include Cacheable
Expand All @@ -20,31 +20,31 @@ class ApplicationController < ActionController::API
def set_jsonp_format
if params[:callback] && request.get?
self.response_body = "#{params[:callback]}(#{response.body})"
headers["Content-Type"] = 'application/javascript'
headers["Content-Type"] = "application/javascript"
end
end

def set_consumer_header
if current_user
response.headers['X-Credential-Username'] = current_user.uid
response.headers["X-Credential-Username"] = current_user.uid
else
response.headers['X-Anonymous-Consumer'] = true
response.headers["X-Anonymous-Consumer"] = true
end
end

def default_format_json
request.format = :json if request.format.html?
end

#convert parameters with hyphen to parameters with underscore.
# convert parameters with hyphen to parameters with underscore.
# https://stackoverflow.com/questions/35812277/fields-parameters-with-hyphen-in-ruby-on-rails
def transform_params
params.transform_keys! { |key| key.tr('-', '_') }
params.transform_keys! { |key| key.tr("-", "_") }
end

def authenticate_user_from_token!
token = token_from_request_headers
return false unless token.present?
return false if token.blank?

@current_user = User.new(token)
end
Expand All @@ -55,35 +55,35 @@ def current_ability

# from https://github.com/nsarno/knock/blob/master/lib/knock/authenticable.rb
def token_from_request_headers
unless request.headers['Authorization'].nil?
request.headers['Authorization'].split.last
end
request.headers["Authorization"]&.split&.last
end

unless Rails.env.development?
rescue_from *RESCUABLE_EXCEPTIONS do |exception|
status = case exception.class.to_s
when "CanCan::AccessDenied", "JWT::DecodeError" then 401
when "Elasticsearch::Transport::Transport::Errors::NotFound","AbstractController::ActionNotFound", "ActionController::RoutingError" then 404
when "Elasticsearch::Transport::Transport::Errors::NotFound", "AbstractController::ActionNotFound", "ActionController::RoutingError" then 404
when "ActiveModel::ForbiddenAttributesError", "ActionController::ParameterMissing", "ActionController::UnpermittedParameters", "NoMethodError" then 422
else 400
end

if status == 404
message = "The resource you are looking for doesn't exist."
elsif status == 401
message = "You are not authorized to access this page."
else
message = exception.message
end

render json: { errors: [{ status: status.to_s, title: message }] }.to_json, status: status
message = case status
when 404
"The resource you are looking for doesn't exist."
when 401
"You are not authorized to access this page."
else
exception.message
end

render json: { errors: [{ status: status.to_s, title: message }] }.to_json,
status: status
end
end

protected

def is_admin_or_staff?
current_user && current_user.is_admin_or_staff? ? 1 : 0
current_user&.is_admin_or_staff? ? 1 : 0
end
end
14 changes: 7 additions & 7 deletions app/controllers/concerns/delegatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ module Delegatable

included do
def dois_count(uid, **options)
Rails.cache.fetch("dois_count/#{uid}", expires_in: 6.hours, force: options[:force]) do
if self.is_a?(ClientsController)
response = Maremma.get(ENV['API_URL'] + "/clients/" + uid)
elsif self.is_a?(ProvidersController)
response = Maremma.get(ENV['API_URL'] + "/providers/" + uid)
Rails.cache.fetch("dois_count/#{uid}", expires_in: 6.hours,
force: options[:force]) do
case self
when ClientsController
response = Maremma.get("#{ENV['API_URL']}/clients/#{uid}")
when ProvidersController
response = Maremma.get("#{ENV['API_URL']}/providers/#{uid}")
end
response.body.to_h.dig("meta", "dois")
end
Expand All @@ -23,7 +25,6 @@ def dois_count(uid, **options)
# response.body["meta"]["prefixes"]
# end


# def repository_count uid
# if self.is_a?(ClientsController)
# response = Maremma.get(ENV['API_URL']+"/clients/"+uid)
Expand All @@ -33,6 +34,5 @@ def dois_count(uid, **options)

# response.body["meta"]["repositories"]
# end

end
end
2 changes: 1 addition & 1 deletion app/controllers/concerns/facetable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def facet_by_year(arr)
def facet_by_provider(arr)
# generate hash with id and name for each provider in facet
ids = arr.map { |hsh| hsh["key"] }.join(",")
providers = Provider.find_by_ids(ids).to_a.reduce({}) do |sum, p|
providers = Provider.find_by(ids: ids).to_a.reduce({}) do |sum, p|
sum[p.id] = p.name
sum
end
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/heartbeat_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class HeartbeatController < ApplicationController
def index
heartbeat = Heartbeat.new
render plain: heartbeat.string, status: heartbeat.status, content_type: "text/plain"
render plain: heartbeat.string, status: heartbeat.status,
content_type: "text/plain"
end
end
2 changes: 1 addition & 1 deletion app/controllers/index_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class IndexController < ApplicationController
def index
render plain: ENV['SITE_TITLE']
render plain: ENV["SITE_TITLE"]
end
end
6 changes: 3 additions & 3 deletions app/jobs/affiliation_identifier_import_by_month_job.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class AffiliationIdentifierImportByMonthJob < ActiveJob::Base
class AffiliationIdentifierImportByMonthJob < ApplicationJob
queue_as :levriero

def perform(options={})
def perform(options = {})
AffiliationIdentifier.import(options)
end
end
end
4 changes: 2 additions & 2 deletions app/jobs/affiliation_identifier_import_job.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class AffiliationIdentifierImportJob < ActiveJob::Base
class AffiliationIdentifierImportJob < ApplicationJob
queue_as :levriero

def perform(item)
AffiliationIdentifier.push_item(item)
end
end
end
3 changes: 3 additions & 0 deletions app/jobs/application_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

class ApplicationJob < ActiveJob::Base; end
4 changes: 2 additions & 2 deletions app/jobs/crossref_funder_import_by_month_job.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class CrossrefFunderImportByMonthJob < ActiveJob::Base
class CrossrefFunderImportByMonthJob < ApplicationJob
queue_as :levriero

def perform(options={})
def perform(options = {})
CrossrefFunder.import(options)
end
end
2 changes: 1 addition & 1 deletion app/jobs/crossref_funder_import_job.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class CrossrefFunderImportJob < ActiveJob::Base
class CrossrefFunderImportJob < ApplicationJob
queue_as :levriero

def perform(item)
Expand Down
Loading

0 comments on commit 91f225d

Please sign in to comment.