Class: Dogo::Url
- Inherits:
-
Object
- Object
- Dogo::Url
- Defined in:
- lib/dogo/url.rb
Instance Attribute Summary collapse
-
#full ⇒ Object
readonly
Returns the value of attribute full.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
-
.find(id) ⇒ Object
Find a URL by its shortened id.
-
.shortened?(url) ⇒ Boolean
Check if the given URL is already shortened.
-
.valid?(url) ⇒ Boolean
Check if the given URL is valid, according the the
URI
regular expression.
Instance Method Summary collapse
-
#click! ⇒ Object
Increment the click counter for this URL.
-
#clicks ⇒ Object
Return the clicks for the current URL.
-
#initialize(full) ⇒ Url
constructor
A new instance of Url.
- #url ⇒ Object
Constructor Details
#initialize(full) ⇒ Url
Returns a new instance of Url.
26 27 28 29 |
# File 'lib/dogo/url.rb', line 26 def initialize(full) @full = full load_or_create end |
Instance Attribute Details
#full ⇒ Object (readonly)
Returns the value of attribute full.
3 4 5 |
# File 'lib/dogo/url.rb', line 3 def full @full end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
3 4 5 |
# File 'lib/dogo/url.rb', line 3 def id @id end |
Class Method Details
.find(id) ⇒ Object
Find a URL by its shortened id.
19 20 21 22 23 24 |
# File 'lib/dogo/url.rb', line 19 def self.find(id) key = Dogo.redis.keys("urls:*:#{id}").first return unless key new Dogo.redis.get(key) end |
.shortened?(url) ⇒ Boolean
Check if the given URL is already shortened. This will consider the host name and check if the url is already saved.
13 14 15 16 |
# File 'lib/dogo/url.rb', line 13 def self.shortened?(url) url.start_with?(Dogo.host) && find(url.split("/").last) end |
.valid?(url) ⇒ Boolean
Check if the given URL is valid, according the the URI
regular expression.
7 8 9 |
# File 'lib/dogo/url.rb', line 7 def self.valid?(url) URI::DEFAULT_PARSER.regexp[:ABS_URI] =~ url.to_s end |
Instance Method Details
#click! ⇒ Object
Increment the click counter for this URL.
36 37 38 |
# File 'lib/dogo/url.rb', line 36 def click! Dogo.redis.incr(click_key) end |