googleurlshortener 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +37 -0
- data/Rakefile +1 -0
- data/googleurlshortener.gemspec +24 -0
- data/lib/googleurlshortener.rb +71 -0
- data/lib/googleurlshortener/version.rb +3 -0
- data/test/test_url_shortener.rb +36 -0
- metadata +55 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
Google Url Shortener
|
2
|
+
====================
|
3
|
+
|
4
|
+
Gem for simply using Google's URL shortening service.
|
5
|
+
|
6
|
+
Installation
|
7
|
+
------------
|
8
|
+
|
9
|
+
gem install googleurlshortener
|
10
|
+
|
11
|
+
Usage
|
12
|
+
-----
|
13
|
+
|
14
|
+
There are two ways to use the shortener
|
15
|
+
|
16
|
+
1) Default (unauthenticated)
|
17
|
+
|
18
|
+
shortener = Googleurlshortener::Default.new
|
19
|
+
short_url = shortener.shorten("https://github.com/mkraft/GoogleAPIs")
|
20
|
+
|
21
|
+
2) Authenticated
|
22
|
+
|
23
|
+
authd_shortener = Googleurlshortener::Authenticated.new("someemail@gmail.com", "somepassword")
|
24
|
+
short_url = authd_shortener.shorten("https://github.com/mkraft/GoogleAPIs")
|
25
|
+
|
26
|
+
If you're authenticated you can use all of the normal methods and also get analytics about URL's you have shortened.
|
27
|
+
|
28
|
+
analytics_object = authd_shortener.analytics(short_url)
|
29
|
+
|
30
|
+
Methods
|
31
|
+
-------
|
32
|
+
|
33
|
+
Assuming a Googleurlshortener instance named `s`, one can do the following
|
34
|
+
|
35
|
+
- `s.shorten(long_url)`
|
36
|
+
- `s.expand(short_url)`
|
37
|
+
- `s.analytics(short_url)` (Authenticated only) Returns an object in the structure of the JSON object sent by Google.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "googleurlshortener/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "googleurlshortener"
|
7
|
+
s.version = Googleurlshortener::VERSION
|
8
|
+
s.authors = ["Martin Kraft"]
|
9
|
+
s.email = ["martinkraft@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Shorten and expand URL's with Google's shortener API.}
|
12
|
+
s.description = %q{Shorten a long URL, get click analylitcs about it, and re-expand it again using Google's URL shortening API.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "googleurlshortener"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
# s.add_runtime_dependency "rest-client"
|
24
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
#require "googleurlshortener/version"
|
2
|
+
require 'json'
|
3
|
+
require 'uri'
|
4
|
+
require 'net/http'
|
5
|
+
|
6
|
+
module Googleurlshortener
|
7
|
+
|
8
|
+
class Default
|
9
|
+
|
10
|
+
def shorten(long_url)
|
11
|
+
uri = URI("https://www.googleapis.com/urlshortener/v1/url")
|
12
|
+
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
|
13
|
+
request = Net::HTTP::Post.new uri.request_uri
|
14
|
+
if defined? @auth_token && @auth_token != "" && @auth_token != nil
|
15
|
+
request['Authorization'] = @auth_token
|
16
|
+
end
|
17
|
+
request["Content-Type"] = "application/json"
|
18
|
+
response = http.request(request, "{\"longUrl\": \"#{long_url}\"}")
|
19
|
+
@body = response.body
|
20
|
+
end
|
21
|
+
return JSON.parse(@body)["id"]
|
22
|
+
end
|
23
|
+
|
24
|
+
def expand(short_url)
|
25
|
+
uri = URI("https://www.googleapis.com/urlshortener/v1/url?shortUrl=#{short_url}")
|
26
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
27
|
+
response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') {|http|
|
28
|
+
http.request(request)
|
29
|
+
}
|
30
|
+
return JSON.parse(response.body)["longUrl"]
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
class Authenticated < Default
|
36
|
+
|
37
|
+
def initialize(email, password)
|
38
|
+
@auth_token = get_authentication_token(email, password)
|
39
|
+
end
|
40
|
+
|
41
|
+
def analytics(short_url)
|
42
|
+
uri = URI("https://www.googleapis.com/urlshortener/v1/url?shortUrl=#{short_url}&projection=FULL")
|
43
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
44
|
+
request['Authorization'] = @auth_token
|
45
|
+
response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') {|http|
|
46
|
+
http.request(request)
|
47
|
+
}
|
48
|
+
return JSON.parse(response.body)
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def get_authentication_token(email, password)
|
54
|
+
uri = URI("https://www.google.com/accounts/ClientLogin")
|
55
|
+
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
|
56
|
+
request = Net::HTTP::Post.new uri.request_uri
|
57
|
+
data = "accountType=HOSTED_OR_GOOGLE&Email=#{email}&Passwd=#{password}&service=lh2&source=someapp1"
|
58
|
+
response = http.request(request, data)
|
59
|
+
@body = response.body
|
60
|
+
end
|
61
|
+
start_index = @body.index('Auth=')
|
62
|
+
slice_of_auth_to_end = @body[start_index..-1]
|
63
|
+
end_index = slice_of_auth_to_end.index("\n")
|
64
|
+
auth_string = slice_of_auth_to_end[0...end_index]
|
65
|
+
auth_token = "GoogleLogin #{auth_string}"
|
66
|
+
return auth_token
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'shoulda'
|
3
|
+
require_relative '../lib/googleurlshortener'
|
4
|
+
|
5
|
+
module GoogleUrlShortener
|
6
|
+
class TestUrlShortener < Test::Unit::TestCase
|
7
|
+
|
8
|
+
context "shorten a URL" do
|
9
|
+
url_shortener = Googleurlshortener::Default.new
|
10
|
+
should "return a shorter url" do
|
11
|
+
long_url = "https://github.com/mkraft/GoogleAPIs"
|
12
|
+
short_url = url_shortener.shorten(long_url)
|
13
|
+
assert long_url.size > short_url.size
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "expand a URL" do
|
18
|
+
url_shortener = Googleurlshortener::Default.new
|
19
|
+
should "return a longer url" do
|
20
|
+
short_url = "http://goo.gl/cQJ6o"
|
21
|
+
long_url = url_shortener.expand(short_url)
|
22
|
+
assert long_url.size > short_url.size
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "get analytics object" do
|
27
|
+
url_shortener = Googleurlshortener::Authenticated.new("apitest33@gmail.com", "ruhak23A")
|
28
|
+
should "return an expected result for 'kind'" do
|
29
|
+
short_url = "http://goo.gl/cQJ6o"
|
30
|
+
analytics = url_shortener.analytics(short_url)
|
31
|
+
assert analytics["kind"] == "urlshortener#url"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: googleurlshortener
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Martin Kraft
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-18 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Shorten a long URL, get click analylitcs about it, and re-expand it again
|
15
|
+
using Google's URL shortening API.
|
16
|
+
email:
|
17
|
+
- martinkraft@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- googleurlshortener.gemspec
|
27
|
+
- lib/googleurlshortener.rb
|
28
|
+
- lib/googleurlshortener/version.rb
|
29
|
+
- test/test_url_shortener.rb
|
30
|
+
homepage: ''
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project: googleurlshortener
|
50
|
+
rubygems_version: 1.8.12
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: Shorten and expand URL's with Google's shortener API.
|
54
|
+
test_files:
|
55
|
+
- test/test_url_shortener.rb
|