Class: Dogo::Url

Inherits:
Object
  • Object
show all
Defined in:
lib/dogo/url.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#fullObject (readonly)

Returns the value of attribute full.



3
4
5
# File 'lib/dogo/url.rb', line 3

def full
  @full
end

#idObject (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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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

#clicksObject

Return the clicks for the current URL.



41
42
43
# File 'lib/dogo/url.rb', line 41

def clicks
  Dogo.redis.get(click_key).to_i
end

#urlObject



31
32
33
# File 'lib/dogo/url.rb', line 31

def url
  File.join(Dogo.host, id)
end