Sha256: 935c099ca6d01c35f5cc71a9c07c8cb0e8029537997a189d70b9e20464c8e4f5

Contents?: true

Size: 959 Bytes

Versions: 1

Compression:

Stored size: 959 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 => 255
    property :visits,       Integer,    :default => 0
    property :created_at,   DateTime,   :default => Time.now
    
    # Increase the visits counter by 1
    def visit!
      self.update(:visits => self.visits + 1)
    end
    
    # Encode a URL and return the encoded ID
    def self.encode(url)
    
      @result = self.first(:url => url)
    
      if @result.nil?
        @result = self.create(:url => url)
        @result.update(:code => Firefly::Base62.encode(@result.id.to_i))
      end
    
      return @result.code
    end
  
    # Decode a code to the original URL
    def self.decode(code)
      @result = Firefly::Url.first(:code => code)  
      return @result.nil? ? nil : @result
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
firefly-0.2.0 lib/firefly/url.rb