lib/bitly/v3/client.rb in bitly-0.6.1 vs lib/bitly/v3/client.rb in bitly-0.6.2
- old
+ new
@@ -61,11 +61,11 @@
# Looks up the short url and global hash of a url or array of urls
#
# Returns the results in the order they were entered
def lookup(input)
- input = [input] if input.is_a?(String)
+ input = arrayize(input)
query = input.inject([]) { |query, i| query << "url=#{CGI.escape(i)}" }
query = "/lookup?" + query.join('&')
response = get(query)
results = response['data']['lookup'].inject([]) do |results, url|
url['long_url'] = url['url']
@@ -102,15 +102,24 @@
def clicks_by_minute(input)
get_method(:clicks_by_minute, input)
end
# Takes a short url, hash or array of either and gets the clicks by day
- def clicks_by_day(input)
- get_method(:clicks_by_day, input)
+ def clicks_by_day(input, opts={})
+ opts.reject! { |k, v| k.to_s != 'days' }
+ get_method(:clicks_by_day, input, opts)
end
private
+
+ def arrayize(arg)
+ if arg.is_a?(String)
+ [arg]
+ else
+ arg.dup
+ end
+ end
def get(method, opts={})
opts[:query] ||= {}
opts[:query].merge!(@default_query_opts)
response = self.class.get(method, opts)
@@ -135,19 +144,22 @@
query = "/#{method}?" + query
response = get(query)
return Bitly::V3::Url.new(self,response['data'])
end
- def get_method(method, input)
- input = [input] if input.is_a? String
+ def get_method(method, input, opts={})
+ input = arrayize(input)
query = input.inject([]) do |query,i|
if is_a_short_url?(i)
query << "shortUrl=#{CGI.escape(i)}"
else
query << "hash=#{CGI.escape(i)}"
end
end
+ query = opts.inject(query) do |query, (k,v)|
+ query << "#{k}=#{v}"
+ end
query = "/#{method}?" + query.join('&')
response = get(query)
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?
@@ -163,6 +175,6 @@
end
return results.length > 1 ? results : results[0]
end
end
end
-end
\ No newline at end of file
+end