google_url_shortener 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/lib/google/url_shortener/base.rb +4 -8
- data/lib/google/url_shortener/request.rb +32 -0
- data/lib/google/url_shortener/url.rb +29 -19
- data/lib/google/url_shortener/version.rb +1 -1
- data/lib/google_url_shortener.rb +12 -16
- metadata +4 -3
data/.gitignore
CHANGED
@@ -1,16 +1,12 @@
|
|
1
|
-
# TODO: remove
|
2
|
-
require 'rubygems'
|
3
|
-
require 'restclient'
|
4
|
-
require 'json'
|
5
|
-
# EO TODO
|
6
|
-
|
7
1
|
module Google
|
8
2
|
module UrlShortener
|
9
3
|
class Base
|
10
|
-
URL = "https://www.googleapis.com/urlshortener/v1/url"
|
11
|
-
|
12
4
|
class << self
|
13
5
|
|
6
|
+
def log=(logger)
|
7
|
+
RestClient.log = logger
|
8
|
+
end
|
9
|
+
|
14
10
|
def api_key=(key)
|
15
11
|
@@api_key = key
|
16
12
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Google
|
2
|
+
module UrlShortener
|
3
|
+
module Request
|
4
|
+
BASE_URL = "https://www.googleapis.com/urlshortener/v1/url"
|
5
|
+
REQUEST_HEADERS = { :content_type => :json, :accept => :json }
|
6
|
+
|
7
|
+
def post(params={})
|
8
|
+
response = RestClient.post(BASE_URL, format_post_params(params), REQUEST_HEADERS)
|
9
|
+
parse(response)
|
10
|
+
end
|
11
|
+
|
12
|
+
def get(params={})
|
13
|
+
full_url = [BASE_URL, "?", format_get_params(params)].join
|
14
|
+
response = RestClient.get(full_url)
|
15
|
+
parse(response)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
def parse(response)
|
20
|
+
JSON.parse(response)
|
21
|
+
end
|
22
|
+
|
23
|
+
def format_post_params(params={})
|
24
|
+
params.to_json
|
25
|
+
end
|
26
|
+
|
27
|
+
def format_get_params(params={})
|
28
|
+
params.collect { |k, v| "#{k}=#{v}" }.join("&")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
module Google
|
2
2
|
module UrlShortener
|
3
|
-
class Url
|
3
|
+
class Url < Base
|
4
|
+
include Request
|
4
5
|
attr_reader :long_url, :short_url, :status
|
5
6
|
|
6
7
|
def initialize(opts={})
|
@@ -10,33 +11,42 @@ module Google
|
|
10
11
|
end
|
11
12
|
|
12
13
|
def decode!
|
13
|
-
|
14
|
-
|
14
|
+
params = validate_and_prepare_params(:shortUrl => self.short_url, :projection => "FULL")
|
15
|
+
response = get(params)
|
16
|
+
|
15
17
|
# TODO: get more info from response
|
16
|
-
@long_url =
|
18
|
+
@long_url = response["longUrl"]
|
17
19
|
end
|
20
|
+
alias_method :expand!, :decode!
|
18
21
|
|
19
22
|
def encode!
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
+
params = validate_and_prepare_params(:longUrl => self.long_url)
|
24
|
+
response = post(params)
|
25
|
+
|
26
|
+
@short_url = response["id"]
|
23
27
|
end
|
28
|
+
alias_method :shorten!, :encode!
|
24
29
|
|
25
30
|
private
|
26
|
-
def
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
+
def validate_and_prepare_params(params={})
|
32
|
+
params = params_for_request(params)
|
33
|
+
|
34
|
+
params.each_pair do |k, v|
|
35
|
+
validate(k, params)
|
36
|
+
end
|
37
|
+
|
38
|
+
params
|
39
|
+
end
|
40
|
+
|
41
|
+
def params_for_request(params={})
|
42
|
+
base_params = { :key => self.class.api_key }
|
43
|
+
base_params.merge!(params)
|
31
44
|
end
|
32
45
|
|
33
|
-
def
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
json = RestClient.get Base::URL + "?" + params.collect { |k, v| "#{k}=#{v}" }.join("&")
|
38
|
-
puts json
|
39
|
-
JSON.parse(json)
|
46
|
+
def validate(key, hash={})
|
47
|
+
if hash[key].nil? || hash[key].empty?
|
48
|
+
raise "Key :#{key} missing from request parameters!"
|
49
|
+
end
|
40
50
|
end
|
41
51
|
end
|
42
52
|
end
|
data/lib/google_url_shortener.rb
CHANGED
@@ -1,16 +1,12 @@
|
|
1
|
-
|
2
|
-
require '
|
3
|
-
require '
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
url
|
13
|
-
puts url.decode!.inspect
|
14
|
-
|
15
|
-
# http://code.google.com/apis/urlshortener/v1/reference.html#method_urlshortener_url_get
|
16
|
-
|
1
|
+
# TODO: remove
|
2
|
+
require 'rubygems'
|
3
|
+
require 'restclient'
|
4
|
+
require 'json'
|
5
|
+
# EO TODO
|
6
|
+
|
7
|
+
ROOT = File.expand_path(File.dirname(__FILE__) + "/..")
|
8
|
+
|
9
|
+
%w{ version
|
10
|
+
request
|
11
|
+
base
|
12
|
+
url }.each { |f| require("#{ROOT}/lib/google/url_shortener/#{f}") }
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google_url_shortener
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Josh Nesbitt
|
@@ -52,6 +52,7 @@ files:
|
|
52
52
|
- google_url_shortener.gemspec
|
53
53
|
- lib/google/url_shortener/analytics.rb
|
54
54
|
- lib/google/url_shortener/base.rb
|
55
|
+
- lib/google/url_shortener/request.rb
|
55
56
|
- lib/google/url_shortener/url.rb
|
56
57
|
- lib/google/url_shortener/version.rb
|
57
58
|
- lib/google_url_shortener.rb
|