google_url_shortener 0.0.1 → 0.0.2

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 CHANGED
@@ -1,3 +1,4 @@
1
1
  pkg/*
2
2
  *.gem
3
3
  .bundle
4
+ examples/*
@@ -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
- # check for short
14
- resp = get(:shortUrl => self.short_url, :projection => "FULL")
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 = resp["longUrl"]
18
+ @long_url = response["longUrl"]
17
19
  end
20
+ alias_method :expand!, :decode!
18
21
 
19
22
  def encode!
20
- # check for long
21
- resp = post(:longUrl => self.long_url)
22
- @short_url = resp["id"]
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 post(params={})
27
- # check for API key
28
- params.merge!(:key => Base.api_key)
29
- json = RestClient.post Base::URL, params.to_json, :content_type => :json, :accept => :json
30
- JSON.parse(json)
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 get(params={})
34
- # check for API key
35
- params.merge!(:key => Base.api_key)
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
@@ -1,6 +1,6 @@
1
1
  module Google
2
2
  module UrlShortener
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
 
5
5
  end
6
6
  end
@@ -1,16 +1,12 @@
1
- require 'google/url_shortener/version'
2
- require 'google/url_shortener/base'
3
- require 'google/url_shortener/url'
4
-
5
- RestClient.log = $stdout
6
-
7
- Google::UrlShortener::Base.api_key = "AIzaSyCDp22BuCNkkd_LwQmnzOKA-kA7GXjvOsI"
8
-
9
- url = Google::UrlShortener::Url.new(:long_url => "http://blog.josh-nesbitt.net")
10
- puts url.encode!.inspect
11
-
12
- url = Google::UrlShortener::Url.new(:short_url => url.short_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: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
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