Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/johno/url_format
Validate the url format of an attribute.
https://github.com/johno/url_format
Last synced: 2 months ago
JSON representation
Validate the url format of an attribute.
- Host: GitHub
- URL: https://github.com/johno/url_format
- Owner: johno
- License: mit
- Created: 2013-12-13T17:58:02.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2015-01-27T07:35:55.000Z (almost 10 years ago)
- Last Synced: 2024-10-19T17:29:32.818Z (3 months ago)
- Language: Ruby
- Homepage: http://johnotander.com/gems/2013/12/24/url-format-validator-gem/
- Size: 258 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# UrlFormat
[![Build Status](https://travis-ci.org/johnotander/url_format.svg?branch=master)](https://travis-ci.org/johnotander/url_format)
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:
```ruby
class User < ActiveRecord::Base
# ...validates :url, url_format: true
# ...
end
```Now the url attribute will be validated accordingly:
```ruby
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:
```ruby
require 'url_format'class Awesome
include ActiveModel::Validationsattr_accessor :url
validates :url, url_format: true
def domain
UrlFormat.get_domain(url)
end
endawesome = 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:
```ruby
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