goshortener 1.1 → 1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +9 -3
- data/lib/goshortener.rb +12 -19
- metadata +19 -3
data/README.rdoc
CHANGED
@@ -10,8 +10,14 @@ Uses Google URL shortener API service to shorten/expand given URLs.
|
|
10
10
|
of if you are using bundler, add the following line to your Gemfile.
|
11
11
|
gem "goshortener"
|
12
12
|
|
13
|
-
=== Initialize
|
13
|
+
=== Initialize
|
14
|
+
go = GoShortener.new(YOUR_API_KEY)
|
15
|
+
or
|
14
16
|
go = GoShortener.new
|
17
|
+
|
18
|
+
Using Google API keys will increase the usage quota.
|
19
|
+
|
20
|
+
=== Shorten urls
|
15
21
|
short_url = go.shorten "http://github.com/luckydev"
|
16
22
|
|
17
23
|
=== To expand short urls
|
@@ -20,8 +26,8 @@ of if you are using bundler, add the following line to your Gemfile.
|
|
20
26
|
=== Example usage in irb
|
21
27
|
ruby-1.9.2-p136 :001 > require "goshortener"
|
22
28
|
=> true
|
23
|
-
ruby-1.9.2-p136 :002 > go = GoShortener.new
|
24
|
-
=> #<GoShortener:0x93b35e0 @base_url="https://www.googleapis.com/urlshortener/v1/url">
|
29
|
+
ruby-1.9.2-p136 :002 > go = GoShortener.new("yourapikeyfromgoogle")
|
30
|
+
=> #<GoShortener:0x93b35e0 @base_url="https://www.googleapis.com/urlshortener/v1/url", @api_key="yourapikeyfromgoogle">
|
25
31
|
ruby-1.9.2-p136 :003 > go.shorten "http://github.com/luckydev"
|
26
32
|
=> "http://goo.gl/TCZHi"
|
27
33
|
ruby-1.9.2-p136 :004 > go.lengthen "http://goo.gl/TCZHi"
|
data/lib/goshortener.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require "rubygems"
|
2
|
-
require "bundler/setup"
|
3
2
|
|
4
3
|
require "rest_client"
|
5
4
|
require "json"
|
@@ -7,16 +6,24 @@ require "json"
|
|
7
6
|
|
8
7
|
class GoShortener
|
9
8
|
|
10
|
-
|
9
|
+
#initialize with/without api key
|
10
|
+
def initialize(api_key="")
|
11
|
+
unless api_key == ""
|
12
|
+
@api_key = api_key
|
13
|
+
else
|
14
|
+
puts "[GoShortener] Use Google API key to increase your usage quota."
|
15
|
+
end
|
11
16
|
@base_url = "https://www.googleapis.com/urlshortener/v1/url"
|
12
17
|
end
|
13
18
|
|
19
|
+
#Given a long URL, Returns the true short url using http://goo.gl service
|
14
20
|
def shorten(long_url)
|
15
21
|
if long_url.is_a?(String)
|
16
22
|
request_json = {'longUrl' => long_url}.to_json
|
23
|
+
request_url = @api_key ? (@base_url + "?key=#{@api_key}") : @base_url
|
17
24
|
|
18
25
|
begin
|
19
|
-
response = RestClient.post
|
26
|
+
response = RestClient.post request_url, request_json, :accept => :json, :content_type => :json
|
20
27
|
rescue
|
21
28
|
raise "Please provide a valid url string"
|
22
29
|
end
|
@@ -29,25 +36,11 @@ class GoShortener
|
|
29
36
|
return short_url
|
30
37
|
end
|
31
38
|
|
39
|
+
#Given a short URL, Returns the true long url using http://goo.gl service
|
32
40
|
def lengthen(short_url)
|
33
|
-
##
|
34
|
-
# Accepts a short url shortened by http://goo.gl and returns original
|
35
|
-
# long url.
|
36
|
-
#
|
37
|
-
# Examples:
|
38
|
-
# go = GoShortener.new
|
39
|
-
#
|
40
|
-
# go.lengthen("http://goo.gl/TCZHi") #=> "http://github.com/luckydev"
|
41
|
-
#
|
42
|
-
# go.lengthen("http://www.goo.gl/TCZHi") #=> Error
|
43
|
-
# go.lengthen("goo.gl/TCZHi") #=> Error
|
44
|
-
# go.lengthen("http://goo.gl/") #=> Error
|
45
|
-
# go.lengthen("http://goo.gl/T") #=> Error
|
46
|
-
# go.lengthen("http://bit.ly/TCZHi") #=> Error
|
47
|
-
#
|
48
|
-
##
|
49
41
|
if short_url.is_a?(String)
|
50
42
|
request_params = {:shortUrl => short_url}
|
43
|
+
request_params.merge!(:key => @api_key) if @api_key
|
51
44
|
|
52
45
|
begin
|
53
46
|
response = RestClient.get @base_url, :params => request_params
|
metadata
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: goshortener
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 11
|
4
5
|
prerelease:
|
5
|
-
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 2
|
9
|
+
version: "1.2"
|
6
10
|
platform: ruby
|
7
11
|
authors:
|
8
12
|
- lakshmanan
|
@@ -10,7 +14,7 @@ autorequire:
|
|
10
14
|
bindir: bin
|
11
15
|
cert_chain: []
|
12
16
|
|
13
|
-
date: 2011-02-
|
17
|
+
date: 2011-02-22 00:00:00 +05:30
|
14
18
|
default_executable:
|
15
19
|
dependencies:
|
16
20
|
- !ruby/object:Gem::Dependency
|
@@ -21,6 +25,9 @@ dependencies:
|
|
21
25
|
requirements:
|
22
26
|
- - ">="
|
23
27
|
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
24
31
|
version: "0"
|
25
32
|
type: :runtime
|
26
33
|
version_requirements: *id001
|
@@ -32,6 +39,9 @@ dependencies:
|
|
32
39
|
requirements:
|
33
40
|
- - ">="
|
34
41
|
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
35
45
|
version: "0"
|
36
46
|
type: :runtime
|
37
47
|
version_requirements: *id002
|
@@ -61,17 +71,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
61
71
|
requirements:
|
62
72
|
- - ">="
|
63
73
|
- !ruby/object:Gem::Version
|
74
|
+
hash: 3
|
75
|
+
segments:
|
76
|
+
- 0
|
64
77
|
version: "0"
|
65
78
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
79
|
none: false
|
67
80
|
requirements:
|
68
81
|
- - ">="
|
69
82
|
- !ruby/object:Gem::Version
|
83
|
+
hash: 3
|
84
|
+
segments:
|
85
|
+
- 0
|
70
86
|
version: "0"
|
71
87
|
requirements: []
|
72
88
|
|
73
89
|
rubyforge_project:
|
74
|
-
rubygems_version: 1.5.
|
90
|
+
rubygems_version: 1.5.2
|
75
91
|
signing_key:
|
76
92
|
specification_version: 3
|
77
93
|
summary: GoShortener uses Google URL shortener service to shorten/expand urls.
|