Project

url_format

0.0
No commit activity in last 3 years
No release in over 3 years
Validate the url format of an attribute.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0
>= 0

Runtime

 Project Readme

UrlFormat

Build Status

This gem validates a URL, adds the http:// prefix if not included, and provides a get_domain method.

Installation

Add this line to your application's Gemfile:

gem 'url_format'

And then execute:

$ bundle

Or install it yourself as:

$ gem install url_format

Usage

Using it is as simple as using the validates keyword in your model:

class User < ActiveRecord::Base
  
  # ...

  validates :url, url_format: true

  # ...

end

Now the url attribute will be validated accordingly:

User.new(url: 'http://example.com').valid? # => true
User.new(url: 'invalid_url..com').valid?    # => false

Also, the model in question doesn't need to inherit from ActiveRecord::Base, you only need to include ActiveModel::Validations in your class:

require 'url_format'

class Awesome
  include ActiveModel::Validations

  attr_accessor :url

  validates :url, url_format: true

  def domain
    UrlFormat.get_domain(url)
  end
end

awesome = Awesome.new

awesome.url = "www.example.com"
awesome.valid? # => true
awesome.url    # => 'http://www.example.com'
awesome.domain # => 'example.com'

awesome.url = "invalid_url"
awesome.valid? # => false

The gem can ensure that a url is prefixed with http:// as well:

UrlFormat.ensure_http_prefix('google.com')         # => 'http://google.com'
UrlFormat.ensure_http_prefix('https://google.com') # => 'https://google.com'

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request