Skip to content

Commit 963a4bc

Browse files
committed
RuboCop style fixes
1 parent 1d71928 commit 963a4bc

6 files changed

Lines changed: 184 additions & 113 deletions

File tree

Gemfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
source 'https://rubygems.org'
22

3-
gem 'addressable', require: 'addressable/uri' # for unicode URIs
4-
gem 'activesupport'
53
gem 'activerecord'
4+
gem 'activesupport'
5+
gem 'addressable', require: 'addressable/uri' # for unicode URIs
66
gem 'httpi'
77

88
group :development do
99
# PUBLISHING
10-
gem 'psych', '< 4.0' # jeweler incompatibility
1110
gem 'juwelier'
11+
gem 'psych', '< 4.0' # jeweler incompatibility
1212

1313
# DOCS
14-
gem 'yard', require: nil
1514
gem 'redcarpet', require: nil
15+
gem 'yard', require: nil
1616

1717
# SPECS
1818
gem 'rspec'

Rakefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ require 'bundler'
33
begin
44
Bundler.setup(:default, :development)
55
rescue Bundler::BundlerError => e
6-
$stderr.puts e.message
7-
$stderr.puts "Run `bundle install` to install missing gems"
6+
warn e.message
7+
warn "Run `bundle install` to install missing gems"
88
exit e.status_code
99
end
1010
require 'rake'
1111

1212
require 'juwelier'
1313
Juwelier::Tasks.new do |gem|
1414
gem.name = 'url_validation'
15-
gem.summary = %Q{Simple URL validation in Rails 3+}
16-
gem.description = %Q{A simple, localizable EachValidator for URL fields in ActiveRecord 3.0.}
15+
gem.summary = %(Simple URL validation in Rails 3+)
16+
gem.description = %(A simple, localizable EachValidator for URL fields in ActiveRecord 3.0.)
1717
gem.email = 'git@timothymorgan.info'
1818
gem.homepage = 'http://github.com/riscfuture/url_validation'
1919
gem.authors = ['Tim Morgan']
@@ -29,7 +29,7 @@ YARD::Rake::YardocTask.new('doc') do |doc|
2929
doc.options << '-o' << 'doc'
3030
doc.options << '--title' << 'url_validation Documentation'.inspect
3131

32-
doc.files = %w(lib/**/*.rb README.md)
32+
doc.files = %w[lib/**/*.rb README.md]
3333
end
3434

3535
require 'rspec/core/rake_task'

lib/url_validation.rb

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class UrlValidator < ActiveModel::EachValidator
127127
variant_also_negotiates: 506,
128128
insufficient_storage: 507,
129129
not_extended: 510
130-
}
130+
}.freeze
131131

132132
# @private
133133
def validate_each(record, attribute, value)
@@ -136,7 +136,7 @@ def validate_each(record, attribute, value)
136136
begin
137137
uri = Addressable::URI.parse(value)
138138

139-
if uri.scheme.nil? and options[:default_scheme]
139+
if uri.scheme.nil? && options[:default_scheme]
140140
uri = Addressable::URI.parse("#{options[:default_scheme]}://#{value}")
141141
end
142142
rescue Addressable::URI::InvalidURIError
@@ -145,14 +145,14 @@ def validate_each(record, attribute, value)
145145
end
146146

147147
record.errors.add(attribute, options[:invalid_url_message] || :invalid_url) unless url_format_valid?(uri, options)
148-
record.errors.add(attribute, options[:url_not_accessible_message] || :url_not_accessible) unless response = url_accessible?(uri, options)
148+
record.errors.add(attribute, options[:url_not_accessible_message] || :url_not_accessible) unless (response = url_accessible?(uri, options))
149149
record.errors.add(attribute, options[:url_invalid_response_message] || :url_invalid_response) unless url_response_valid?(response, options)
150150
end
151151

152152
private
153153

154154
def url_format_valid?(uri, options)
155-
return false unless Array.wrap(options[:scheme] || %w(http https)).include?(uri.scheme)
155+
return false unless Array.wrap(options[:scheme] || %w[http https]).include?(uri.scheme)
156156

157157
case uri.scheme
158158
when 'http', 'https'
@@ -163,16 +163,16 @@ def url_format_valid?(uri, options)
163163
end
164164

165165
def http_url_format_valid?(uri)
166-
uri.host.present? and not uri.path.nil?
166+
uri.host.present? && !uri.path.nil?
167167
end
168168

169169
def url_accessible?(uri, options)
170-
return true unless options[:check_host] or options[:check_path]
170+
return true unless options[:check_host] || options[:check_path]
171171

172172
check_host = options[:check_host]
173-
check_host ||= %w(http https) if options[:check_path]
174-
if (schemes = Array.wrap(check_host)) and schemes.all? { |scheme| scheme.kind_of?(String) }
175-
return true unless schemes.include?(uri.scheme)
173+
check_host ||= %w[http https] if options[:check_path]
174+
if (schemes = Array.wrap(check_host)) && schemes.all? { |scheme| scheme.kind_of?(String) } && !schemes.include?(uri.scheme)
175+
return true
176176
end
177177

178178
case uri.scheme
@@ -187,12 +187,13 @@ def http_url_accessible?(uri, options)
187187
request = HTTPI::Request.new(uri.to_s)
188188
options[:request_callback].call(request) if options[:request_callback].respond_to?(:call)
189189
return HTTPI.get(request, options[:httpi_adapter])
190-
rescue
190+
rescue StandardError
191191
return false
192192
end
193193

194194
def url_response_valid?(response, options)
195-
return true unless response.kind_of?(HTTPI::Response) and options[:check_path]
195+
return true unless response.kind_of?(HTTPI::Response) && options[:check_path]
196+
196197
response_codes = options[:check_path] == true ? [400..499, 500..599] : Array.wrap(options[:check_path]).flatten
197198
return response_codes.none? do |code| # it's good if it's not a bad response
198199
case code # and it's a bad response if...

spec/spec_helper.rb

Lines changed: 94 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,98 @@
1-
require 'bundler'
2-
Bundler.require :default, :development
1+
# This file was generated by the `rspec --init` command. Conventionally, all
2+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3+
# The generated `.rspec` file contains `--require spec_helper` which will cause
4+
# this file to always be loaded, without a need to explicitly require it in any
5+
# files.
6+
#
7+
# Given that it is always loaded, you are encouraged to keep this file as
8+
# light-weight as possible. Requiring heavyweight dependencies from this file
9+
# will add to the boot time of your test suite on EVERY test run, even for an
10+
# individual file that may not need all of that loaded. Instead, consider making
11+
# a separate helper file that requires the additional dependencies and performs
12+
# the additional setup, and require it from the spec files that actually need
13+
# it.
14+
#
15+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
16+
RSpec.configure do |config|
17+
# rspec-expectations config goes here. You can use an alternate
18+
# assertion/expectation library such as wrong or the stdlib/minitest
19+
# assertions if you prefer.
20+
config.expect_with :rspec do |expectations|
21+
# This option will default to `true` in RSpec 4. It makes the `description`
22+
# and `failure_message` of custom matchers include text for helper methods
23+
# defined using `chain`, e.g.:
24+
# be_bigger_than(2).and_smaller_than(4).description
25+
# # => "be bigger than 2 and smaller than 4"
26+
# ...rather than:
27+
# # => "be bigger than 2"
28+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
29+
end
330

4-
$LOAD_PATH.unshift(File.dirname(__FILE__))
5-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
31+
# rspec-mocks config goes here. You can use an alternate test double
32+
# library (such as bogus or mocha) by changing the `mock_with` option here.
33+
config.mock_with :rspec do |mocks|
34+
# Prevents you from mocking or stubbing a method that does not exist on
35+
# a real object. This is generally recommended, and will default to
36+
# `true` in RSpec 4.
37+
mocks.verify_partial_doubles = true
38+
end
639

7-
require 'url_validation'
8-
require 'active_model'
9-
require 'active_support/core_ext/kernel/singleton_class'
40+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
41+
# have no way to turn it off -- the option exists only for backwards
42+
# compatibility in RSpec 3). It causes shared context metadata to be
43+
# inherited by the metadata hash of host groups and examples, rather than
44+
# triggering implicit auto-inclusion in groups with matching metadata.
45+
config.shared_context_metadata_behavior = :apply_to_host_groups
46+
47+
# This allows you to limit a spec run to individual examples or groups
48+
# you care about by tagging them with `:focus` metadata. When nothing
49+
# is tagged with `:focus`, all examples get run. RSpec also provides
50+
# aliases for `it`, `describe`, and `context` that include `:focus`
51+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
52+
config.filter_run_when_matching :focus
53+
54+
# Allows RSpec to persist some state between runs in order to support
55+
# the `--only-failures` and `--next-failure` CLI options. We recommend
56+
# you configure your source control system to ignore this file.
57+
config.example_status_persistence_file_path = "spec/examples.txt"
58+
59+
# Limits the available syntax to the non-monkey patched syntax that is
60+
# recommended. For more details, see:
61+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
62+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
63+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
64+
config.disable_monkey_patching!
65+
66+
# This setting enables warnings. It's recommended, but in some cases may
67+
# be too noisy due to issues in dependencies.
68+
config.warnings = true
1069

11-
RSpec.configure do |c|
12-
70+
# Many RSpec users commonly either run the entire suite or an individual
71+
# file, and it's useful to allow more verbose output when running an
72+
# individual spec file.
73+
if config.files_to_run.one?
74+
# Use the documentation formatter for detailed output,
75+
# unless a formatter has already been configured
76+
# (e.g. via a command-line flag).
77+
config.default_formatter = "doc"
78+
end
79+
80+
# Print the 10 slowest examples and example groups at the
81+
# end of the spec run, to help surface which specs are running
82+
# particularly slow.
83+
config.profile_examples = 10
84+
85+
# Run specs in random order to surface order dependencies. If you find an
86+
# order dependency and want to debug it, you can fix the order by providing
87+
# the seed, which is printed after each run.
88+
# --seed 1234
89+
config.order = :random
90+
91+
# Seed global randomization in this process using the `--seed` CLI option.
92+
# Setting this allows you to use `--seed` to deterministically reproduce
93+
# test failures related to randomization by passing the same `--seed` value
94+
# as the one that triggered the failure.
95+
Kernel.srand config.seed
1396
end
97+
98+
require 'url_validation'

0 commit comments

Comments
 (0)