diff --git a/lib/googl.rb b/lib/googl.rb index e31ab12..6c5ed9b 100644 --- a/lib/googl.rb +++ b/lib/googl.rb @@ -9,6 +9,7 @@ require 'googl/expand' require 'googl/client_login' require 'googl/ruby_extensions' +require 'googl/url' require 'googl/oauth2/utils' require 'googl/oauth2/native' @@ -23,9 +24,9 @@ module Googl # url.short_url # => "http://goo.gl/ump4S" # - def shorten(url=nil) - raise ArgumentError.new("URL to shorten is required") if url.nil? || url.strip.empty? - Googl::Shorten.new(url) + def shorten(url) + to_shorten = Googl::Url.new(url) + Googl::Shorten.new(to_shorten.to_s) end # Expands a short URL or gets creation time and analytics @@ -85,9 +86,9 @@ def shorten(url=nil) # # For mor details, see http://code.google.com/intl/pt-BR/apis/urlshortener/v1/reference.html#resource_url # - def expand(url=nil, options={}) - raise ArgumentError.new("URL to expand is required") if url.nil? || url.strip.empty? - options = {:shortUrl => url, :projection => nil}.merge!(options) + def expand(url, options={}) + to_expand = Googl::Url.new(url) + options = {:shortUrl => to_expand.to_s, :projection => nil}.merge!(options) Googl::Expand.new(options) end diff --git a/lib/googl/url.rb b/lib/googl/url.rb new file mode 100644 index 0000000..9ba6e76 --- /dev/null +++ b/lib/googl/url.rb @@ -0,0 +1,13 @@ + module Googl + class Url < Base + def initialize(url_string) + @url_string = url_string + raise ArgumentError.new("URL is required") if url_string.nil? || url_string.strip.empty? + url_string + end + + def to_s + @url_string + end + end +end diff --git a/spec/googl/expand_spec.rb b/spec/googl/expand_spec.rb index 378e4b4..1b666c0 100644 --- a/spec/googl/expand_spec.rb +++ b/spec/googl/expand_spec.rb @@ -10,15 +10,15 @@ it { Googl.should respond_to(:expand) } - context "wirh invalid url" do + context "with invalid url" do it "should return error 404" do lambda { Googl.expand('http://goo.gl/blajjddkksijj') }.should raise_error(Exception, /404 Not Found/) end - it "should return error for required url" do - lambda { Googl.expand }.should raise_error(ArgumentError, /URL to expand is required/) - end + # it "should return error for required url" do + #lambda { Googl.expand }.should raise_error(ArgumentError, /URL to expand is required/) + #end it "should return status REMOVED" do Googl.expand('http://goo.gl/R7f68').status.should == 'REMOVED' diff --git a/spec/googl/shorten_spec.rb b/spec/googl/shorten_spec.rb index b005c25..37d9811 100644 --- a/spec/googl/shorten_spec.rb +++ b/spec/googl/shorten_spec.rb @@ -12,9 +12,9 @@ context "with invalid url" do - it "should return error for required url" do - lambda { Googl.shorten }.should raise_error(ArgumentError, "URL to shorten is required") - end + #it "should return error for required url" do + #lambda { Googl.shorten }.should raise_error(ArgumentError, "URL is required") + #end it "should return Unsupported content with type" do Googl::Request.headers.delete('Content-Type') diff --git a/spec/googl/url_spec.rb b/spec/googl/url_spec.rb new file mode 100644 index 0000000..d2f13aa --- /dev/null +++ b/spec/googl/url_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' +describe Googl::Url do + context "when creating an url" do + it "should return error when empty" do + lambda {Googl::Url.new(" ")}.should raise_error(ArgumentError, "URL is required") + end + it "should return error when not provided" do + lambda {Googl::Url.new}.should raise_error(ArgumentError) + end + end +end