google_shortener 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +32 -0
- data/google_shortener.gemspec +1 -1
- data/lib/google_shortener.rb +24 -3
- data/lib/google_shortener/version.rb +1 -1
- data/test/google_shortener_test.rb +22 -0
- metadata +5 -5
data/README.markdown
CHANGED
@@ -29,6 +29,38 @@ Usage:
|
|
29
29
|
irb(main):005:0> google_shortener.api_key='your own API key'
|
30
30
|
=> "your own API key"
|
31
31
|
|
32
|
+
Analytics can be obtained for the short URL, to obtain detail
|
33
|
+
information set projection to 'FULL'. Other values for projection are
|
34
|
+
'ANALYTICS_CLICKS', 'ANALYTICS_TOP_STRINGS'
|
35
|
+
|
36
|
+
irb(main):006:0> google_shortener.analytics(google_shortener.shorten('http://www.twitter.com'), 'ANALYTICS_CLICKS')
|
37
|
+
=> {
|
38
|
+
"kind"=>"urlshortener#url",
|
39
|
+
"id"=>"http://goo.gl/CbHS",
|
40
|
+
"longUrl"=>"http://www.twitter.com/",
|
41
|
+
"status"=>"OK",
|
42
|
+
"analytics"=>{
|
43
|
+
"allTime"=>{
|
44
|
+
"shortUrlClicks"=>"655",
|
45
|
+
"longUrlClicks"=>"4515"
|
46
|
+
},
|
47
|
+
"month"=>{
|
48
|
+
"shortUrlClicks"=>"17",
|
49
|
+
"longUrlClicks"=>"50"
|
50
|
+
},
|
51
|
+
"week"=>{
|
52
|
+
"shortUrlClicks"=>"2",
|
53
|
+
"longUrlClicks"=>"12"
|
54
|
+
},
|
55
|
+
"day"=>{
|
56
|
+
"shortUrlClicks"=>"0",
|
57
|
+
"longUrlClicks"=>"5"},
|
58
|
+
"twoHours"=>{
|
59
|
+
"shortUrlClicks"=>"0",
|
60
|
+
"longUrlClicks"=>"0"
|
61
|
+
}}}
|
62
|
+
|
63
|
+
|
32
64
|
## Contributing
|
33
65
|
|
34
66
|
1. Fork it
|
data/google_shortener.gemspec
CHANGED
@@ -16,6 +16,6 @@ Gem::Specification.new do |gem|
|
|
16
16
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
17
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
18
|
gem.require_paths = ["lib"]
|
19
|
-
gem.add_development_dependency "
|
19
|
+
gem.add_development_dependency "rake"
|
20
20
|
gem.add_runtime_dependency "json"
|
21
21
|
end
|
data/lib/google_shortener.rb
CHANGED
@@ -4,12 +4,16 @@ require 'json'
|
|
4
4
|
class GoogleShortener
|
5
5
|
attr_accessor :api_key
|
6
6
|
|
7
|
+
PROJECTIONS = ['FULL', 'ANALYTICS_CLICKS', 'ANALYTICS_TOP_STRINGS']
|
8
|
+
|
7
9
|
def initialize
|
8
10
|
@api_key = 'AIzaSyDOY31us5qGR_MdMQKaTfGlhpOi8w3Ur7U'
|
9
11
|
end
|
10
12
|
|
11
13
|
def shorten(long_url)
|
12
|
-
|
14
|
+
raise ArgumentError, "Long URL should be a String" unless long_url.is_a?(String)
|
15
|
+
|
16
|
+
if long_url.empty?
|
13
17
|
raise ArgumentError, 'Long URL is required'
|
14
18
|
end
|
15
19
|
|
@@ -24,7 +28,9 @@ class GoogleShortener
|
|
24
28
|
end
|
25
29
|
|
26
30
|
def expand(short_url)
|
27
|
-
|
31
|
+
raise ArgumentError, "Short URL should be a String" unless short_url.is_a?(String)
|
32
|
+
|
33
|
+
if short_url.empty?
|
28
34
|
raise ArgumentError, 'Short URL is required'
|
29
35
|
end
|
30
36
|
res = get_from_google_url_shortener_api(short_url)
|
@@ -37,6 +43,20 @@ class GoogleShortener
|
|
37
43
|
end
|
38
44
|
end
|
39
45
|
|
46
|
+
def analytics(short_url, projection='FULL')
|
47
|
+
raise ArgumentError, "Short URL should be a String" unless short_url.is_a?(String)
|
48
|
+
|
49
|
+
if short_url.empty?
|
50
|
+
raise ArgumentError, 'Short URL is required'
|
51
|
+
end
|
52
|
+
|
53
|
+
raise ArgumentError, "Projection should be a String" unless projection.is_a?(String)
|
54
|
+
raise ArgumentError, "Projection can only be #{PROJECTIONS.join(',')}" unless PROJECTIONS.include?(projection)
|
55
|
+
|
56
|
+
res = get_from_google_url_shortener_api(short_url, {'projection' => projection})
|
57
|
+
res_body = JSON.load(res.body)
|
58
|
+
end
|
59
|
+
|
40
60
|
def valid_api_key?
|
41
61
|
res = post_to_google_url_shortener_api('http://www.google.com')
|
42
62
|
|
@@ -53,9 +73,10 @@ class GoogleShortener
|
|
53
73
|
'https://www.googleapis.com/urlshortener/v1/url'
|
54
74
|
end
|
55
75
|
|
56
|
-
def get_from_google_url_shortener_api(short_url)
|
76
|
+
def get_from_google_url_shortener_api(short_url, options = {})
|
57
77
|
uri = URI(google_url_shortener_location)
|
58
78
|
params = {'shortUrl' => short_url, 'key' => @api_key}
|
79
|
+
params = params.merge(options) unless options.empty?
|
59
80
|
uri.query = URI.encode_www_form(params)
|
60
81
|
req = Net::HTTP::Get.new(uri.request_uri)
|
61
82
|
req.content_type = 'application/json'
|
@@ -1 +1 @@
|
|
1
|
-
VERSION = "0.0.
|
1
|
+
VERSION = "0.0.3"
|
@@ -42,6 +42,28 @@ class GoogleShortenerTest < MiniTest::Unit::TestCase
|
|
42
42
|
assert_raises ArgumentError do
|
43
43
|
google_shortener.expand
|
44
44
|
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_analytics_with_short_url_and_projection
|
48
|
+
google_shortener = GoogleShortener.new
|
49
|
+
analytics_hash = google_shortener.analytics('http://goo.gl/fbsS')
|
50
|
+
assert_equal ['kind', 'id', 'longUrl', 'status', 'created', 'analytics'], analytics_hash.keys
|
51
|
+
|
52
|
+
analytics_hash = google_shortener.analytics('http://goo.gl/fbsS', 'ANALYTICS_CLICKS')
|
53
|
+
assert_equal ['kind', 'id', 'longUrl', 'status', 'analytics'], analytics_hash.keys
|
54
|
+
|
55
|
+
analytics_hash = google_shortener.analytics('http://goo.gl/fbsS', 'ANALYTICS_TOP_STRINGS')
|
56
|
+
assert_equal ['kind', 'id', 'longUrl', 'status', 'analytics'], analytics_hash.keys
|
57
|
+
|
58
|
+
assert_raises ArgumentError do
|
59
|
+
google_shortener.analytics('http://goo.gl/fbsS', 'foo')
|
60
|
+
end
|
61
|
+
end
|
45
62
|
|
63
|
+
def test_analytics_without_short_url
|
64
|
+
google_shortener = GoogleShortener.new
|
65
|
+
assert_raises ArgumentError do
|
66
|
+
google_shortener.analytics(nil)
|
67
|
+
end
|
46
68
|
end
|
47
69
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google_shortener
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,10 +9,10 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
15
|
+
name: rake
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
@@ -73,7 +73,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
73
73
|
version: '0'
|
74
74
|
segments:
|
75
75
|
- 0
|
76
|
-
hash:
|
76
|
+
hash: -3080101778216265152
|
77
77
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
78
|
none: false
|
79
79
|
requirements:
|
@@ -82,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
82
|
version: '0'
|
83
83
|
segments:
|
84
84
|
- 0
|
85
|
-
hash:
|
85
|
+
hash: -3080101778216265152
|
86
86
|
requirements: []
|
87
87
|
rubyforge_project:
|
88
88
|
rubygems_version: 1.8.25
|