google_shortener 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.markdown +38 -0
- data/Rakefile +12 -0
- data/google_shortener.gemspec +21 -0
- data/lib/google_shortener/version.rb +1 -0
- data/lib/google_shortener.rb +77 -0
- metadata +91 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Chaitanya Vellanki
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
GoogleShortener is a Rubygem to create short url's using Google's URL shortener API (http://goo.gl/)
|
2
|
+
===============================================================================
|
3
|
+
|
4
|
+
-------------------------------------------------------------------------------
|
5
|
+
Installation:
|
6
|
+
-------------------------------------------------------------------------------
|
7
|
+
|
8
|
+
gem install google_shortener
|
9
|
+
|
10
|
+
-------------------------------------------------------------------------------
|
11
|
+
Usage:
|
12
|
+
-------------------------------------------------------------------------------
|
13
|
+
irb(main):001:0> require 'google_shortener'
|
14
|
+
=> true
|
15
|
+
|
16
|
+
irb(main):002:0> google_shortener = GoogleShortener.new
|
17
|
+
=> #<GoogleShortener:0x000000025a8ca8 @api_key="AIzaSyDOY31us5qGR_MdMQKaTfGlhpOi8w3Ur7U">
|
18
|
+
|
19
|
+
irb(main):003:0> google_shortener.shorten('http://www.google.com/')
|
20
|
+
=> "http://goo.gl/fbsS"
|
21
|
+
|
22
|
+
irb(main):004:0> google_shortener.expand("http://goo.gl/fbsS")
|
23
|
+
=> "http://www.google.com/"
|
24
|
+
|
25
|
+
By default, the above object contains a Google URL shortener API key, you
|
26
|
+
may want to use your own API key for analytics purposes. Refer to
|
27
|
+
https://developers.google.com/url-shortener/ .
|
28
|
+
|
29
|
+
irb(main):005:0> google_shortener.api_key='your own API key'
|
30
|
+
=> "your own API key"
|
31
|
+
|
32
|
+
## Contributing
|
33
|
+
|
34
|
+
1. Fork it
|
35
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
36
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
37
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
38
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'benchmark'
|
4
|
+
|
5
|
+
task :default => [:test]
|
6
|
+
|
7
|
+
desc 'Run basic tests'
|
8
|
+
Rake::TestTask.new do |test|
|
9
|
+
test.libs << "test"
|
10
|
+
test.test_files = Dir["test/*_test.rb"]
|
11
|
+
test.verbose = true
|
12
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'google_shortener/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "google_shortener"
|
8
|
+
gem.version = VERSION
|
9
|
+
gem.authors = ["Chaitanya Vellanki"]
|
10
|
+
gem.email = ["me@chaitanyavellanki.com"]
|
11
|
+
gem.description = %q{Rubygem to shorten long url}
|
12
|
+
gem.summary = %q{Uses url shortener services from Google}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
gem.add_development_dependency "shoulda-context"
|
20
|
+
gem.add_runtime_dependency "json"
|
21
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
VERSION = "0.0.1"
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
class GoogleShortener
|
5
|
+
attr_accessor :api_key
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@api_key = 'AIzaSyDOY31us5qGR_MdMQKaTfGlhpOi8w3Ur7U'
|
9
|
+
end
|
10
|
+
|
11
|
+
def shorten(long_url)
|
12
|
+
if long_url.nil?
|
13
|
+
raise ArgumentError, 'Long URL is required'
|
14
|
+
end
|
15
|
+
|
16
|
+
res = post_to_google_url_shortener_api(long_url)
|
17
|
+
res_body = JSON.load(res.body)
|
18
|
+
|
19
|
+
if res.is_a? Net::HTTPSuccess
|
20
|
+
res_body['id']
|
21
|
+
else
|
22
|
+
res_body
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def expand(short_url)
|
27
|
+
if short_url.nil?
|
28
|
+
raise ArgumentError, 'Short URL is required'
|
29
|
+
end
|
30
|
+
res = get_from_google_url_shortener_api(short_url)
|
31
|
+
res_body = JSON.load(res.body)
|
32
|
+
|
33
|
+
if res.is_a?(Net::HTTPSuccess)
|
34
|
+
res_body['longUrl']
|
35
|
+
else
|
36
|
+
res_body
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def valid_api_key?
|
41
|
+
res = post_to_google_url_shortener_api('http://www.google.com')
|
42
|
+
|
43
|
+
if res.is_a?(Net::HTTPSuccess)
|
44
|
+
true
|
45
|
+
else
|
46
|
+
false
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def google_url_shortener_location
|
53
|
+
'https://www.googleapis.com/urlshortener/v1/url'
|
54
|
+
end
|
55
|
+
|
56
|
+
def get_from_google_url_shortener_api(short_url)
|
57
|
+
uri = URI(google_url_shortener_location)
|
58
|
+
params = {'shortUrl' => short_url}
|
59
|
+
uri.query = URI.encode_www_form(params)
|
60
|
+
req = Net::HTTP::Get.new(uri.request_uri)
|
61
|
+
req.content_type = 'application/json'
|
62
|
+
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) do |http|
|
63
|
+
http.request(req)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def post_to_google_url_shortener_api(long_url)
|
68
|
+
uri = URI(google_url_shortener_location)
|
69
|
+
req = Net::HTTP::Post.new(uri.path)
|
70
|
+
req.body = JSON.dump({'longUrl' => long_url, 'key' => @api_key})
|
71
|
+
req.content_type = 'application/json'
|
72
|
+
|
73
|
+
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) do |http|
|
74
|
+
http.request(req)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: google_shortener
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chaitanya Vellanki
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: shoulda-context
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: json
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Rubygem to shorten long url
|
47
|
+
email:
|
48
|
+
- me@chaitanyavellanki.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.markdown
|
57
|
+
- Rakefile
|
58
|
+
- google_shortener.gemspec
|
59
|
+
- lib/google_shortener.rb
|
60
|
+
- lib/google_shortener/version.rb
|
61
|
+
homepage: ''
|
62
|
+
licenses: []
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
hash: 4113926791253946452
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
segments:
|
83
|
+
- 0
|
84
|
+
hash: 4113926791253946452
|
85
|
+
requirements: []
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 1.8.25
|
88
|
+
signing_key:
|
89
|
+
specification_version: 3
|
90
|
+
summary: Uses url shortener services from Google
|
91
|
+
test_files: []
|