googleurlshortener 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/googleurlshortener.gemspec +1 -1
- data/lib/googleurlshortener.rb +24 -18
- data/lib/googleurlshortener/version.rb +1 -1
- metadata +2 -2
data/googleurlshortener.gemspec
CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.version = Googleurlshortener::VERSION
|
8
8
|
s.authors = ["Martin Kraft"]
|
9
9
|
s.email = ["martinkraft@gmail.com"]
|
10
|
-
s.homepage = ""
|
10
|
+
s.homepage = "https://github.com/mkraft/GoogleUrlShortener"
|
11
11
|
s.summary = %q{Shorten and expand URL's with Google's shortener API.}
|
12
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
13
|
|
data/lib/googleurlshortener.rb
CHANGED
@@ -9,6 +9,7 @@ module Googleurlshortener
|
|
9
9
|
|
10
10
|
def shorten(long_url)
|
11
11
|
uri = URI("https://www.googleapis.com/urlshortener/v1/url")
|
12
|
+
body = ""
|
12
13
|
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
|
13
14
|
request = Net::HTTP::Post.new uri.request_uri
|
14
15
|
if defined? @auth_token && @auth_token != "" && @auth_token != nil
|
@@ -16,19 +17,29 @@ module Googleurlshortener
|
|
16
17
|
end
|
17
18
|
request["Content-Type"] = "application/json"
|
18
19
|
response = http.request(request, "{\"longUrl\": \"#{long_url}\"}")
|
19
|
-
|
20
|
+
body = response.body
|
20
21
|
end
|
21
|
-
return JSON.parse(
|
22
|
+
return JSON.parse(body)["id"]
|
22
23
|
end
|
23
24
|
|
24
25
|
def expand(short_url)
|
25
|
-
|
26
|
-
|
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"]
|
26
|
+
json = get_json_response("https://www.googleapis.com/urlshortener/v1/url?shortUrl=#{short_url}")
|
27
|
+
return json["longUrl"]
|
31
28
|
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def get_json_response(url)
|
33
|
+
uri = URI(url)
|
34
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
35
|
+
if self.class.name == "Googleurlshortener::Authenticated"
|
36
|
+
request['Authorization'] = @auth_token
|
37
|
+
end
|
38
|
+
response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') {|http|
|
39
|
+
http.request(request)
|
40
|
+
}
|
41
|
+
return JSON.parse(response.body)
|
42
|
+
end
|
32
43
|
|
33
44
|
end
|
34
45
|
|
@@ -39,27 +50,22 @@ module Googleurlshortener
|
|
39
50
|
end
|
40
51
|
|
41
52
|
def analytics(short_url)
|
42
|
-
|
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)
|
53
|
+
return get_json_response("https://www.googleapis.com/urlshortener/v1/url?shortUrl=#{short_url}&projection=FULL")
|
49
54
|
end
|
50
55
|
|
51
56
|
private
|
52
57
|
|
53
58
|
def get_authentication_token(email, password)
|
54
59
|
uri = URI("https://www.google.com/accounts/ClientLogin")
|
60
|
+
body = ""
|
55
61
|
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
|
56
62
|
request = Net::HTTP::Post.new uri.request_uri
|
57
63
|
data = "accountType=HOSTED_OR_GOOGLE&Email=#{email}&Passwd=#{password}&service=lh2&source=someapp1"
|
58
64
|
response = http.request(request, data)
|
59
|
-
|
65
|
+
body = response.body
|
60
66
|
end
|
61
|
-
start_index =
|
62
|
-
slice_of_auth_to_end =
|
67
|
+
start_index = body.index('Auth=')
|
68
|
+
slice_of_auth_to_end = body[start_index..-1]
|
63
69
|
end_index = slice_of_auth_to_end.index("\n")
|
64
70
|
auth_string = slice_of_auth_to_end[0...end_index]
|
65
71
|
auth_token = "GoogleLogin #{auth_string}"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: googleurlshortener
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -27,7 +27,7 @@ files:
|
|
27
27
|
- lib/googleurlshortener.rb
|
28
28
|
- lib/googleurlshortener/version.rb
|
29
29
|
- test/test_url_shortener.rb
|
30
|
-
homepage:
|
30
|
+
homepage: https://github.com/mkraft/GoogleUrlShortener
|
31
31
|
licenses: []
|
32
32
|
post_install_message:
|
33
33
|
rdoc_options: []
|