Class: Firefly::Url

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Resource
Defined in:
lib/firefly/url.rb

Constant Summary collapse

VALID_URL_REGEX =
/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
VALID_CODE_REGEX =
/^[a-z0-9\-_]{3,64}$/i

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.shorten(long_url, code = nil) ⇒ Object

Shorten a long_url and return a new FireFly::Url



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/firefly/url.rb', line 21

def self.shorten(long_url, code = nil)
  code = nil if code !~ /\S/

  raise Firefly::InvalidUrlError.new unless valid_url?(long_url)
  raise Firefly::InvalidCodeError.new unless valid_code?(code)

  long_url = normalize_url(long_url)

  the_url = Firefly::Url.first(:url => long_url) || Firefly::Url.create(:url => long_url)
  return the_url unless the_url.code.nil?

  code ||= get_me_a_code
  the_url.update(:code => code)
  the_url
end

Instance Method Details

#register_click!Object

Increase the visits counter by 1



16
17
18
# File 'lib/firefly/url.rb', line 16

def register_click!
  self.update(:clicks => self.clicks + 1)
end