lib/shorty/bitly.rb in shorty-0.1.1 vs lib/shorty/bitly.rb in shorty-0.1.2
- old
+ new
@@ -30,25 +30,23 @@
#
# - longurl: the URL to be shortened
def shorten( longurl )
query = {:longUrl => longurl}
query.merge!(@options)
- short = self.class.get('/shorten', :query => query)
- short = Crack::JSON.parse(short)
- short["errorCode"].zero? ? short["results"][longurl]["shortUrl"] : raise_error(short["errorCode"], short["errorMessage"])
+ short = Crack::JSON.parse(self.class.get('/shorten', :query => query))
+ short["errorCode"].zero? ? short["results"][longurl]["shortUrl"] : raise_error(short)
end
# expand- given a bit.ly url, returns long source url, takes:
#
# - shorturl: the bit.ly url, can either be the full url or missing http://bit.ly
def expand(shorturl)
- shorturl = gsub_url(shorturl)
- query = {:hash => shorturl}
+ hash = gsub_url(shorturl)
+ query = {:hash => hash}
query.merge!(@options)
- expand = self.class.get('/expand', :query => query)
- expand = Crack::JSON.parse(expand)
- expand["errorCode"].zero? ? expand["results"][shorturl]["longUrl"] : raise_error(expand["errorCode"], expand["errorMessage"])
+ expand = Crack::JSON.parse(self.class.get('/expand', :query => query))
+ expand["errorCode"].zero? ? expand["results"][hash]["longUrl"] : raise_error(expand)
end
# info - Given a bit.ly url or hash, return information about that page, such as the long source url, ...
def info(urlorhash, keys = [])
urlhash = gsub_url(urlorhash)
@@ -56,46 +54,56 @@
query = {:hash => urlhash, :keys => keys.join(',')}
else
query = {:hash => urlhash}
end
query.merge!(@options)
- stats = self.class.get('/info', :query => query)
- stats = Crack::JSON.parse(stats)
- stats["errorCode"].zero? ? stats["results"][urlhash] : raise_error(stats["errorCode"], stats["errorMessage"])
+ stats = Crack::JSON.parse(self.class.get('/info', :query => query))
+ stats["errorCode"].zero? ? stats["results"][urlhash] : raise_error(stats)
end
# stats - get stats on clicks and reffers, pass either:
#
# - shortURL: A single bitly url, eg: http://bit.ly/1RmnUT
# - hash: A single hash, eg: 1RmnUT
#
# Example:
# bitly = Shorty::Bitly.new('login', 'apikey')
- # bitly.expand('1RmnUT')
+ # bitly.stats('1RmnUT')
# Or:
# bitly = Shorty::Bitly.new('login', 'apikey')
- # bitly.expand('http://bit.ly/1RmnUT')
+ # bitly.stats('http://bit.ly/1RmnUT')
def stats(urlorhash)
urlhash = gsub_url(urlorhash)
query = {:hash => urlhash}
query.merge!(@options)
- stats = self.class.get('/stats', :query => query)
- stats = Crack::JSON.parse(stats)
- stats["errorCode"].zero? ? stats["results"] : raise_error(stats["errorCode"], stats["errorMessage"])
+ stats = Crack::JSON.parse(self.class.get('/stats', :query => query))
+ stats["errorCode"].zero? ? stats["results"] : raise_error(stats)
end
protected
def gsub_url(shorturl)
shorturl.split('/').last
- # shorturl = shorturl.gsub(/http:\/\//, '') if shorturl.include?('http://')
- # shorturl = shorturl.gsub(/bit.ly\//, '') if shorturl.include?('bit.ly/')
end
- def raise_error(code, message = '(no error message)')
+ def raise_error(hash)
+ code = hash["errorCode"]
+ message = hash["errorMessage"] || '(no error message)'
error = message + " (error code: #{code})"
raise Shorty::Bitly::Error, error
end
+
+ # We need to work on how to handle this and working with openstruct
+ # def handle_response(resp, url)
+ # r = {
+ # "error" => {
+ # "code" => resp["errorCode"],
+ # "message" => resp["errorMessage"]
+ # },
+ # "hash" => resp["results"][url]["hash"]
+ # }
+ # resp.to_openstruct
+ # end
end
end
\ No newline at end of file