Sha256: 1f71db7ec42284f3b5d9161c6e2c60c8be587ef47a14aaaf2a3940654c0a0d52
Contents?: true
Size: 1.32 KB
Versions: 1
Compression:
Stored size: 1.32 KB
Contents
module Pixy class Shorten API_URL = 'http://p.tl/api/api_simple.php' attr_accessor :status, :long_url, :short_url, :counter def initialize(key, url) uri = URI "#{API_URL}?key=#{key}&url=#{escape_url(url)}" response = Net::HTTP.get_response(uri) result = JSON.parse(response.body) if response.code.to_i == 200 self.status = result['status'] self.long_url = result['long_url'] self.short_url = result['short_url'] self.counter = result['counter'] raise_exception self.status unless self.status == 'ok' else raise UnknownError, "Server responded with code #{response.code}" end end private def raise_exception(status) case status when 'empty long url' raise EmptyLongUrl, "Missing long URL." when 'empty API key' raise EmptyApiKey, "Missing API key." when 'API limit' raise ApiLimit, "API limit exceeded." when 'invalid API key' raise InvalidApiKey, "API key is invalid." when 'invalid long url' raise InvalidLongUrl, "The URL can not be shortened." else raise UnknownError, "Unknown status error." end end def escape_url(url) URI.escape(url, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
pixy-0.1.0 | lib/pixy/shorten.rb |