Sha256: 22f8f10dab50cc78abdcd8d1bb3f993a24d03cdb2c883ab12622af568a204dba
Contents?: true
Size: 1.39 KB
Versions: 3
Compression:
Stored size: 1.39 KB
Contents
# encoding: UTF-8 module Firefly class Url include DataMapper::Resource property :id, Serial property :url, String, :index => true, :length => 355 property :code, String, :index => true, :length => 64 property :clicks, Integer, :default => 0 property :created_at, DateTime, :default => Proc.new{Time.now} # Increase the visits counter by 1 def register_click! self.update(:clicks => self.clicks + 1) end # Shorten a long_url and return a new FireFly::Url def self.shorten(long_url, code = nil) code = nil if code !~ /\S/ 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 private # Generate a unique code, not already in use. def self.get_me_a_code code = Firefly::CodeFactory.next_code! if Firefly::Url.count(:code => code) > 0 code = get_me_a_code end code end # Normalize the URL def self.valid_url?(url) url.match(Firefly::Url::VALID_URL_REGEX) end def self.valid_code?(code) return true if code.nil? code.match(Firefly::Url::VALID_CODE_REGEX) && Firefly::Url.count(:code => code) == 0 end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
tmin-0.1.1 | lib/tmin/url.rb |
tmin-0.1.0 | lib/tmin/url.rb |
tmin-0.0.9 | lib/tmin/url.rb |