lib/bitly/client.rb in philnash-bitly-0.2 vs lib/bitly/client.rb in philnash-bitly-0.3

- old
+ new

@@ -16,34 +16,34 @@ def initialize(login,api_key) @login = login @api_key = api_key end - - def shorten(input, keyword=nil) + + def shorten(input, opts={}) if input.is_a? String - request = create_url "shorten", :longUrl => input, :keyword => keyword + request = create_url("shorten", :longUrl => input, :history => (opts[:history] ? 1 : nil)) result = get_result(request) result = {:long_url => input}.merge result[input] Bitly::Url.new(@login,@api_key,result) elsif input.is_a? Array - request = create_url "shorten" + request = create_url("shorten", :history => (opts[:history] ? 1 : nil)) request.query << "&" + input.map { |long_url| "longUrl=#{CGI.escape(long_url)}" }.join("&") unless input.nil? result = get_result(request) input.map do |long_url| new_url = {:long_url => long_url}.merge result[long_url] long_url = Bitly::Url.new(@login,@api_key,new_url) end else - raise ArgumentError + raise ArgumentError.new("Shorten requires either a url or an array of urls") end end def expand(input) if input.is_a? String if input.include? "bit.ly/" - hash = input.gsub(/^.*bit.ly\//,'') + hash = create_hash_from_url(input) request = create_url "expand", :hash => hash result = get_result(request) result = { :short_url => input, :hash => hash }.merge result[hash] else request = create_url "expand", :hash => input @@ -57,18 +57,18 @@ input.map do |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 + raise ArgumentError('Expand requires either a short url, a hash or an array of hashes') end end def info(input) if input.is_a? String if input.include? "bit.ly/" - hash = input.gsub(/^.*bit.ly\//,'') + hash = create_hash_from_url(input) 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 @@ -81,40 +81,42 @@ 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 + else + raise ArgumentError.new('Info requires either a short url, a hash or an array of hashes') end end def stats(input) if input.is_a? String if input.include? "bit.ly/" - hash = input.gsub(/^.*bit.ly\//,'') + hash = create_hash_from_url(input) 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,:stats => result) else - raise ArgumentError + raise ArgumentError.new("Stats requires either a short url or a hash") end end end end class BitlyError < StandardError attr_reader :code alias :msg :message - def initialize(msg, code, req) + def initialize(msg, code) @code = code - super("'#{req}' - #{msg}") + super("#{msg} - '#{code}'") end end # How it should work \ No newline at end of file