googl-api 0.2.4 → 0.3.0

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/Gemfile CHANGED
@@ -1,5 +1,4 @@
1
1
  source "http://rubygems.org"
2
- # gem 'httparty'
3
2
 
4
3
  # Add dependencies to develop your gem here.
5
4
  # Include everything needed to run rake, tests, features, etc.
@@ -8,4 +7,6 @@ group :development do
8
7
  gem "bundler", "~> 1.0.0"
9
8
  gem "jeweler", "~> 1.5.2"
10
9
  gem "rcov", ">= 0"
10
+ gem 'httparty'
11
+ gem "google_client_login"
11
12
  end
@@ -1,7 +1,11 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
+ crack (0.1.8)
4
5
  git (1.2.5)
6
+ google_client_login (0.3.1)
7
+ httparty (0.6.1)
8
+ crack (= 0.1.8)
5
9
  jeweler (1.5.2)
6
10
  bundler (~> 1.0.0)
7
11
  git (>= 1.2.5)
@@ -15,6 +19,8 @@ PLATFORMS
15
19
 
16
20
  DEPENDENCIES
17
21
  bundler (~> 1.0.0)
22
+ google_client_login
23
+ httparty
18
24
  jeweler (~> 1.5.2)
19
25
  rcov
20
26
  shoulda
@@ -2,7 +2,7 @@
2
2
 
3
3
  A very simple Ruby wrapper for the Google Url Shortener.
4
4
 
5
- * service - http://goo.gl/
5
+ * service - http://goo.gl
6
6
  * api docs - http://code.google.com/apis/urlshortener/index.html
7
7
 
8
8
  == INSTALLATION:
@@ -11,11 +11,12 @@ A very simple Ruby wrapper for the Google Url Shortener.
11
11
 
12
12
  == USAGE:
13
13
 
14
- Create a googl client using your API key:
14
+ Create a googl client using your API key and/or with ClientLogin auth information. (note: you will need to "Activate" the "URL Shortener API" on this page to retrieve and user your api_key: https://code.google.com/apis/console/b/0/#project:1090911256926:overview )
15
15
 
16
- googl = GooglApi.new('your api key')
16
+ googl = GooglApi.new(:api_key => 'your_api_key', :email => 'google_email', :password => 'valid_password')
17
+
17
18
 
18
- Then use the client to shorten, extend or review the analytics for a URL.
19
+ Then use the client to shorten, extend or review the analytics for a URL. If authentication is provided the short_url provided will be specific to the given user and will show up in the users history.
19
20
 
20
21
  googl.shorten('http://www.google.com')
21
22
  googl.expand('http://goo.gl/fbsS')
@@ -23,9 +24,15 @@ Then use the client to shorten, extend or review the analytics for a URL.
23
24
 
24
25
  These methods will return a response object containing the short/long url and other values provide by the API
25
26
 
26
- * TODO: fix api key usage (broken in goo.gl api?)
27
- * TODO: implement analytics object
28
- * TODO: implement usage method
27
+ To access a users history you must provide authentication information
28
+
29
+ googl = GooglApi.new(:email => 'google_email', :password => 'valid_password')
30
+ googl.history
31
+
32
+ This method returns a hash containing the authenticated users history
33
+
34
+ * TODO: implement analytics and history response objects
35
+ * TODO: implement oauth
29
36
 
30
37
  == ACKNOWLEDGEMENTS
31
38
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.4
1
+ 0.3.0
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{googl-api}
8
- s.version = "0.2.4"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["John Allen"]
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
28
28
  "init.rb",
29
29
  "lib/googl-api.rb",
30
30
  "lib/googl-api/client.rb",
31
+ "lib/googl-api/client_login.rb",
31
32
  "lib/googl-api/response.rb",
32
33
  "test/helper.rb",
33
34
  "test/test_googl.rb"
@@ -51,12 +52,16 @@ Gem::Specification.new do |s|
51
52
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
52
53
  s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
53
54
  s.add_development_dependency(%q<rcov>, [">= 0"])
55
+ s.add_development_dependency(%q<httparty>, [">= 0"])
56
+ s.add_development_dependency(%q<google_client_login>, [">= 0"])
54
57
  s.add_runtime_dependency(%q<httparty>, [">= 0.6.1"])
55
58
  else
56
59
  s.add_dependency(%q<shoulda>, [">= 0"])
57
60
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
58
61
  s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
59
62
  s.add_dependency(%q<rcov>, [">= 0"])
63
+ s.add_dependency(%q<httparty>, [">= 0"])
64
+ s.add_dependency(%q<google_client_login>, [">= 0"])
60
65
  s.add_dependency(%q<httparty>, [">= 0.6.1"])
61
66
  end
62
67
  else
@@ -64,6 +69,8 @@ Gem::Specification.new do |s|
64
69
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
65
70
  s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
66
71
  s.add_dependency(%q<rcov>, [">= 0"])
72
+ s.add_dependency(%q<httparty>, [">= 0"])
73
+ s.add_dependency(%q<google_client_login>, [">= 0"])
67
74
  s.add_dependency(%q<httparty>, [">= 0.6.1"])
68
75
  end
69
76
  end
@@ -1,2 +1,3 @@
1
1
  require 'googl-api/client'
2
+ require 'googl-api/client_login'
2
3
  require 'googl-api/response'
@@ -4,8 +4,8 @@ module GooglApi
4
4
  API_URL = 'https://www.googleapis.com/urlshortener/'
5
5
  API_VERSION = 'v1'
6
6
 
7
- def self.new(api_key = nil)
8
- Client.new(api_key)
7
+ def self.new(options = {})
8
+ Client.new(options)
9
9
  end
10
10
 
11
11
  class Client
@@ -14,8 +14,14 @@ module GooglApi
14
14
  base_uri "#{API_URL}#{API_VERSION}"
15
15
  headers 'Content-Type' => 'application/json; charset=utf-8'
16
16
 
17
- def initialize(api_key = nil)
18
- @api_key = { :key => api_key } unless api_key.blank?
17
+ def initialize(options = {})
18
+ # set api key if provided
19
+ @api_key = { :key => options[:api_key] } if options.has_key?(:api_key)
20
+
21
+ # set auth token for client login if provided
22
+ if options[:email] && options[:password] && @token = ClientLogin.authenticate(options[:email], options[:password])
23
+ self.class.headers.merge!("Authorization" => "GoogleLogin auth=#{@token}")
24
+ end
19
25
  end
20
26
 
21
27
  def shorten(url)
@@ -32,6 +38,11 @@ module GooglApi
32
38
  raise ArgumentError.new("A URL to check analytics on is required") if url.blank?
33
39
  load_respose(self.class.get('/url', :query => @api_key.merge({ :shortUrl => url, :projection => projection })))
34
40
  end
41
+
42
+ def history
43
+ raise ArgumentError.new("Doing a history search requires a Client Login (or eventually OAuth to be set)") unless @token
44
+ self.class.get('/url/history')
45
+ end
35
46
  private
36
47
  def load_respose(resp)
37
48
  raise GooglError.new(resp.message, resp.code) if resp.code != 200
@@ -0,0 +1,16 @@
1
+ require 'google_client_login'
2
+
3
+ module GooglApi
4
+ class ClientLogin
5
+ class << self
6
+ def authenticate(email, password)
7
+ login_service = GoogleClientLogin::GoogleAuth.new(:accountType => 'HOSTED_OR_GOOGLE',
8
+ :service => 'urlshortener',
9
+ :source => 'companyName-applicationName-versionID'
10
+ )
11
+ login_service.authenticate(email, password)
12
+ auth_token = login_service.auth
13
+ end
14
+ end
15
+ end
16
+ end
@@ -17,7 +17,14 @@ require 'googl-api'
17
17
 
18
18
  class Test::Unit::TestCase
19
19
  def api_key
20
- # 'test_key'
21
- 'AIzaSyDIENPX6ZJoRFIS4Ix-ev5fOjRh8pNME9Y'
20
+ 'test_key'
21
+ end
22
+
23
+ def email
24
+ "email"
25
+ end
26
+
27
+ def password
28
+ "password"
22
29
  end
23
30
  end
@@ -3,13 +3,13 @@ require 'helper'
3
3
  class TestGoogl < Test::Unit::TestCase
4
4
  context "googl-api module" do
5
5
  should "create a new googl-api client" do
6
- c = GooglApi.new(api_key)
6
+ c = GooglApi.new(:api_key => api_key)
7
7
  assert_equal GooglApi::Client, c.class
8
8
  end
9
9
  end
10
10
  context "using the googl-api client" do
11
11
  setup do
12
- @client = GooglApi.new(api_key)
12
+ @client = GooglApi.new(:api_key => api_key)
13
13
  end
14
14
 
15
15
  context "shortening" do
@@ -21,13 +21,13 @@ class TestGoogl < Test::Unit::TestCase
21
21
  assert_kind_of GooglApi::Response, @url
22
22
  end
23
23
  should "return a short goo.gl url" do
24
- assert_equal "http://goo.gl/p2Jpa", @url.short_url
24
+ assert @url.short_url.match(/^http:\/\/goo.gl\//)
25
25
  end
26
26
  should "save the long url" do
27
27
  assert_equal "http://ruby-lang.org/", @url.long_url
28
28
  end
29
29
  should "provide a qr code url" do
30
- assert_equal "http://goo.gl/p2Jpa.qr", @url.qr_code
30
+ assert_equal @url.short_url, @url.qr_code[0..(@url.short_url.size - 1)]
31
31
  end
32
32
  end
33
33
  context "no links" do
@@ -97,5 +97,33 @@ class TestGoogl < Test::Unit::TestCase
97
97
  end
98
98
  end
99
99
  end
100
+ context "history" do
101
+ should "raise an ArgumentError because auth is not set" do
102
+ assert_raise ArgumentError do
103
+ @client.history
104
+ end
105
+ end
106
+ end
107
+ end
108
+ context "using google client login" do
109
+ setup do
110
+ @client_auth = GooglApi.new(:email => email, :password => password)
111
+ end
112
+
113
+ context "shortening" do
114
+ context "a single link" do
115
+ setup do
116
+ @url = @client_auth.shorten('http://ruby-lang.org/')
117
+ end
118
+ should "return a GooglApi::Response" do
119
+ assert_kind_of GooglApi::Response, @url
120
+ end
121
+ end
122
+ end
123
+ context "history" do
124
+ should "have history" do
125
+ assert_not_nil @client_auth.history
126
+ end
127
+ end
100
128
  end
101
129
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
- - 4
9
- version: 0.2.4
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - John Allen
@@ -76,6 +76,32 @@ dependencies:
76
76
  - !ruby/object:Gem::Dependency
77
77
  name: httparty
78
78
  requirement: &id005 !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: *id005
89
+ - !ruby/object:Gem::Dependency
90
+ name: google_client_login
91
+ requirement: &id006 !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *id006
102
+ - !ruby/object:Gem::Dependency
103
+ name: httparty
104
+ requirement: &id007 !ruby/object:Gem::Requirement
79
105
  none: false
80
106
  requirements:
81
107
  - - ">="
@@ -87,7 +113,7 @@ dependencies:
87
113
  version: 0.6.1
88
114
  type: :runtime
89
115
  prerelease: false
90
- version_requirements: *id005
116
+ version_requirements: *id007
91
117
  description: A very simple ruby wrapper for the goo.gl URL Shortening service
92
118
  email: john@threedogconsulting.com
93
119
  executables: []
@@ -109,6 +135,7 @@ files:
109
135
  - init.rb
110
136
  - lib/googl-api.rb
111
137
  - lib/googl-api/client.rb
138
+ - lib/googl-api/client_login.rb
112
139
  - lib/googl-api/response.rb
113
140
  - test/helper.rb
114
141
  - test/test_googl.rb
@@ -126,7 +153,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
126
153
  requirements:
127
154
  - - ">="
128
155
  - !ruby/object:Gem::Version
129
- hash: -2555399239044821738
156
+ hash: 3605682061074164118
130
157
  segments:
131
158
  - 0
132
159
  version: "0"