Module: Qrcx

Defined in:
lib/qrcx.rb,
lib/qrcx/version.rb

Defined Under Namespace

Classes: Error, InvalidOrUnsupportedURL, RedirectURLUnsupported, Timeout

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.makelong(url) ⇒ Object

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/qrcx.rb', line 39

def self.makelong(url)
  url = CGI.escape(url)
  retry_count = 0

  begin
    short_url = Net::HTTP.get_response(URI.parse("http://qr.cx/api/?get=#{url}")).body
  rescue Timeout::Error
    retry_count = retry_count + 1
    retry if retry_count <= 1
    raise Timeout
  end
  raise Error.new(short_url) if short_url.include? "error"
  
  return short_url
end

.shorten(url) ⇒ Object

throws QRCX::Error, QRCX::InvalidOrUnsupportedURL (< Error), QRCX::Timeout (< Error)

QRCX::RedirectURLUnsupported (< InvalidOrUnsupportedURL)

throws all Errors that Net::HTTP.get_response may throw

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/qrcx.rb', line 16

def self.shorten(url)
  url = CGI.escape(url)
  retry_count = 0
  begin
    short_url = Net::HTTP.get_response(URI.parse("http://qr.cx/api/?longurl=#{url}")).body
  rescue Timeout::Error
    retry_count = retry_count + 1
    retry if retry_count <= 1
    raise Timeout.new($!)
 end

  #error handling
  if short_url.include? "error: either unsupported url or the url is not valid"
    response = Net::HTTP.get_response(URI.parse(CGI.unescape(url)))
    raise RedirectURLUnsupported.new("#{response.code} #{response.message} (#{CGI.unescape(url)})") if response.code =~ /3\d\d/
    raise InvalidOrUnsupportedURL 
  end
  
  raise Error.new(short_url) if short_url.include? "error"

  return short_url
end