Skip to content

Commit

Permalink
Getting stats working.
Browse files Browse the repository at this point in the history
  • Loading branch information
philnash committed Jan 26, 2009
1 parent 2bd1d3a commit c21e68e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 10 deletions.
48 changes: 43 additions & 5 deletions lib/bitly/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,62 @@ def expand(input)
else
request = create_url "expand", :hash => input
result = get_result(request)
result = { :hash => input }.merge result[input]
result = { :hash => input, :short_url => "http://bit.ly/#{input}" }.merge result[input]
end
Bitly::Url.new(@login,@api_key,result)
elsif input.is_a? Array
request = create_url "expand", :hash => input.join(',')
result = get_result(request)
input.map do |hsh|
new_url = {:hash => hsh}.merge result[hsh]
new_url = {:hash => hsh, :short_url => "http://bit.ly/#{hsh}"}.merge result[hsh]
hsh = Bitly::Url.new(@login,@api_key,new_url)
end
else
raise ArgumentError
end
end

def info(input)
if input.is_a? String
if input.include? "bit.ly/"
hash = input.gsub(/^.*bit.ly\//,'')
request = create_url 'info', :hash => hash
result = get_result(request)
result = { :short_url => "http://bit.ly/#{hash}", :hash => hash }.merge result[hash]
else
request = create_url 'info', :hash => input
result = get_result(request)
result = { :short_url => "http://bit.ly/#{input}", :hash => input }.merge result[input]
end
Bitly::Url.new(@login,@api_key,result)
elsif input.is_a? Array
request = create_url "info", :hash => input.join(',')
result = get_result(request)
input.map do |hsh|
new_url = {:hash => hsh, :short_url => "http://bit.ly/#{hsh}"}.merge result[hsh]
hsh = Bitly::Url.new(@login,@api_key,:info => new_url)
end
end
end

def stats(input)
if input.is_a? String
if input.include? "bit.ly/"
hash = input.gsub(/^.*bit.ly\//,'')
request = create_url 'stats', :hash => hash
result = get_result(request)
result = { :short_url => "http://bit.ly/#{hash}", :hash => hash }.merge result
else
request = create_url 'stats', :hash => input
result = get_result(request)
result = { :short_url => "http://bit.ly/#{input}", :hash => input }.merge result
end
Bitly::Url.new(@login,@api_key,result)
else
raise ArgumentError
end
end

private

def create_url(resource="",args={})
Expand All @@ -80,9 +121,6 @@ def get_result(request)
raise BitlyError.new(result['errorMessage'],result['errorCode'],'expand')
end
end



end

end
Expand Down
40 changes: 37 additions & 3 deletions lib/bitly/url.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,51 @@
module Bitly

class Url
class Url < Client
include Bitly::Utils
attr_reader :long_url, :short_url, :hash

def initialize(login,api_key,obj=nil)
unless obj.nil?
instance_variablise(obj)
instance_variablise(obj, VARIABLES)
@info = obj[:info] if obj[:info]
@stats = obj[:stats] if obj[:stats]
end
@login = login
@api_key = api_key
end

def info
if @info.nil?
if @hash
request = create_url "info", :hash => @hash
result = get_result(request)[@hash]
instance_variablise(result)
@info = result
elsif @short_url
hash = @short_url.gsub(/^.*bit.ly\//,'')
request = create_url "info", :hash => hash
result = get_result(request)[hash]
instance_variablise(result)
@info = result
else
nil
end
else
@info
end
end

def stats
if @stats.nil?
# get info
else
@stats
end
end

private

VARIABLES = ['long_url', 'short_url', 'hash', 'user_hash', 'short_keyword_url']

end

end
4 changes: 2 additions & 2 deletions lib/bitly/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ def attr_define(k,v)
meta.class_eval { attr_reader k.to_sym }
end

def instance_variablise(obj)
def instance_variablise(obj,variables)
if obj.is_a? Hash
obj.each do |k,v|
if v.is_a? Hash
instance_variablise(v)
else
attr_define(underscore(k),v)
attr_define(underscore(k),v) if variables.include?(underscore(k))
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions test/test_bitly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ def test_returns_short_url

def test_returns_a_long_url
urls = @bitly.expand(["2bYgqR","1RmnUT"])
assert_equal urls[0].class, Bitly::Url
assert_equal urls[0].long_url, "http://cnn.com"
assert_equal urls[0].hash, "2bYgqR"
assert_equal urls[1].long_url, "http://google.com"
assert_equal urls[1].hash, "1RmnUT"
url = @bitly.expand("http://bit.ly/wQaT")
assert_equal url.class, Bitly::Url
assert_equal url.short_url, "http://bit.ly/wQaT"
assert_equal url.long_url, "http://google.com/"
assert_equal url.hash, "wQaT"
url2 = @bitly.expand("wQaT")
assert_equal url2.class, Bitly::Url
assert_equal url2.hash, "wQaT"
assert_equal url2.short_url, "http://bit.ly/wQaT"
assert_equal url2.long_url, "http://google.com/"
end
end

0 comments on commit c21e68e

Please sign in to comment.