Sha256: 2198103ae9ef012d9347ec21154b84f2eb78959dd3d7015439760983115517c1

Contents?: true

Size: 795 Bytes

Versions: 3

Compression:

Stored size: 795 Bytes

Contents

module Firefly
  class Url
    include DataMapper::Resource

    property :id,           Serial
    property :url,          String,     :index => true, :length => 255
    property :code,         String,     :index => true, :length => 16
    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)
      the_url = Firefly::Url.first(:url => long_url) || Firefly::Url.create(:url => long_url)
      the_url.update(:code => Firefly::Base62.encode(the_url.id.to_i)) if the_url.code.nil?
      the_url
    end  
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
firefly-0.4.0.1 lib/firefly/url.rb
firefly-0.4.0 lib/firefly/url.rb
firefly-0.3.1 lib/firefly/url.rb