Class: UrlShortly::Generator
- Inherits:
-
Object
- Object
- UrlShortly::Generator
- Defined in:
- lib/url_shortly/generator.rb
Constant Summary collapse
- BASE_URL =
"http://short.ly/"
Class Method Summary collapse
Class Method Details
.shorten(url) ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/url_shortly/generator.rb', line 7 def self.shorten(url) raise Error, "URL cannot be empty" if url.to_s.empty? raise Error, "Invalid URL format" if !valid_url?(url) id = Base62.encode(url.hash.abs) "#{BASE_URL}#{id}" end |
.valid_url?(url) ⇒ Boolean
15 16 17 18 19 20 |
# File 'lib/url_shortly/generator.rb', line 15 def self.valid_url?(url) uri = URI.parse(url) uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS) rescue URI::InvalidURIError false end |