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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ spec/dummy/tmp/
.rspec
.rvmrc
.rbenv-version
Gemfile.lock
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
language: ruby
rvm:
- 1.8.7
- 1.9.2
- 1.9.3
- 2.0.0
- 2.1
- 2.2
branches:
only:
- master
8 changes: 4 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
source "http://rubygems.org"

gem "httparty", "~> 0.10.0"
gem "httparty", "~> 0.10"
gem "json", ">= 1.4.6"

group :development do
gem "rspec", "~> 2.10.0"
gem "bundler", "~> 1.3.5"
gem "jeweler", "~> 1.8.3"
gem "rspec", "~> 2.10"
gem "jeweler", ">= 1.8.3"
gem "webmock", "~> 1.8.6"
gem "timecop", "~> 0.7.1"
end
74 changes: 0 additions & 74 deletions Gemfile.lock

This file was deleted.

5 changes: 4 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Google URL Shortener API in Ruby
``` ruby
url = Googl.shorten('http://www.zigotto.com')

# Optional - provide a user_ip and your google api_key to bypass Google's request rate limits
url = Googl.shorten('http://www.zigotto.com', "213.57.23.49", "google_api_key")

url.short_url
#=> "http://goo.gl/ump4S"

Expand Down Expand Up @@ -246,4 +249,4 @@ gem install googl

## License

MIT License. Copyright 2011-2014 Zigotto®. http://www.zigotto.com.br
MIT License. Copyright 2011-2015 Zigotto®. http://www.zigotto.com.br
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.6.3
0.7.1
44 changes: 21 additions & 23 deletions googl.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,33 @@
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-
# stub: googl 0.7.1 ruby lib

Gem::Specification.new do |s|
s.name = "googl"
s.version = "0.6.3"
s.version = "0.7.1"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.require_paths = ["lib"]
s.authors = ["Jesus Lopes"]
s.date = "2013-01-21"
s.date = "2015-05-21"
s.description = "Small library for Google URL Shortener API"
s.email = "jlopes@zigotto.com.br"
s.extra_rdoc_files = [
"README.rdoc"
"README.markdown"
]
s.files = [
".travis.yml",
"Gemfile",
"Gemfile.lock",
"MIT-LICENSE",
"README.rdoc",
"README.markdown",
"Rakefile",
"VERSION",
"googl.gemspec",
"lib/googl.rb",
"lib/googl/base.rb",
"lib/googl/client_login.rb",
"lib/googl/error.rb",
"lib/googl/expand.rb",
"lib/googl/oauth2/native.rb",
"lib/googl/oauth2/server.rb",
Expand Down Expand Up @@ -65,38 +67,34 @@ Gem::Specification.new do |s|
]
s.homepage = "http://github.com/zigotto/googl"
s.licenses = ["MIT"]
s.require_paths = ["lib"]
s.rubygems_version = "1.8.23"
s.rubygems_version = "2.4.5"
s.summary = "Wrapper for Google URL Shortener API"

if s.respond_to? :specification_version then
s.specification_version = 3
s.specification_version = 4

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<httparty>, ["~> 0.10.0"])
s.add_runtime_dependency(%q<httparty>, ["~> 0.10"])
s.add_runtime_dependency(%q<json>, [">= 1.4.6"])
s.add_development_dependency(%q<rspec>, ["~> 2.10.0"])
s.add_development_dependency(%q<bundler>, ["~> 1.2.3"])
s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
s.add_development_dependency(%q<rcov>, [">= 0"])
s.add_development_dependency(%q<rspec>, ["~> 2.10"])
s.add_development_dependency(%q<jeweler>, [">= 1.8.3"])
s.add_development_dependency(%q<webmock>, ["~> 1.8.6"])
s.add_development_dependency(%q<timecop>, ["~> 0.7.1"])
else
s.add_dependency(%q<httparty>, ["~> 0.10.0"])
s.add_dependency(%q<httparty>, ["~> 0.10"])
s.add_dependency(%q<json>, [">= 1.4.6"])
s.add_dependency(%q<rspec>, ["~> 2.10.0"])
s.add_dependency(%q<bundler>, ["~> 1.2.3"])
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
s.add_dependency(%q<rcov>, [">= 0"])
s.add_dependency(%q<rspec>, ["~> 2.10"])
s.add_dependency(%q<jeweler>, [">= 1.8.3"])
s.add_dependency(%q<webmock>, ["~> 1.8.6"])
s.add_dependency(%q<timecop>, ["~> 0.7.1"])
end
else
s.add_dependency(%q<httparty>, ["~> 0.10.0"])
s.add_dependency(%q<httparty>, ["~> 0.10"])
s.add_dependency(%q<json>, [">= 1.4.6"])
s.add_dependency(%q<rspec>, ["~> 2.10.0"])
s.add_dependency(%q<bundler>, ["~> 1.2.3"])
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
s.add_dependency(%q<rcov>, [">= 0"])
s.add_dependency(%q<rspec>, ["~> 2.10"])
s.add_dependency(%q<jeweler>, [">= 1.8.3"])
s.add_dependency(%q<webmock>, ["~> 1.8.6"])
s.add_dependency(%q<timecop>, ["~> 0.7.1"])
end
end

5 changes: 3 additions & 2 deletions lib/googl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'ostruct'
require 'json'

require 'googl/error'
require 'googl/utils'
require 'googl/base'
require 'googl/request'
Expand All @@ -23,9 +24,9 @@ module Googl
# url.short_url
# => "http://goo.gl/ump4S"
#
def shorten(url=nil)
def shorten(url=nil, user_ip = nil, api_key = nil)
raise ArgumentError.new("URL to shorten is required") if url.nil? || url.strip.empty?
Googl::Shorten.new(url)
Googl::Shorten.new(url, user_ip, api_key)
end

# Expands a short URL or gets creation time and analytics
Expand Down
4 changes: 4 additions & 0 deletions lib/googl/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Googl
class Error < StandardError
end
end
16 changes: 13 additions & 3 deletions lib/googl/shorten.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@ class Shorten < Base

# Creates a new short URL, see Googl.shorten
#
def initialize(long_url)
def initialize(long_url, user_ip = nil, api_key = nil)
modify_headers('Content-Type' => 'application/json')
options = {"longUrl" => long_url}.to_json
resp = post(API_URL, :body => options)
options = {"longUrl" => long_url}
shorten_url = API_URL

if (user_ip != nil && !user_ip.empty?)
options["userIp"] = user_ip
end
if (api_key != nil && !api_key.empty?)
shorten_url += "?key=#{api_key}"
end

options_json = options.to_json
resp = post(shorten_url, :body => options_json)
if resp.code == 200
self.short_url = resp['id']
self.long_url = resp['longUrl']
Expand Down
2 changes: 1 addition & 1 deletion lib/googl/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def get(url, params={})
end

def exception(msg)
Exception.new(msg)
Googl::Error.new(msg)
end

end
Expand Down
2 changes: 1 addition & 1 deletion spec/googl/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

describe "#short_url" do
it "should return a short URL" do
subject.short_url.start_with?("http://goo.gl/").should be_true
subject.short_url.start_with?("http://goo.gl/").should be true
end
end

Expand Down
21 changes: 12 additions & 9 deletions spec/googl/oauth2/native_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@

before do
@now = Time.now
Time.stub!(:now).and_return(@now)
Time.stub(:now).and_return(@now)
end

let(:native) { subject.request_access_token("4/SuSud6RqPojUXsPpeh-wSVCwnmTQ") }
Expand All @@ -97,33 +97,36 @@
end

describe "#expires?" do

before :each do
Time.stub!(:now).and_return(Time.parse("2011-04-23 15:30:00"))
before do
Timecop.freeze(DateTime.parse("2011-04-23 15:30:00"))
subject.request_access_token("4/SuSud6RqPojUXsPpeh-wSVCwnmTQ")
end

it "should be true if access token expires" do
Time.stub!(:now).and_return(Time.parse("2011-04-23 18:30:00"))
subject.expires?.should be_true
Timecop.freeze(DateTime.parse("2011-04-23 18:30:00")) do
subject.expires?.should be true
end
end

it "should be false if access token not expires" do
subject.expires?.should be_false
subject.expires?.should be false
end

after do
Timecop.return
end
end

describe "#authorized?" do

it "should return false if client is not authorized" do
subject.authorized?.should be_false
subject.authorized?.should be false
end

let(:native) { subject.request_access_token("4/SuSud6RqPojUXsPpeh-wSVCwnmTQ") }

it "should return true if client is authorized" do
native.authorized?.should be_true
native.authorized?.should be true
end

end
Expand Down
Loading
Loading