google_plus 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/lib/google_plus.rb +14 -1
- data/lib/google_plus/activity.rb +32 -0
- data/lib/google_plus/cursor.rb +53 -0
- data/lib/google_plus/entity.rb +53 -0
- data/lib/google_plus/errors/connection_error.rb +15 -0
- data/lib/google_plus/errors/request_error.rb +16 -0
- data/lib/google_plus/person.rb +27 -0
- data/lib/google_plus/resource.rb +30 -0
- data/lib/google_plus/version.rb +1 -1
- data/spec/spec_helper.rb +5 -0
- metadata +33 -6
data/lib/google_plus.rb
CHANGED
@@ -1,8 +1,21 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require File.dirname(__FILE__) + '/google_plus/resource'
|
3
|
+
require File.dirname(__FILE__) + '/google_plus/cursor'
|
4
|
+
|
1
5
|
module GooglePlus
|
2
6
|
|
7
|
+
autoload :Activity, File.dirname(__FILE__) + '/google_plus/activity'
|
8
|
+
autoload :Person, File.dirname(__FILE__) + '/google_plus/person'
|
9
|
+
|
10
|
+
class << self
|
11
|
+
attr_accessor :api_key
|
12
|
+
end
|
13
|
+
|
3
14
|
# Return whether or not the we have an API
|
15
|
+
# For historic purposes - since this client existed before there
|
16
|
+
# was a GooglePlus API
|
4
17
|
def self.has_api?
|
5
|
-
|
18
|
+
true
|
6
19
|
end
|
7
20
|
|
8
21
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'json'
|
2
|
+
require File.dirname(__FILE__) + '/entity'
|
3
|
+
|
4
|
+
module GooglePlus
|
5
|
+
|
6
|
+
class Activity
|
7
|
+
|
8
|
+
extend GooglePlus::Resource
|
9
|
+
include GooglePlus::Entity
|
10
|
+
|
11
|
+
def self.get(activity_id)
|
12
|
+
data = make_request(:get, "activities/#{activity_id}")
|
13
|
+
Activity.new(JSON.parse(data)) if data
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.for_person(user_id, params = {})
|
17
|
+
collection = params[:collection] || :public
|
18
|
+
resource = "people/#{user_id}/activities/#{collection}"
|
19
|
+
GooglePlus::Cursor.new(self, :get, resource, params)
|
20
|
+
end
|
21
|
+
|
22
|
+
def person
|
23
|
+
@person ||= GooglePlus::Person.get(actor.id)
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize(hash)
|
27
|
+
load_hash(hash)
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module GooglePlus
|
4
|
+
|
5
|
+
class Cursor
|
6
|
+
|
7
|
+
extend GooglePlus::Resource
|
8
|
+
|
9
|
+
def items(params = {})
|
10
|
+
if instance_variable_defined?(:@items)
|
11
|
+
# TODO raise error if params are passed here, since they're meaningless
|
12
|
+
@items
|
13
|
+
else
|
14
|
+
@items = load_page(true, params)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def next_page(params = {})
|
19
|
+
@items = load_page(false, params)
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize(klass, method, resource, params)
|
23
|
+
@resource_klass = klass
|
24
|
+
@method = method
|
25
|
+
@resource = resource
|
26
|
+
@base_params = params
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
# Load the next page
|
32
|
+
def load_page(force = false, params)
|
33
|
+
if @next_page_token || force
|
34
|
+
ap = params.merge(@base_params)
|
35
|
+
ap[:maxResults] = ap.delete(:max_results) if ap.has_key?(:max_results)
|
36
|
+
ap[:fields] = ap.delete(:fields) if ap.has_key?(:fields)
|
37
|
+
ap[:pageToken] = @next_page_token if @next_page_token
|
38
|
+
# make request
|
39
|
+
if json = self.class.make_request(@method, @resource, ap)
|
40
|
+
data = JSON::parse(json)
|
41
|
+
@next_page_token = data['nextPageToken']
|
42
|
+
if items = data['items']
|
43
|
+
return data['items'].map { |d| @resource_klass.send(:new, d) }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
# otherwise, nil
|
48
|
+
nil
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module GooglePlus
|
2
|
+
|
3
|
+
module Entity
|
4
|
+
|
5
|
+
def method_missing(method, *arguments, &block)
|
6
|
+
method_s = method.to_s
|
7
|
+
if @attributes.has_key?(method_s)
|
8
|
+
val = @attributes[method_s]
|
9
|
+
if val.is_a?(Hash)
|
10
|
+
GooglePlus::EntityHash.new @attributes[method_s]
|
11
|
+
else
|
12
|
+
val
|
13
|
+
end
|
14
|
+
else
|
15
|
+
super
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def respond_to?(method, include_private = false)
|
20
|
+
method_s = method.to_s
|
21
|
+
if @attributes.has_key?(method_s)
|
22
|
+
true
|
23
|
+
else
|
24
|
+
super
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
attr_reader :attributes
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def load_hash(hash)
|
33
|
+
# underscore all of the attributes
|
34
|
+
@attributes = hash
|
35
|
+
@attributes.keys.each do |key|
|
36
|
+
mod_key = key.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').tr('-', '_').downcase
|
37
|
+
@attributes[mod_key] = @attributes.delete(key) unless mod_key == key
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
class EntityHash
|
44
|
+
|
45
|
+
include Entity
|
46
|
+
|
47
|
+
def initialize(hash = {})
|
48
|
+
load_hash(hash)
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'json'
|
2
|
+
require File.dirname(__FILE__) + '/entity'
|
3
|
+
|
4
|
+
module GooglePlus
|
5
|
+
|
6
|
+
class Person
|
7
|
+
|
8
|
+
extend GooglePlus::Resource
|
9
|
+
include GooglePlus::Entity
|
10
|
+
|
11
|
+
def self.get(user_id)
|
12
|
+
data = make_request(:get, "people/#{user_id}")
|
13
|
+
Person.new(JSON.parse(data)) if data
|
14
|
+
end
|
15
|
+
|
16
|
+
# get a cursor for activities for this user
|
17
|
+
def list_activities
|
18
|
+
GooglePlus::Activity.for_person(id)
|
19
|
+
end
|
20
|
+
|
21
|
+
def initialize(hash)
|
22
|
+
load_hash(hash)
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'restclient'
|
2
|
+
require File.dirname(__FILE__) + '/errors/request_error'
|
3
|
+
require File.dirname(__FILE__) + '/errors/connection_error'
|
4
|
+
|
5
|
+
module GooglePlus
|
6
|
+
|
7
|
+
module Resource
|
8
|
+
|
9
|
+
# Base resource URI - includes trailing slash
|
10
|
+
BASE_URI = 'https://www.googleapis.com/plus/v1/'
|
11
|
+
|
12
|
+
# Make a request to an external resource
|
13
|
+
def make_request(method, resource, params = {})
|
14
|
+
params[:key] = GooglePlus.api_key unless GooglePlus.api_key.nil?
|
15
|
+
params[:userIp] = params.delete(:user_ip) if params.has_key?(:user_id)
|
16
|
+
params[:pp] = '0' # google documentation is incorrect, it says 'prettyPrint'
|
17
|
+
begin
|
18
|
+
RestClient.get "#{BASE_URI}#{resource}", :params => params
|
19
|
+
rescue RestClient::Forbidden, RestClient::BadRequest => e
|
20
|
+
raise GooglePlus::RequestError.new(e)
|
21
|
+
rescue SocketError => e
|
22
|
+
raise GooglePlus::ConnectionError.new(e)
|
23
|
+
rescue RestClient::ResourceNotFound
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
data/lib/google_plus/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google_plus
|
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:
|
@@ -9,19 +9,46 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-
|
13
|
-
|
14
|
-
|
12
|
+
date: 2011-09-16 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70195096445960 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.6.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70195096445960
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rest-client
|
27
|
+
requirement: &70195096441660 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.6.1
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70195096441660
|
15
36
|
description: Google+ Ruby Gem
|
16
37
|
email: john.crepezzi@gmail.com
|
17
38
|
executables: []
|
18
39
|
extensions: []
|
19
40
|
extra_rdoc_files: []
|
20
41
|
files:
|
42
|
+
- lib/google_plus/activity.rb
|
43
|
+
- lib/google_plus/cursor.rb
|
44
|
+
- lib/google_plus/entity.rb
|
45
|
+
- lib/google_plus/errors/connection_error.rb
|
46
|
+
- lib/google_plus/errors/request_error.rb
|
47
|
+
- lib/google_plus/person.rb
|
48
|
+
- lib/google_plus/resource.rb
|
21
49
|
- lib/google_plus/version.rb
|
22
50
|
- lib/google_plus.rb
|
23
51
|
- spec/spec_helper.rb
|
24
|
-
has_rdoc: true
|
25
52
|
homepage: http://github.com/seejohnrun/google_plus
|
26
53
|
licenses: []
|
27
54
|
post_install_message:
|
@@ -42,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
42
69
|
version: '0'
|
43
70
|
requirements: []
|
44
71
|
rubyforge_project: google_plus
|
45
|
-
rubygems_version: 1.
|
72
|
+
rubygems_version: 1.8.10
|
46
73
|
signing_key:
|
47
74
|
specification_version: 3
|
48
75
|
summary: Ruby Gem for the Google+ API
|