Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adds info to version 3 API

  • Loading branch information...
commit 099bb8a56fcab7b0daf092ff7df85548034324ed 1 parent f3f5d83
@philnash philnash authored
View
6 History.txt
@@ -1,3 +1,9 @@
+=== 0.5.3 / 2010-07-30
+
+* 1 minor enhancement
+
+ * Added the info call to the version 3 API
+
=== 0.5.2 / 2010-07-18
* 1 minor enhancement
View
16 lib/bitly/v3/client.rb
@@ -54,6 +54,11 @@ def clicks(input)
get_method(:clicks, input)
end
+ # Like expand, but gets the title of the page and who created it
+ def info(input)
+ get_method(:info, input)
+ end
+
# Looks up the short url and global hash of a url or array of urls
#
# Returns the results in the order they were entered
@@ -107,15 +112,16 @@ def get_method(method, input)
end
query = "/#{method}?" + query.join('&')
response = get(query)
- results = response['data'][method.to_s].inject do |results, url|
+ results = response['data'][method.to_s].inject([]) do |results, url|
+ result_index = input.index(url['short_url'] || url['hash']) || input.index(url['global_hash'])
if url['error'].nil?
# builds the results array in the same order as the input
- results[input.index(url['short_url'] || url['hash'])] = Bitly::V3::Url.new(self, url)
+ results[result_index] = Bitly::V3::Url.new(self, url)
# remove the key from the original array, in case the same hash/url was entered twice
- input[input.index(url['short_url'] || url['hash'])] = nil
+ input[result_index] = nil
else
- results[input.index(url['short_url'] || url['hash'])] = Bitly::V3::MissingUrl.new(url)
- input[input.index(url['short_url'] || url['hash'])] = nil
+ results[result_index] = Bitly::V3::MissingUrl.new(url)
+ input[result_index] = nil
end
results
end
View
24 lib/bitly/v3/url.rb
@@ -16,6 +16,8 @@ def initialize(client, opts={})
@new_hash = (opts['new_hash'] == 1)
@user_clicks = opts['user_clicks']
@global_clicks = opts['global_clicks']
+ @title = opts['title']
+ @created_by = opts['created_by']
end
@short_url = "http://bit.ly/#{@user_hash}" unless @short_url
end
@@ -40,6 +42,22 @@ def global_clicks(opts={})
update_clicks_data if @global_clicks.nil? || opts[:force]
@global_clicks
end
+
+ # If the url already has the title, return it.
+ # IF there is no title or <tt>:force => true</tt> is passed,
+ # updates the info and returns the title
+ def title(opts={})
+ update_info if @title.nil? || opts[:force]
+ @title
+ end
+
+ # If the url already has the creator, return it.
+ # IF there is no creator or <tt>:force => true</tt> is passed,
+ # updates the info and returns the creator
+ def created_by(opts={})
+ update_info if @created_by.nil? || opts[:force]
+ @created_by
+ end
private
@@ -48,6 +66,12 @@ def update_clicks_data
@global_clicks = full_url.global_clicks
@user_clicks = full_url.user_clicks
end
+
+ def update_info
+ full_url = @client.info(@user_hash || @short_url)
+ @created_by = full_url.created_by
+ @title = full_url.title
+ end
end
end
end
View
2  lib/bitly/version.rb
@@ -1,3 +1,3 @@
module Bitly
- VERSION = '0.5.2'
+ VERSION = '0.5.3'
end
Please sign in to comment.
Something went wrong with that request. Please try again.