Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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('[email protected]', '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
Expand Down
12 changes: 8 additions & 4 deletions lib/googl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/googl/client_login.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 24 additions & 1 deletion lib/googl/shorten.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions spec/fixtures/history.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
18 changes: 18 additions & 0 deletions spec/fixtures/shorten_ice1.json
Original file line number Diff line number Diff line change
@@ -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/"
}
18 changes: 18 additions & 0 deletions spec/fixtures/shorten_ice2.json
Original file line number Diff line number Diff line change
@@ -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/"
}
20 changes: 20 additions & 0 deletions spec/googl/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'}).
Expand Down