Skip to content

Commit 71cce76

Browse files
committed
Rubocop
1 parent 8638198 commit 71cce76

7 files changed

Lines changed: 71 additions & 27 deletions

File tree

.github/workflows/ruby.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,24 @@ jobs:
3333
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
3434
- name: Run unit tests
3535
run: bundle exec rake
36+
37+
linters:
38+
name: Linters
39+
runs-on: ubuntu-latest
40+
steps:
41+
- uses: actions/checkout@v2
42+
- name: Set up Ruby
43+
uses: ruby/setup-ruby@v1
44+
with:
45+
ruby-version: "3.1"
46+
bundler-cache: true
47+
- name: Run Rubocop
48+
run: |
49+
gem install rubocop-rspec rubocop-performance
50+
git clone https://gist.github.com/65e21b9e8b0d1db285dcb4fc627b98fa.git .rubocop
51+
cp .rubocop/.rubocop.yml .rubocop-ruby.yml
52+
git clone https://gist.github.com/14cfa24d53c12bf385871e9b93b95c37.git .rubocop-rspec
53+
cp .rubocop-rspec/.rubocop-rspec.yml .
54+
cat .rubocop.yml | sed -e 's/~\/.rubocop/.rubocop/' | sed -e 's/.rubocop.yml/.rubocop-ruby.yml/' > .rubocop2.yml
55+
mv .rubocop2.yml .rubocop.yml
56+
rubocop --parallel

.rubocop.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
inherit_from:
2+
- ~/.rubocop.yml
3+
- ~/.rubocop-rspec.yml
4+
5+
inherit_mode:
6+
merge:
7+
- Include
8+
- Exclude
9+
- Environments
10+
11+
AllCops:
12+
Exclude:
13+
- "*.gemspec" # temporarily until we move off of juweiler

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
source 'https://rubygems.org'
24

35
gem 'activerecord'

Rakefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'rubygems'
24
require 'bundler'
35
begin

lib/url_validation.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'addressable/uri'
24
require 'httpi'
35
require 'active_support/core_ext/hash/except'
@@ -171,7 +173,7 @@ def url_accessible?(uri, options)
171173

172174
check_host = options[:check_host]
173175
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)
176+
if (schemes = Array.wrap(check_host)) && schemes.all?(String) && !schemes.include?(uri.scheme)
175177
return true
176178
end
177179

@@ -194,7 +196,7 @@ def http_url_accessible?(uri, options)
194196
def url_response_valid?(response, options)
195197
return true unless response.kind_of?(HTTPI::Response) && options[:check_path]
196198

197-
response_codes = options[:check_path] == true ? [400..499, 500..599] : Array.wrap(options[:check_path]).flatten
199+
response_codes = (options[:check_path] == true) ? [400..499, 500..599] : Array.wrap(options[:check_path]).flatten
198200
return response_codes.none? do |code| # it's good if it's not a bad response
199201
case code # and it's a bad response if...
200202
when Range

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
# This file was generated by the `rspec --init` command. Conventionally, all
24
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
35
# The generated `.rspec` file contains `--require spec_helper` which will cause

spec/url_validator_spec.rb

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'spec_helper'
24
require 'active_model'
35

@@ -13,21 +15,21 @@ class Record
1315
end
1416

1517
context '[basic]' do
16-
it "should allow nil if :allow_nil is set" do
18+
it "allows nil if :allow_nil is set" do
1719
@validator = described_class.new(attributes: %i[field], allow_nil: true)
1820
@validator.validate_each(@record, :field, nil)
1921
expect(@record.errors).to be_empty
2022
end
2123

22-
it "should allow \"\" if :allow_blank is set" do
24+
it "allows '' if :allow_blank is set" do
2325
@validator = described_class.new(attributes: %i[field], allow_blank: true)
2426
@validator.validate_each(@record, :field, "")
2527
expect(@record.errors).to be_empty
2628
end
2729
end
2830

2931
context '[format]' do
30-
it "should only allow HTTP URLs if :scheme is set to 'http'" do
32+
it "only allows HTTP URLs if :scheme is set to 'http'" do
3133
@validator = described_class.new(attributes: %i[field], scheme: 'http')
3234
@validator.validate_each(@record, :field, 'http://www.apple.com')
3335
expect(@record.errors).to be_empty
@@ -36,7 +38,7 @@ class Record
3638
expect(@record.errors[:field].first).to include('invalid_url')
3739
end
3840

39-
it "should only allow HTTP and HTTPS URLs if :scheme is set to %w(http https)" do
41+
it "onlies allow HTTP and HTTPS URLs if :scheme is set to %w(http https)" do
4042
@validator = described_class.new(attributes: %i[field], scheme: %w[http https])
4143
@validator.validate_each(@record, :field, 'http://www.apple.com')
4244
expect(@record.errors).to be_empty
@@ -47,14 +49,14 @@ class Record
4749
expect(@record.errors[:field].first).to include('invalid_url')
4850
end
4951

50-
it "should try a default scheme if :default_scheme is set" do
52+
it "tries a default scheme if :default_scheme is set" do
5153
@validator = described_class.new(attributes: %i[field], scheme: 'http', default_scheme: 'http')
5254
@validator.validate_each(@record, :field, 'www.apple.com')
5355
expect(@record.errors).to be_empty
5456
end
5557

5658
context '[HTTP(S)]' do
57-
it "should not allow garbage URLs that still somehow pass the ridiculously open-ended RFC" do
59+
it "does not allow garbage URLs that still somehow pass the ridiculously open-ended RFC" do
5860
@validator = described_class.new(attributes: %i[field])
5961

6062
%w[
@@ -70,7 +72,7 @@ class Record
7072
end
7173
end
7274

73-
it "should not allow invalid scheme formats" do
75+
it "does not allow invalid scheme formats" do
7476
@validator = described_class.new(attributes: %i[field])
7577
@validator.validate_each(@record, :field, ' https://www.apple.com')
7678
expect(@record.errors[:field].first).to include('invalid_url')
@@ -80,7 +82,7 @@ class Record
8082

8183
context '[accessibility]' do
8284
context '[:check_host]' do
83-
it "should only validate if the host is accessible when :check_host is set" do
85+
it "only validates if the host is accessible when :check_host is set" do
8486
@validator = described_class.new(attributes: %i[field])
8587
@validator.validate_each(@record, :field, 'http://www.invalid.tld')
8688
expect(@record.errors).to be_empty
@@ -90,25 +92,25 @@ class Record
9092
expect(@record.errors[:field].first).to include('url_not_accessible')
9193
end
9294

93-
it "should not perform the accessibility check if :check_host is set to 'http' and the URL scheme is not HTTP" do
95+
it "does not perform the accessibility check if :check_host is set to 'http' and the URL scheme is not HTTP" do
9496
@validator = described_class.new(attributes: %i[field], check_host: 'http')
9597
@validator.validate_each(@record, :field, 'https://www.invalid.tld')
9698
expect(@record.errors).to be_empty
9799
end
98100

99-
it "should only validate if the host is accessible when :check_host is set to 'http' and the URL scheme is HTTP" do
101+
it "only validates if the host is accessible when :check_host is set to 'http' and the URL scheme is HTTP" do
100102
@validator = described_class.new(attributes: %i[field], check_host: 'http')
101103
@validator.validate_each(@record, :field, 'http://www.invalid.tld')
102104
expect(@record.errors[:field].first).to include('url_not_accessible')
103105
end
104106

105-
it "should not perform the accessibility check if :check_host is set to %w(http https) and the URL scheme is not HTTP(S)" do
107+
it "does not perform the accessibility check if :check_host is set to %w(http https) and the URL scheme is not HTTP(S)" do
106108
@validator = described_class.new(attributes: %i[field], check_host: %w[http https], scheme: %w[ftp http https])
107109
@validator.validate_each(@record, :field, 'ftp://www.invalid.tld')
108110
expect(@record.errors).to be_empty
109111
end
110112

111-
it "should only validate if the host is accessible when :check_host is set to %w(http https) and the URL scheme is HTTP(S)" do
113+
it "only validates if the host is accessible when :check_host is set to %w(http https) and the URL scheme is HTTP(S)" do
112114
@validator = described_class.new(attributes: %i[field], check_host: %w[http https])
113115
@validator.validate_each(@record, :field, 'http://www.invalid.tld')
114116
expect(@record.errors[:field].first).to include('url_not_accessible')
@@ -118,15 +120,15 @@ class Record
118120
expect(@record.errors[:field].first).to include('url_not_accessible')
119121
end
120122

121-
it "should only validate the host" do
123+
it "only validates the host" do
122124
@validator = described_class.new(attributes: %i[field], check_host: true)
123125
@validator.validate_each(@record, :field, 'http://www.google.com/sdgsdgf')
124126
expect(@record.errors).to be_empty
125127
end
126128
end
127129

128130
context '[:check_path]' do
129-
it "should not validate if the response code is equal to the Integer value of this option" do
131+
it "does not validate if the response code is equal to the Integer value of this option" do
130132
@validator = described_class.new(attributes: %i[field], check_path: 404)
131133
@validator.validate_each(@record, :field, 'http://www.google.com/sdgsdgf')
132134
expect(@record.errors[:field].first).to include('url_invalid_response')
@@ -138,7 +140,7 @@ class Record
138140
expect(@record.errors[:field]).to be_empty
139141
end
140142

141-
it "should not validate if the response code is equal to the Symbol value of this option" do
143+
it "does not validate if the response code is equal to the Symbol value of this option" do
142144
@validator = described_class.new(attributes: %i[field], check_path: :not_found)
143145
@validator.validate_each(@record, :field, 'http://www.google.com/sdgsdgf')
144146
expect(@record.errors[:field].first).to include('url_invalid_response')
@@ -150,7 +152,7 @@ class Record
150152
expect(@record.errors[:field]).to be_empty
151153
end
152154

153-
it "should not validate if the response code is within the Range value of this option" do
155+
it "does not validate if the response code is within the Range value of this option" do
154156
@validator = described_class.new(attributes: %i[field], check_path: 400..499)
155157
@validator.validate_each(@record, :field, 'http://www.google.com/sdgsdgf')
156158
expect(@record.errors[:field].first).to include('url_invalid_response')
@@ -162,7 +164,7 @@ class Record
162164
expect(@record.errors[:field]).to be_empty
163165
end
164166

165-
it "should not validate if the response code is equal to the Integer value contained in the Array value of this option" do
167+
it "does not validate if the response code is equal to the Integer value contained in the Array value of this option" do
166168
@validator = described_class.new(attributes: %i[field], check_path: [404, 405])
167169
@validator.validate_each(@record, :field, 'http://www.google.com/sdgsdgf')
168170
expect(@record.errors[:field].first).to include('url_invalid_response')
@@ -174,7 +176,7 @@ class Record
174176
expect(@record.errors[:field]).to be_empty
175177
end
176178

177-
it "should not validate if the response code is equal to the Symbol value contained in the Array value of this option" do
179+
it "does not validate if the response code is equal to the Symbol value contained in the Array value of this option" do
178180
@validator = described_class.new(attributes: %i[field], check_path: %i[not_found unauthorized])
179181
@validator.validate_each(@record, :field, 'http://www.google.com/sdgsdgf')
180182
expect(@record.errors[:field].first).to include('url_invalid_response')
@@ -186,7 +188,7 @@ class Record
186188
expect(@record.errors[:field]).to be_empty
187189
end
188190

189-
it "should not validate if the response code is equal to the Range value contained in the Array value of this option" do
191+
it "does not validate if the response code is equal to the Range value contained in the Array value of this option" do
190192
@validator = described_class.new(attributes: %i[field], check_path: [400..499, 500..599])
191193
@validator.validate_each(@record, :field, 'http://www.google.com/sdgsdgf')
192194
expect(@record.errors[:field].first).to include('url_invalid_response')
@@ -198,37 +200,37 @@ class Record
198200
expect(@record.errors[:field]).to be_empty
199201
end
200202

201-
it "should skip validation by default" do
203+
it "skips validation by default" do
202204
@validator = described_class.new(attributes: %i[field], check_path: nil)
203205
@validator.validate_each(@record, :field, 'http://www.google.com/sdgsdgf')
204206
expect(@record.errors[:field]).to be_empty
205207
end
206208

207-
it "should not validate 4xx and 5xx response codes if the value is true" do
209+
it "does not validate 4xx and 5xx response codes if the value is true" do
208210
@validator = described_class.new(attributes: %i[field], check_path: true)
209211
@validator.validate_each(@record, :field, 'http://www.google.com/sdgsdgf')
210212
expect(@record.errors[:field].first).to include('url_invalid_response')
211213
end
212214

213-
it "should skip validation for non-HTTP URLs" do
215+
it "skips validation for non-HTTP URLs" do
214216
@validator = described_class.new(attributes: %i[field], check_path: true, scheme: %w[ftp http https])
215217
@validator.validate_each(@record, :field, 'ftp://ftp.sdgasdgohaodgh.com/sdgjsdg')
216218
expect(@record.errors[:field]).to be_empty
217219
end
218220
end
219221

220222
context '[:httpi_adapter]' do
221-
it "should use the specified HTTPI adapter" do
223+
it "uses the specified HTTPI adapter" do
222224
@validator = described_class.new(attributes: %i[field], httpi_adapter: :curl, check_host: true)
223225
expect(HTTPI).to receive(:get).once.with(an_instance_of(HTTPI::Request), :curl).and_return(false)
224226
@validator.validate_each(@record, :field, 'http://www.google.com/sdgsdgf')
225227
end
226228
end
227229

228230
context '[:request_callback]' do
229-
it "should be yielded the HTTPI request" do
231+
it "is yielded the HTTPI request" do
230232
called = false
231-
@validator = described_class.new(attributes: %i[field], check_host: true, request_callback: ->(request) { called = true; expect(request).to be_kind_of(HTTPI::Request) })
233+
@validator = described_class.new(attributes: %i[field], check_host: true, request_callback: ->(request) { called = true; expect(request).to be_a(HTTPI::Request) })
232234
@validator.validate_each(@record, :field, 'http://www.google.com/sdgsdgf')
233235
expect(called).to be(true)
234236
end

0 commit comments

Comments
 (0)