Skip to content

Commit

Permalink
Update rubocop and fix errors (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
bstewart00 authored Jul 7, 2021
1 parent 49c4157 commit 41b0aa3
Show file tree
Hide file tree
Showing 20 changed files with 100 additions and 512 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: 2
jobs:
build:
docker:
- image: circleci/ruby:2.3-jessie
- image: circleci/ruby:2.5.4
steps:
- checkout
- restore_cache:
Expand Down
53 changes: 9 additions & 44 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,8 @@
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# Cop supports --auto-correct.
Layout/EmptyLinesAroundAccessModifier:
Exclude:
- 'lib/wovnrb/text_caches/memory_cache.rb'

# Offense count: 1
# Cop supports --auto-correct.
Layout/EmptyLinesAroundMethodBody:
Exclude:
- 'test/lib/text_caches/memory_cache_test.rb'
Gemspec/RequiredRubyVersion:
Enabled: false

# Offense count: 11
# Cop supports --auto-correct.
Expand All @@ -25,13 +16,9 @@ Layout/ExtraSpacing:
Exclude:
- 'lib/wovnrb/lang.rb'

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, IndentationWidth.
# SupportedStyles: special_inside_parentheses, consistent, align_braces
Layout/FirstHashElementIndentation:
Style/HashEachMethods:
Exclude:
- 'lib/wovnrb/text_caches/cache_base.rb'
- 'lib/wovnrb/store.rb'

# Offense count: 4
# Cop supports --auto-correct.
Expand All @@ -42,13 +29,6 @@ Layout/HeredocIndentation:
- 'test/lib/lang_test.rb'
- 'test/lib/wovnrb_test.rb'

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: AllowForAlignment.
Layout/SpaceAroundOperators:
Exclude:
- 'lib/wovnrb/text_caches/memory_cache.rb'

# Offense count: 3
# Configuration parameters: AllowSafeAssignment.
Lint/AssignmentInCondition:
Expand All @@ -70,13 +50,6 @@ Lint/ImplicitStringConcatenation:
Exclude:
- 'test/lib/lang_test.rb'

# Offense count: 3
# Cop supports --auto-correct.
# Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods.
Lint/UnusedMethodArgument:
Exclude:
- 'lib/wovnrb/text_caches/cache_base.rb'

# Offense count: 5
Lint/UselessAssignment:
Exclude:
Expand Down Expand Up @@ -116,12 +89,13 @@ Metrics/MethodLength:
# Offense count: 12
Metrics/PerceivedComplexity:
Max: 22
Exclude:
- 'lib/wovnrb/headers.rb'

# Offense count: 3
Naming/AccessorMethodName:
Exclude:
- 'lib/wovnrb/services/wovn_logger.rb'
- 'lib/wovnrb/text_caches/cache_base.rb'

# Offense count: 120
# Configuration parameters: .
Expand Down Expand Up @@ -153,12 +127,6 @@ Style/ClassCheck:
Exclude:
- 'lib/wovnrb/store.rb'

# Offense count: 6
Style/ClassVars:
Exclude:
- 'lib/wovnrb/text_caches/cache_base.rb'
- 'lib/wovnrb/text_caches/memory_cache.rb'

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: Keywords.
Expand All @@ -183,8 +151,6 @@ Style/Documentation:
- 'lib/wovnrb/services/wovn_logger.rb'
- 'lib/wovnrb/settings.rb'
- 'lib/wovnrb/store.rb'
- 'lib/wovnrb/text_caches/cache_base.rb'
- 'lib/wovnrb/text_caches/memory_cache.rb'

# Offense count: 2
Style/DoubleNegation:
Expand Down Expand Up @@ -224,7 +190,6 @@ Style/IfUnlessModifier:
- 'Rakefile'
- 'lib/wovnrb/helpers/nokogumbo_helper.rb'
- 'lib/wovnrb/store.rb'
- 'lib/wovnrb/text_caches/memory_cache.rb'

# Offense count: 1
# Cop supports --auto-correct.
Expand All @@ -241,7 +206,6 @@ Style/NegatedIf:
Style/PreferredHashMethods:
Exclude:
- 'lib/wovnrb/store.rb'
- 'lib/wovnrb/text_caches/memory_cache.rb'

# Offense count: 2
# Cop supports --auto-correct.
Expand Down Expand Up @@ -297,7 +261,7 @@ Style/ZeroLengthPredicate:
# Offense count: 1436
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Layout/LineLength:
Max: 1424

Style/FrozenStringLiteralComment:
Expand All @@ -310,6 +274,7 @@ Style/SafeNavigation:
Enabled: false

AllCops:
NewCops: enable
Exclude:
- 'docker/**/*'
- 'vendor/**/*'
- 'vendor/**/*'
5 changes: 0 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in wovn.gemspec
if RUBY_VERSION < '2.2.2'
gem 'activesupport', '< 5.0.0'
end
gemspec
4 changes: 1 addition & 3 deletions lib/wovnrb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
require 'active_support'
require 'json'
require 'wovnrb/helpers/nokogumbo_helper'
require 'wovnrb/text_caches/cache_base'
require 'wovnrb/railtie' if defined?(Rails)
require 'wovnrb/version'

Expand All @@ -18,9 +17,8 @@ class Interceptor
def initialize(app, opts = {})
@app = app
@store = Store.instance
opts = opts.each_with_object({}) { |(k, v), memo| memo[k.to_s] = v }
opts = opts.transform_keys(&:to_s)
@store.update_settings(opts)
CacheBase.set_single(@store.settings)
end

def call(env)
Expand Down
2 changes: 1 addition & 1 deletion lib/wovnrb/api_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def compress_request_data(data_hash)
end

def api_uri
Addressable::URI.parse(@store.settings['api_url'] + '/v0/')
Addressable::URI.parse("#{@store.settings['api_url']}/v0/")
end

def api_timeout
Expand Down
69 changes: 30 additions & 39 deletions lib/wovnrb/headers.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
module Wovnrb
class Headers
attr_reader :unmasked_url
attr_reader :url
attr_reader :protocol
attr_reader :unmasked_host
attr_reader :host
attr_reader :unmasked_pathname
attr_reader :pathname
attr_reader :pathname_with_trailing_slash_if_present
attr_reader :unmasked_url, :url, :protocol, :unmasked_host, :host, :unmasked_pathname, :pathname, :pathname_with_trailing_slash_if_present

# Generates new instance of Wovnrb::Headers.
# Its parameters are set by parsing env variable.
#

def initialize(env, settings)
request = Rack::Request.new(env)

Expand Down Expand Up @@ -42,21 +35,21 @@ def initialize(env, settings)
@pathname, @query = @env['REQUEST_URI'].split('?')
@pathname = settings['url_pattern'] == 'path' ? remove_lang(@pathname, lang_code) : @pathname
@query ||= ''
@url = "#{@host}#{@pathname}#{(!@query.empty? ? '?' : '') + remove_lang(@query, lang_code)}"
if !settings['query'].empty?
@url = "#{@host}#{@pathname}#{(@query.empty? ? '' : '?') + remove_lang(@query, lang_code)}"
if settings['query'].empty?
@query = ''
else
query_vals = []
settings['query'].each do |qv|
rx = Regexp.new("(^|&)(?<query_val>#{qv}[^&]+)(&|$)")
m = @query.match(rx)
query_vals.push(m[:query_val]) if m && m[:query_val]
end
@query = if !query_vals.empty?
"?#{query_vals.sort.join('&')}"
else
@query = if query_vals.empty?
''
else
"?#{query_vals.sort.join('&')}"
end
else
@query = ''
end
@query = remove_lang(@query, lang_code)
@pathname_with_trailing_slash_if_present = @pathname
Expand Down Expand Up @@ -115,11 +108,11 @@ def redirect_location(lang)
case @settings['url_pattern']
when 'query'
lang_param_name = @settings['lang_param_name']
if location !~ /\?/
location = "#{location}?#{lang_param_name}=#{lang_code}"
else @env['REQUEST_URI'] !~ /(\?|&)#{lang_param_name}=/
location = "#{location}&#{lang_param_name}=#{lang_code}"
end
location = if location =~ /\?/
"#{location}&#{lang_param_name}=#{lang_code}"
else
"#{location}?#{lang_param_name}=#{lang_code}"
end
when 'subdomain'
location = "#{lang_code.downcase}.#{location}"
# when 'path'
Expand Down Expand Up @@ -173,7 +166,7 @@ def remove_lang(uri, lang = path_lang)
lang_param_name = @settings['lang_param_name']
uri.sub(/(^|\?|&)#{lang_param_name}=#{lang_code}(&|$)/, '\1').gsub(/(\?|&)$/, '')
when 'subdomain'
rp = Regexp.new('(^|(//))' + lang_code + '\.', 'i')
rp = Regexp.new("(^|(//))#{lang_code}\\.", 'i')
uri.sub(rp, '\1')
# when 'path'
else
Expand All @@ -182,24 +175,22 @@ def remove_lang(uri, lang = path_lang)
end

def out(headers)
r = Regexp.new('//' + @host)
r = Regexp.new("//#{@host}")
lang_code = Store.instance.settings['custom_lang_aliases'][self.lang_code] || self.lang_code
if lang_code != @settings['default_lang'] && headers.key?('Location') && headers['Location'] =~ r
unless @settings['ignore_globs'].ignore?(headers['Location'])
case @settings['url_pattern']
when 'query'
headers['Location'] += if headers['Location'] =~ /\?/
'&'
else
'?'
end
headers['Location'] += "#{@settings['lang_param_name']}=#{lang_code}"
when 'subdomain'
headers['Location'] = headers['Location'].sub(/\/\/([^.]+)/, '//' + lang_code + '.\1')
# when 'path'
else
headers['Location'] = headers['Location'].sub(/(\/\/[^\/]+)/, '\1/' + lang_code)
end
if lang_code != @settings['default_lang'] && headers.key?('Location') && headers['Location'] =~ r && !@settings['ignore_globs'].ignore?(headers['Location'])
case @settings['url_pattern']
when 'query'
headers['Location'] += if headers['Location'] =~ /\?/
'&'
else
'?'
end
headers['Location'] += "#{@settings['lang_param_name']}=#{lang_code}"
when 'subdomain'
headers['Location'] = headers['Location'].sub(/\/\/([^.]+)/, "//#{lang_code}.\\1")
# when 'path'
else
headers['Location'] = headers['Location'].sub(/(\/\/[^\/]+)/, "\\1/#{lang_code}")
end
end
headers
Expand Down
16 changes: 7 additions & 9 deletions lib/wovnrb/helpers/nokogumbo_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ module Wovnrb
module Helpers
module NokogumboHelper
def parse_html(html_string, encoding = 'UTF-8')
dom = if html_string.strip[0..999] =~ /<html/i
d = Nokogiri::HTML5(html_string)
d.encoding = encoding
d
else
parse_fragment(html_string, encoding)
end

dom
if html_string.strip[0..999] =~ /<html/i
d = Nokogiri::HTML5(html_string)
d.encoding = encoding
d
else
parse_fragment(html_string, encoding)
end
end

# https://www.rubydoc.info/gems/nokogumbo/Nokogiri/HTML5#fragment-class_method
Expand Down
29 changes: 15 additions & 14 deletions lib/wovnrb/lang.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,46 +146,47 @@ def add_lang_code(href, pattern, headers)
if uri.host.downcase === headers.host.downcase
case pattern
when 'subdomain'
sub_d = href.match(/\/\/([^\.]*)\./)[1]
sub_d = href.match(/\/\/([^.]*)\./)[1]
sub_code = Lang.get_code(sub_d)
new_href = if sub_code && sub_code.casecmp(code_to_add).zero?
href.sub(Regexp.new(code_to_add, 'i'), code_to_add.downcase)
else
href.sub(/(\/\/)([^\.]*)/, '\1' + code_to_add.downcase + '.' + '\2')
href.sub(/(\/\/)([^.]*)/, "\\1#{code_to_add.downcase}.\\2")
end
when 'query'
new_href = add_query_lang_code(href, code_to_add, lang_param_name)
else # path
new_href = href.sub(/([^\.]*\.[^\/]*)(\/|$)/, '\1/' + code_to_add + '/')
new_href = href.sub(/([^.]*\.[^\/]*)(\/|$)/, "\\1/#{code_to_add}/")
end
end
elsif href
case pattern
when 'subdomain'
lang_url = headers.protocol + '://' + code_to_add.downcase + '.' + headers.host
current_dir = headers.pathname.sub(/[^\/]*\.[^\.]{2,6}$/, '')
new_href = if href =~ /^\.\..*$/
lang_url = "#{headers.protocol}://#{code_to_add.downcase}.#{headers.host}"
current_dir = headers.pathname.sub(/[^\/]*\.[^.]{2,6}$/, '')
new_href = case href
when /^\.\..*$/
# ../path
lang_url + '/' + href.gsub(/^\.\.\//, '')
elsif href =~ /^\..*$/
"#{lang_url}/#{href.gsub(/^\.\.\//, '')}"
when /^\..*$/
# ./path
lang_url + current_dir + '/' + href.gsub(/^\.\//, '')
elsif href =~ /^\/.*$/
"#{lang_url}#{current_dir}/#{href.gsub(/^\.\//, '')}"
when /^\/.*$/
# /path
lang_url + href
else
# path
lang_url + current_dir + '/' + href
"#{lang_url}#{current_dir}/#{href}"
end
when 'query'
new_href = add_query_lang_code(href, code_to_add, lang_param_name)
else # path
if href =~ /^\//
new_href = '/' + code_to_add + href
new_href = "/#{code_to_add}#{href}"
else
current_dir = headers.pathname.sub(/[^\/]*\.[^\.]{2,6}$/, '')
current_dir = headers.pathname.sub(/[^\/]*\.[^.]{2,6}$/, '')
current_dir = '/' if current_dir == ''
new_href = '/' + code_to_add + current_dir + href
new_href = "/#{code_to_add}#{current_dir}#{href}"
end
end
end
Expand Down
Loading

0 comments on commit 41b0aa3

Please sign in to comment.