Class: Shortie::Shorteners::Bitly

Inherits:
BaseShortener show all
Defined in:
lib/shortie/shorteners/bitly.rb

Instance Attribute Summary

Attributes inherited from BaseShortener

#url

Instance Method Summary collapse

Methods inherited from BaseShortener

#initialize, shorten

Constructor Details

This class inherits a constructor from Shortie::BaseShortener

Instance Method Details

#shortenObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/shortie/shorteners/bitly.rb', line 4

def shorten
  xml = SimpleHttp.get("http://api.bit.ly/v3/shorten?login=lassebunk&apiKey=R_f7df445f30899b8aec8ad5af7c732f0b&longUrl=#{escaped_url}&format=xml")
  doc = REXML::Document.new(xml)

  status_element = doc.elements["response/status_txt"]
  raise "No status text (status_txt) returned from bit.ly." if status_element.nil?

  status = status_element.text
  raise status unless status == "OK"

  url_element = doc.elements["response/data/url"]
  raise "No URL returned from bit.ly." if url_element.nil?

  url_element.text
end