Module: Vurlify

Defined in:
lib/vurlify.rb

Constant Summary collapse

VERSION =
'0.1.1'
VURL_HOST =
"http://vurl.me/"
VURL_PATH =
"vurls"
VURL_POST_PATH =
VURL_HOST + VURL_PATH

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cached_vurlObject

Returns the value of attribute cached_vurl.



15
16
17
# File 'lib/vurlify.rb', line 15

def cached_vurl
  @cached_vurl
end

#vurl_cached_atObject

Returns the value of attribute vurl_cached_at.



15
16
17
# File 'lib/vurlify.rb', line 15

def vurl_cached_at
  @vurl_cached_at
end

Instance Method Details

#shorten(url) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/vurlify.rb', line 17

def shorten url
  begin
    Timeout::timeout(5) do
      response = Crack::XML.parse RestClient.post(VURL_POST_PATH, :vurl => {:url => url})
      "#{VURL_HOST}#{response['vurl']['slug']}"
    end
  rescue
    url
  end
end

#vurl_for(slug) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/vurlify.rb', line 28

def vurl_for slug
  return cached_vurl if cached_vurl && Time.now <= (vurl_cached_at + 5)
  begin
    Timeout::timeout(5) do
      response = Crack::XML.parse RestClient.get(vurl_get_path(slug))
      self.cached_vurl = Vurl.new
      response['vurl'].each do |k,v|
        self.cached_vurl[k] = v
      end
      self.vurl_cached_at = Time.now
      cached_vurl
    end
  rescue
    nil
  end
end