Sha256: 3a6fc9a2c90925b07f987e81791850d984e6f8185c21567b5dddc740d42a9fd8
Contents?: true
Size: 1.57 KB
Versions: 2
Compression:
Stored size: 1.57 KB
Contents
# encoding: UTF-8 module Firefly class Url include DataMapper::Resource VALID_URL_REGEX = / /ix VALID_CODE_REGEX = / /i 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.normalize_url(url) url = URI.escape(URI.unescape(url)) URI.parse(url).normalize.to_s end 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
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
tmin-0.0.3 | lib/tmin/url.rb |
tmin-0.0.2 | lib/tmin/url.rb |