Module: GetShorty::InstanceMethods

Defined in:
lib/get_shorty.rb

Instance Method Summary collapse

Instance Method Details

#generate_long_urlObject

Goes against the MVC conventions (although we are generating this for the purpose of creating a short_url for the database so it is to do with the Model.)



61
62
63
64
65
# File 'lib/get_shorty.rb', line 61

def generate_long_url()
  begin
    send("#{self.class.name.downcase}_url", self) unless self.new_record? # Can't generate the url until we have an id and title.
  end
end

#generate_short_url(long_url = nil) ⇒ Object



67
68
69
70
71
72
# File 'lib/get_shorty.rb', line 67

def generate_short_url(long_url=nil)
  url = long_url || self.send(self.class.short_url_options[:long_url_method])
  if url
    self.class.shortening_serivce.shorten( url ) 
  end
end

#get_short_url(url = nil) ⇒ Object



56
57
58
# File 'lib/get_shorty.rb', line 56

def get_short_url(url=nil)
  self.send(self.class.short_url_options[:short_url_method]) || generate_short_url(url)
end

#get_short_url!(long_url = nil) ⇒ Object



44
45
46
47
48
# File 'lib/get_shorty.rb', line 44

def get_short_url!(long_url=nil)
  short_url = get_short_url(long_url)
  self.update_attributes( self.class.short_url_options[:short_url_method] => short_url ) unless has_short_url? || self.readonly?
  short_url
end

#has_short_url?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/get_shorty.rb', line 40

def has_short_url?
  self.send("#{self.class.short_url_options[:short_url_method]}?")
end

#set_short_url(short_url = generate_short_url) ⇒ Object



50
51
52
53
54
# File 'lib/get_shorty.rb', line 50

def set_short_url(short_url=generate_short_url)
  if short_url
    self.update_attributes( self.class.short_url_options[:short_url_method] => short_url )
  end
end