From 8e5ca43c32d6d64cf328f863239fd775de996467 Mon Sep 17 00:00:00 2001 From: dfucci Date: Wed, 3 Dec 2014 16:15:11 +0200 Subject: [PATCH 1/3] added Url class to remove feature envy smell from expand/shorten methods --- lib/googl.rb | 13 +++++++------ spec/googl/shorten_spec.rb | 6 +++--- 2 files changed, 10 insertions(+), 9 deletions(-) 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/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') From a332f127dcf1c211cc021c85e4ec149eb46c7761 Mon Sep 17 00:00:00 2001 From: dfucci Date: Wed, 3 Dec 2014 16:16:55 +0200 Subject: [PATCH 2/3] fixed typo --- spec/googl/expand_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/googl/expand_spec.rb b/spec/googl/expand_spec.rb index 378e4b4..48a39e8 100644 --- a/spec/googl/expand_spec.rb +++ b/spec/googl/expand_spec.rb @@ -10,7 +10,7 @@ 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/) From 522d780c1e8f0950b3dfeb654e8644a14ff13fa5 Mon Sep 17 00:00:00 2001 From: dfucci Date: Wed, 3 Dec 2014 16:20:12 +0200 Subject: [PATCH 3/3] moved expand test with invalid url to url spec --- lib/googl/url.rb | 13 +++++++++++++ spec/googl/expand_spec.rb | 6 +++--- spec/googl/url_spec.rb | 11 +++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 lib/googl/url.rb create mode 100644 spec/googl/url_spec.rb 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 48a39e8..1b666c0 100644 --- a/spec/googl/expand_spec.rb +++ b/spec/googl/expand_spec.rb @@ -16,9 +16,9 @@ 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/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