Sha256: 3856535c3af5fb6300a80bd19aabca6a2bb2484a667ead4ae394fd41f0b0eb30
Contents?: true
Size: 1.17 KB
Versions: 4
Compression:
Stored size: 1.17 KB
Contents
# require 'yajl' require 'crack' require 'crack/json' require 'net/http' require 'cgi' module Tweetable class URL LONG_URL_API_URL = 'http://www.longurlplease.com/api/v1.1' LONG_URL_TIMEOUT = 5 def self.headers { "Content-Type" => 'application/json' } end # the api can handle multiple url queries and response, but this does just 1 def self.lookup_long_url(search) url = URI.parse(LONG_URL_API_URL) url.query = "q=#{CGI.escape(search)}" http = Net::HTTP.new(url.host, url.port) http.read_timeout = LONG_URL_TIMEOUT long_url = nil begin json = http.get(url.to_s, headers).body urls = Crack::JSON.parse(json) long_url = urls.values[0] rescue Crack::ParseError raise TweetableError.new("Error parsing json trying to get long URL: #{json.to_s}") rescue Timeout::Error => e raise TweetableError.new("Timeout trying to get long URL: #{e}") rescue Exception => e raise TweetableError.new("Error trying to get long URL: #{e}") end long_url.nil? ? search : long_url end end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
tweetable-0.1.14 | lib/tweetable/url.rb |
tweetable-0.1.13 | lib/tweetable/url.rb |
tweetable-0.1.11 | lib/tweetable/url.rb |
tweetable-0.1.10 | lib/tweetable/url.rb |