diff --git a/README.rdoc b/README.rdoc index 3ed373d..2c28687 100644 --- a/README.rdoc +++ b/README.rdoc @@ -47,6 +47,21 @@ For shorten requests, each short URL for an authenticated user is unique, and th Go to http://goo.gl to see URL statistics. +=== Shorten without generating new short URL (if exists long_url in history) + + client = Googl.client('user@gmail.com', 'my_valid_password') + url = client.shorten('https://github.com/zigotto/googl') + url.short_url + => http://goo.gl/DWDfi + + url = client.shorten('https://github.com/zigotto/googl') + url.short_url + => http://goo.gl/Oh48S + + url = client.shorten('https://github.com/zigotto/googl', unique: true) + url.short_url + => http://goo.gl/Oh48S + === OAuth Google supports three flows of OAuth 2.0 diff --git a/lib/googl.rb b/lib/googl.rb index e31ab12..4dab122 100644 --- a/lib/googl.rb +++ b/lib/googl.rb @@ -23,9 +23,9 @@ module Googl # url.short_url # => "http://goo.gl/ump4S" # - def shorten(url=nil) + def shorten(url=nil, options = {}) raise ArgumentError.new("URL to shorten is required") if url.nil? || url.strip.empty? - Googl::Shorten.new(url) + Googl::Shorten.new(url, options) end # Expands a short URL or gets creation time and analytics @@ -101,8 +101,12 @@ def expand(url=nil, options={}) # # Go to http://goo.gl to see URL statistics. # - def client(email, passwd) - Googl::ClientLogin.new(email, passwd) + def client(email = nil, passwd = nil) + unless(email.nil? && passwd.nil?) + @client_instance = Googl::ClientLogin.new(email, passwd) + end + + @client_instance end # OAuth 2.0 diff --git a/lib/googl/client_login.rb b/lib/googl/client_login.rb index 193c348..255603c 100644 --- a/lib/googl/client_login.rb +++ b/lib/googl/client_login.rb @@ -24,8 +24,8 @@ def initialize(email, passwd) # # See Googl.client # - def shorten(url) - Googl.shorten(url) + def shorten(url, options = {}) + Googl.shorten(url, options) end # Gets a user's history of shortened URLs. (Authenticated) diff --git a/lib/googl/shorten.rb b/lib/googl/shorten.rb index 5227e81..3a42f64 100644 --- a/lib/googl/shorten.rb +++ b/lib/googl/shorten.rb @@ -8,8 +8,31 @@ class Shorten < Base # Creates a new short URL, see Googl.shorten # - def initialize(long_url) + def initialize(long_url, args = {}) + args = {:unique => false}.merge(args) modify_headers('Content-Type' => 'application/json') + + # if long_url exists in history + # return the related short_url + if args[:unique] && !Googl.client.nil? + to_check = URI(long_url) + to_check.scheme ||= 'http' + to_check.path += '/' if(to_check.path[-1] != '/') + Googl.client.history.items.each do |item| + uri_from_history = URI(item.long_url) + + if(to_check.scheme == uri_from_history.scheme && + to_check.host == uri_from_history.host && + to_check.path == uri_from_history.path && + to_check.query == uri_from_history.query) + self.short_url = item.label + self.long_url = item.long_url + return self + end + + end + end + options = {"longUrl" => long_url}.to_json resp = post(API_URL, :body => options) if resp.code == 200 diff --git a/spec/fixtures/history.json b/spec/fixtures/history.json index 794ceaa..45392c8 100644 --- a/spec/fixtures/history.json +++ b/spec/fixtures/history.json @@ -147,6 +147,13 @@ Transfer-Encoding: chunked "longUrl": "http://jlopes.zigotto.com.br/", "status": "OK", "created": "2010-10-01T13:26:04.029+00:00" + }, + { + "kind": "urlshortener#url", + "id": "http://goo.gl/ice1", + "longUrl": "http://www.ice.com/", + "status": "OK", + "created": "2014-01-13T14:52:30.401+00:00" } ] } diff --git a/spec/fixtures/shorten_ice1.json b/spec/fixtures/shorten_ice1.json new file mode 100644 index 0000000..5c1847b --- /dev/null +++ b/spec/fixtures/shorten_ice1.json @@ -0,0 +1,18 @@ +HTTP/1.1 200 OK +ETag: "EEZ1AD443JkEgW3KJFaymzTd26A/41aG_KwuemZSq8rrtXp-EsY0Iuk" +Cache-Control: no-cache, no-store, max-age=0, must-revalidate +Pragma: no-cache +Expires: Fri, 01 Jan 1990 00:00:00 GMT +Date: Thu, 13 Jan 2011 13:07:26 GMT +Content-Type: application/json; charset=UTF-8 +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-XSS-Protection: 1; mode=block +Server: GSE +Transfer-Encoding: chunked + +{ + "kind": "urlshortener#url", + "id": "http://goo.gl/ice1", + "longUrl": "http://www.ice.com/" +} diff --git a/spec/fixtures/shorten_ice2.json b/spec/fixtures/shorten_ice2.json new file mode 100644 index 0000000..1b3712d --- /dev/null +++ b/spec/fixtures/shorten_ice2.json @@ -0,0 +1,18 @@ +HTTP/1.1 200 OK +ETag: "EEZ1AD443JkEgW3KJFaymzTd26A/41aG_KwuemZSq8rrtXp-EsY0Iuk" +Cache-Control: no-cache, no-store, max-age=0, must-revalidate +Pragma: no-cache +Expires: Fri, 01 Jan 1990 00:00:00 GMT +Date: Thu, 13 Jan 2011 13:07:26 GMT +Content-Type: application/json; charset=UTF-8 +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-XSS-Protection: 1; mode=block +Server: GSE +Transfer-Encoding: chunked + +{ + "kind": "urlshortener#url", + "id": "http://goo.gl/ice2", + "longUrl": "http://www.ice.com/" +} diff --git a/spec/googl/client_spec.rb b/spec/googl/client_spec.rb index 0d4e341..f56bd56 100644 --- a/spec/googl/client_spec.rb +++ b/spec/googl/client_spec.rb @@ -46,6 +46,26 @@ it "should return a short URL" do subject.short_url.start_with?("http://goo.gl/").should be_true end + + context "without unique parameter" do + + let(:first) { @client.shorten('http://www.ice.com')} + let(:second) { @client.shorten('http://www.ice.com')} + + it "should generate new short URL" do + first.short_url.should_not eql(second.short_url) + end + end + + context "with unique parameter" do + + let(:first) { @client.shorten('http://www.ice.com', unique: true)} + let(:second) { @client.shorten('http://www.ice.com', unique: true)} + + it "should get short URL from history" do + first.short_url.should eql(second.short_url) + end + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1486f27..634a28b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -76,6 +76,23 @@ def fake_urls?(status) :headers => {'Authorization'=>'GoogleLogin auth=DQAAAK8AAAC9ahL-o7g', 'Content-Type'=>'application/json'}). to_return(load_fixture('shorten_authenticated.json')) + # simulate random url generation + $COUNTER = true + params = "{\"longUrl\":\"http://www.ice.com\"}" + stub_request(:post, url_shorten). + with(:body => params, + :headers => {'Authorization'=>'GoogleLogin auth=DQAAAK8AAAC9ahL-o7g', 'Content-Type'=>'application/json'}). + to_return(lambda do |request| + if $COUNTER + $COUNTER = !$COUNTER + load_fixture('shorten_ice1.json') + else + $COUNTER = !$COUNTER + load_fixture('shorten_ice2.json') + end + end + ) + # History for ClientLogin stub_request(:get, "https://www.googleapis.com/urlshortener/v1/url/history"). with(:headers => {'Authorization'=>'GoogleLogin auth=DQAAAK8AAAC9ahL-o7g'}).