Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed URL to use Ruby's standard URI regex #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/scrivener/validations.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Scrivener
require "uri"

# Provides a base implementation for extensible validation routines.
# {Scrivener::Validations} currently only provides the following assertions:
Expand Down Expand Up @@ -130,9 +131,7 @@ def assert_numeric(att, error = [att, :not_numeric])
end
end

URL = /\A(http|https):\/\/([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}|(2
5[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}
|localhost)(:[0-9]{1,5})?(\/.*)?\z/ix
URL = URI::regexp(%w(http https))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not know about this. Very cool!


def assert_url(att, error = [att, :not_url])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might make sense to pass the schemes here:

def assert_url(att, schemes = %w(http https), error = [att, :not_url])
  if assert_present(att, error)
    assert_format(att, URI.regexp(schemes), error)
  end
end

This would allow people to use this helper to check other kind of URLs without extra work. Thoughts?

if assert_present(att, error)
Expand Down
4 changes: 4 additions & 0 deletions test/scrivener_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ def validate

p = Post.new(url: "http://google.com", email: "[email protected]")
assert p.valid?

# server name without a / but a ? is a valid URL
p = Post.new(url: "http://google.com?blah=blah", email: "[email protected]")
assert p.valid?
end
end

Expand Down