module Qrcx

Constants

VERSION

Public Class Methods

makelong(url) click to toggle source
# 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) click to toggle source

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

QRCX::RedirectURLUnsupported (< InvalidOrUnsupportedURL)

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

# 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