Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Tidied up some bits.

  • Loading branch information...
commit 5cf9c3005c56c02cfd6b42270d56673e6a2b9045 1 parent 6ca0983
@philnash philnash authored
Showing with 56 additions and 45 deletions.
  1. +7 −34 lib/bitly/base.rb
  2. +49 −11 lib/bitly/url.rb
View
41 lib/bitly/base.rb
@@ -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
View
60 lib/bitly/url.rb
@@ -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"
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.