Skip to content

Commit

Permalink
Finished work on major functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
philnash committed Jan 26, 2009
1 parent c21e68e commit 97be33c
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 44 deletions.
4 changes: 2 additions & 2 deletions History.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=== 1.0.0 / 2009-01-20
=== 0.1.0 / 2009-01-26

* 1 major enhancement

* Birthday!
* First release

7 changes: 0 additions & 7 deletions Manifest.txt

This file was deleted.

12 changes: 8 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# -*- ruby -*-

require 'rubygems'
require 'hoe'
require 'rake'
require 'echoe'
require './lib/bitly.rb'

Hoe.new('bitly', Bitly::VERSION) do |p|
# p.rubyforge_name = 'bitlyx' # if different than lowercase project name
p.developer('philnash', '[email protected]')
Echoe.new('bitly', Bitly::VERSION) do |p|
p.description = "Use the bit.ly API to shorten or expand URLs"
p.url = "http://github.com/philnash/bitly"
p.author = "Phil Nash"
p.email = "[email protected]"
p.development_dependencies = []
end

# vim: syntax=Ruby
Empty file removed bin/bitly
Empty file.
26 changes: 6 additions & 20 deletions lib/bitly/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ module Bitly
API_VERSION = '2.0.1'
# login = 'philnash'
# api_key = 'R_7776acc394294b2b0ad2c261a91c483d'

def self.new(login, api_key)
Bitly::Client.new(login,api_key)
end

class Client

include Bitly::Utils
Expand Down Expand Up @@ -96,31 +101,12 @@ def stats(input)
result = get_result(request)
result = { :short_url => "http://bit.ly/#{input}", :hash => input }.merge result
end
Bitly::Url.new(@login,@api_key,result)
Bitly::Url.new(@login,@api_key,:stats => result)
else
raise ArgumentError
end
end

private

def create_url(resource="",args={})
args = args.merge({:login => @login, :apiKey => @api_key, :version => API_VERSION})
url = URI.join(API_URL,resource)
long_urls = args.delete(:long_urls)
url.query = args.map { |k,v| "%s=%s" % [URI.encode(k.to_s), URI.encode(v.to_s)] }.join("&")
url.query << "&" + long_urls.map { |long_url| "longUrl=#{URI.encode(long_url)}" }.join("&") unless long_urls.nil?
url
end

def get_result(request)
result = JSON.parse(Net::HTTP.get(request))
if result['statusCode'] == "OK"
result = result['results']
else
raise BitlyError.new(result['errorMessage'],result['errorCode'],'expand')
end
end
end

end
Expand Down
19 changes: 11 additions & 8 deletions lib/bitly/url.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module Bitly

class Url < Client
class Url
include Bitly::Utils

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

def initialize(login,api_key,obj=nil)
unless obj.nil?
instance_variablise(obj, VARIABLES)
Expand All @@ -18,13 +20,13 @@ def info
if @hash
request = create_url "info", :hash => @hash
result = get_result(request)[@hash]
instance_variablise(result)
instance_variablise(result, VARIABLES)
@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)
instance_variablise(result, VARIABLES)
@info = result
else
nil
Expand All @@ -36,16 +38,17 @@ def info

def stats
if @stats.nil?
# get info
if @hash
request = create_url "stats", :hash => @hash
elsif @short_url
request = create_url "stats", :shortUrl => @short_url
end
@stats = get_result(request)
else
@stats
end
end

private

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

end

end
21 changes: 20 additions & 1 deletion lib/bitly/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,31 @@ def instance_variablise(obj,variables)
if obj.is_a? Hash
obj.each do |k,v|
if v.is_a? Hash
instance_variablise(v)
instance_variablise(v,variables)
else
attr_define(underscore(k),v) if variables.include?(underscore(k))
end
end
end
end

def create_url(resource="",args={})
args = args.merge({:login => @login, :apiKey => @api_key, :version => API_VERSION})
url = URI.join(API_URL,resource)
long_urls = args.delete(:long_urls)
url.query = args.map { |k,v| "%s=%s" % [URI.encode(k.to_s), URI.encode(v.to_s)] }.join("&")
url.query << "&" + long_urls.map { |long_url| "longUrl=#{URI.encode(long_url)}" }.join("&") unless long_urls.nil?
url
end

def get_result(request)
result = JSON.parse(Net::HTTP.get(request))
if result['statusCode'] == "OK"
result = result['results']
else
raise BitlyError.new(result['errorMessage'],result['errorCode'],'expand')
end
end

end
end
2 changes: 1 addition & 1 deletion lib/bitly/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Bitly
VERSION = '0.0.1'
VERSION = '0.1.0'
end
2 changes: 1 addition & 1 deletion test/test_bitly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class TestBitly < Test::Unit::TestCase
def setup
@api_key = 'R_7776acc394294b2b0ad2c261a91c483d'
@login = 'philnash'
@bitly = Bitly::Client.new(@login,@api_key)
@bitly = Bitly.new(@login,@api_key)
end

# not a good test, but it makes sure things are working for now.
Expand Down

0 comments on commit 97be33c

Please sign in to comment.