Skip to content

Commit

Permalink
Tidied up some bits.
Browse files Browse the repository at this point in the history
  • Loading branch information
philnash committed Jan 22, 2009
1 parent 6ca0983 commit 5cf9c30
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 45 deletions.
41 changes: 7 additions & 34 deletions lib/bitly/base.rb
Expand Up @@ -23,41 +23,14 @@ def shorten(longUrl)

def expand(opt)
url = Bitly::Url.new(@login,@api_key)
url.expand(opt)
url
end

def create_url(resource="",args={})
args = args.merge({:login => @login, :apiKey => @api_key, :version => API_VERSION})
url = URI.join(API_URL,resource)
url.query = args.map { |k,v| "%s=%s" % [URI.encode(k.to_s), URI.encode(v.to_s)] }.join("&")
url
end

def underscore(camel_cased_word) # stolen from rails
camel_cased_word.to_s.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end

def attr_define(k,v)
instance_variable_set("@#{k}", v)
meta = class << self; self; end
meta.class_eval { attr_reader k.to_sym }
end

def instance_variablise(obj)
if obj.is_a? Hash
obj.each do |k,v|
if v.is_a? Hash
instance_variablise(v)
else
attr_define(underscore(k),v)
end
end
if opt[:short_url]
url.expand_short_url(opt[:short_url])
elsif opt[:hash]
url.expand_hash(opt[:hash])
else
return nil
end
url
end

end
Expand Down
60 changes: 49 additions & 11 deletions lib/bitly/url.rb
Expand Up @@ -2,26 +2,31 @@ module Bitly

class Url < Base
attr_reader :long_url, :short_url, :hash

def shorten(url)
@long_url = url
request = create_url "shorten", :longUrl => url
result = JSON.parse(Net::HTTP.get(request))
if result['statusCode'] == "OK"
instance_variablise(result['results'][url])
else
raise BitlyError.new(result['errorMessage'],result['errorCode'],'shorten')
end
call(:long_url => url)
end

def expand(opts)
def expand_hash(hash)
call(:hash => hash)
end

def expand_short_url(url)
call(:short_url => url)
end

private

def call(opts)
if opts[:short_url]
arg = @short_url = opts[:short_url]
request = create_url "expand", :shortUrl => @short_url
elsif opts[:hash]
arg = @hash = opts[:hash]
request = create_url "expand", :hash => @hash
else
return
elsif opts[:long_url]
arg = @long_url = opts[:long_url]
request = create_url "shorten", :longUrl => @long_url
end
result = JSON.parse(Net::HTTP.get(request))
if result['statusCode'] == "OK"
Expand All @@ -30,6 +35,39 @@ def expand(opts)
raise BitlyError.new(result['errorMessage'],result['errorCode'],'expand')
end
end

def create_url(resource="",args={})
args = args.merge({:login => @login, :apiKey => @api_key, :version => API_VERSION})
url = URI.join(API_URL,resource)
url.query = args.map { |k,v| "%s=%s" % [URI.encode(k.to_s), URI.encode(v.to_s)] }.join("&")
url
end

def underscore(camel_cased_word) # stolen from rails
camel_cased_word.to_s.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end

def attr_define(k,v)
instance_variable_set("@#{k}", v)
meta = class << self; self; end
meta.class_eval { attr_reader k.to_sym }
end

def instance_variablise(obj)
if obj.is_a? Hash
obj.each do |k,v|
if v.is_a? Hash
instance_variablise(v)
else
attr_define(underscore(k),v)
end
end
end
end
end

end

0 comments on commit 5cf9c30

Please sign in to comment.