Skip to content

Commit

Permalink
More tests and refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
jtadeulopes committed Jan 17, 2011
1 parent 3de6849 commit 7cd86b5
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 106 deletions.
6 changes: 4 additions & 2 deletions lib/googl.rb
Expand Up @@ -10,11 +10,13 @@

module Googl

def self.shorten(url)
def self.shorten(url=nil)
raise ArgumentError.new("URL to shorten is required") if url.blank?
Googl::Shorten.new(url)
end

def self.expand(url, options={})
def self.expand(url=nil, options={})
raise ArgumentError.new("URL to expand is required") if url.blank?
options = {:shortUrl => url, :projection => nil}.merge!(options)
Googl::Expand.new(options)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/googl/client_login.rb
Expand Up @@ -16,7 +16,7 @@ def initialize(email, passwd)
token = resp.split('=').last.gsub(/\n/, '')
modify_headers("Authorization" => "GoogleLogin auth=#{token}")
else
resp.response
raise Exception.new("#{resp.code} #{resp.parsed_response}")
end
end

Expand Down
5 changes: 3 additions & 2 deletions lib/googl/expand.rb
Expand Up @@ -4,7 +4,7 @@ class Expand

API_URL = "https://www.googleapis.com/urlshortener/v1/url"

attr_accessor :long_url, :analytics
attr_accessor :long_url, :analytics, :status

def initialize(options={})

Expand All @@ -14,8 +14,9 @@ def initialize(options={})
if resp.code == 200
@long_url = resp['longUrl']
@analytics = resp['analytics'].to_openstruct if resp.has_key?('analytics')
@status = resp['status']
else
resp.response
raise Exception.new("#{resp.code} #{resp.message}")
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/googl/shorten.rb
Expand Up @@ -14,12 +14,12 @@ def initialize(long_url)
@short_url = resp['id']
@long_url = resp['longUrl']
else
resp.response
raise Exception.new(resp.parsed_response)
end
end

def qr_code
short_url + ".qr" if !short_url.blank?
"#{short_url}.qr" if !short_url.blank?
end

end
Expand Down
6 changes: 2 additions & 4 deletions spec/client_spec.rb
Expand Up @@ -26,10 +26,8 @@

subject { Googl.client('my_invalid_gmail', 'my_invalid_passwod') }

describe "#code" do
it "should return 403" do
subject.code.should == 403
end
it "should return BadAuthentication" do
lambda { Googl.client('my_invalid_gmail', 'my_invalid_passwod') }.should raise_error(Exception, /403 Error=BadAuthentication/)
end

end
Expand Down
184 changes: 105 additions & 79 deletions spec/expand_spec.rb
Expand Up @@ -5,127 +5,153 @@
before :each do
fake_urls
end

context "when expand any goo.gl short URL" do

it { Googl.should respond_to(:expand) }

subject { Googl.expand('http://goo.gl/7lob') }
context "wirh 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

describe "#long_url" do
it "should return a long url" do
subject.long_url.should == 'http://jlopes.zigotto.com.br/'
it "should return status REMOVED" do
Googl.expand('http://goo.gl/R7f68').status.should == 'REMOVED'
end

end

context "with projection" do
context "with valid url" do

subject { Googl.expand('http://goo.gl/7lob') }

describe "#long_url" do
it "should return a long url" do
subject.long_url.should == 'http://jlopes.zigotto.com.br/'
end
end

describe "#status" do
it "should return a status of url" do
subject.status.should == 'OK'
end
end

context "with projection" do

context "full" do

context "full" do

subject { Googl.expand('http://goo.gl/DWDfi', :projection => :full) }
subject { Googl.expand('http://goo.gl/DWDfi', :projection => :full) }

describe "#all_time" do
let(:element) { subject.analytics.all_time }
describe "#all_time" do
let(:element) { subject.analytics.all_time }

it_should_behave_like 'a clicks'
it_should_behave_like 'a period'
it_should_behave_like 'a clicks'
it_should_behave_like 'a period'

it "should rename id to label" do
element.countries.first.label.should == "BR"
it "should rename id to label" do
element.countries.first.label.should == "BR"
end
end
end

describe "#month" do
let(:element) { subject.analytics.month }
describe "#month" do
let(:element) { subject.analytics.month }

it_should_behave_like 'a clicks'
it_should_behave_like 'a period'
end
it_should_behave_like 'a clicks'
it_should_behave_like 'a period'
end

describe "#week" do
let(:element) { subject.analytics.week }
describe "#week" do
let(:element) { subject.analytics.week }

it_should_behave_like 'a clicks'
it_should_behave_like 'a period'
end
it_should_behave_like 'a clicks'
it_should_behave_like 'a period'
end

describe "#day" do
let(:element) { subject.analytics.day }
describe "#day" do
let(:element) { subject.analytics.day }

it_should_behave_like 'a clicks'
it_should_behave_like 'a period'
end
it_should_behave_like 'a clicks'
it_should_behave_like 'a period'
end

describe "#two_hours" do
let(:element) { subject.analytics.two_hours }

describe "#two_hours" do
let(:element) { subject.analytics.two_hours }
it_should_behave_like 'a clicks'
end

it_should_behave_like 'a clicks'
end

end
context "analytics_clicks" do

context "analytics_clicks" do
subject { Googl.expand('http://goo.gl/DWDfi', :projection => :analytics_clicks) }

subject { Googl.expand('http://goo.gl/DWDfi', :projection => :analytics_clicks) }
describe "#all_time" do
let(:element) { subject.analytics.all_time }

describe "#all_time" do
let(:element) { subject.analytics.all_time }

it_should_behave_like 'a clicks'
end
it_should_behave_like 'a clicks'
end

describe "#month" do
let(:element) { subject.analytics.month }

it_should_behave_like 'a clicks'
end
describe "#month" do
let(:element) { subject.analytics.month }

describe "#week" do
let(:element) { subject.analytics.week }

it_should_behave_like 'a clicks'
end
it_should_behave_like 'a clicks'
end

describe "#day" do
let(:element) { subject.analytics.day }

it_should_behave_like 'a clicks'
end
describe "#week" do
let(:element) { subject.analytics.week }

describe "#two_hours" do
let(:element) { subject.analytics.two_hours }

it_should_behave_like 'a clicks'
end
it_should_behave_like 'a clicks'
end

end
describe "#day" do
let(:element) { subject.analytics.day }

context "analytics_top_strings" do
it_should_behave_like 'a clicks'
end

subject { Googl.expand('http://goo.gl/DWDfi', :projection => :analytics_top_strings) }
describe "#two_hours" do
let(:element) { subject.analytics.two_hours }

describe "#all_time" do
let(:element) { subject.analytics.all_time }
it_should_behave_like 'a clicks'
end

it_should_behave_like 'a period'
end

describe "#month" do
let(:element) { subject.analytics.month }
context "analytics_top_strings" do

it_should_behave_like 'a period'
end
subject { Googl.expand('http://goo.gl/DWDfi', :projection => :analytics_top_strings) }

describe "#week" do
let(:element) { subject.analytics.week }
describe "#all_time" do
let(:element) { subject.analytics.all_time }

it_should_behave_like 'a period'
end
it_should_behave_like 'a period'
end

describe "#month" do
let(:element) { subject.analytics.month }

it_should_behave_like 'a period'
end

describe "#day" do
let(:element) { subject.analytics.day }
describe "#week" do
let(:element) { subject.analytics.week }

it_should_behave_like 'a period'
end

describe "#day" do
let(:element) { subject.analytics.day }

it_should_behave_like 'a period'
end

it_should_behave_like 'a period'
end

end
Expand Down
24 changes: 24 additions & 0 deletions spec/fixtures/expand_404.json
@@ -0,0 +1,24 @@
HTTP/1.1 404 Not Found
Content-Type: application/json; charset=UTF-8
Date: Mon, 17 Jan 2011 19:54:56 GMT
Expires: Mon, 17 Jan 2011 19:54:56 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Transfer-Encoding: chunked

{
"error": {
"errors": [
{
"domain": "global",
"reason": "notFound",
"message": "Not Found"
}
],
"code": 404,
"message": "Not Found"
}
}
17 changes: 17 additions & 0 deletions spec/fixtures/expand_removed.json
@@ -0,0 +1,17 @@
HTTP/1.1 200 OK
ETag: "EEZ1AD443JkEgW3KJFaymzTd26A/gwecVfJbUMJGMk3OPVUqhzMMyY8"
Expires: Mon, 17 Jan 2011 20:11:13 GMT
Date: Mon, 17 Jan 2011 20:11:13 GMT
Cache-Control: public, max-age=0, must-revalidate, no-transform
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/R7f68",
"status": "REMOVED"
}
12 changes: 12 additions & 0 deletions spec/fixtures/shorten_invalid_content_type.json
@@ -0,0 +1,12 @@
HTTP/1.1 400 Bad Request
Content-Type: text/html; charset=UTF-8
Date: Mon, 17 Jan 2011 20:35:55 GMT
Expires: Mon, 17 Jan 2011 20:35:55 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Transfer-Encoding: chunked

Unsupported content with type: application/x-www-form-urlencoded
8 changes: 4 additions & 4 deletions spec/shared_examples.rb
Expand Up @@ -14,14 +14,14 @@

describe "#browsers" do
it { element.should respond_to(:browsers) }
it { element.countries.first.should respond_to(:count) }
it { element.countries.first.should respond_to(:label) }
it { element.browsers.first.should respond_to(:count) }
it { element.browsers.first.should respond_to(:label) }
end

describe "#platforms" do
it { element.should respond_to(:platforms) }
it { element.countries.first.should respond_to(:count) }
it { element.countries.first.should respond_to(:label) }
it { element.platforms.first.should respond_to(:count) }
it { element.platforms.first.should respond_to(:label) }
end

end
Expand Down

0 comments on commit 7cd86b5

Please sign in to comment.