Class: Firefly::Url
- Inherits:
-
Object
- Object
- Firefly::Url
- Includes:
- DataMapper::Resource
- Defined in:
- lib/firefly/url.rb
Constant Summary collapse
- VALID_CODE_REGEX =
case-insensitive, UTF-8 encoded
/^[A-Za-z0-9\-_]{3,64}$/u
Class Method Summary collapse
-
.shorten(long_url, user_id, code = nil) ⇒ Object
Shorten a long_url and return a new FireFly::Url.
Instance Method Summary collapse
-
#register_click!(ip) ⇒ Object
Increase the visits counter by 1.
Class Method Details
.shorten(long_url, user_id, code = nil) ⇒ Object
Shorten a long_url and return a new FireFly::Url
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/firefly/url.rb', line 23 def self.shorten(long_url, user_id, 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, :user_id => user_id) || Firefly::Url.create(:url => long_url, :user_id => user_id) 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!(ip) ⇒ Object
Increase the visits counter by 1
15 16 17 18 19 20 |
# File 'lib/firefly/url.rb', line 15 def register_click!(ip) Firefly::Click.create(:code => code, :ip => ip) if Firefly::Click.count(:code => code, :ip => ip) <= Firefly::Click::MAX_CLICKS_PER_IP self.update(:clicks => self.clicks + 1) end end |