gdata4ruby 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +3 -0
- data/README +38 -0
- data/lib/gdata4ruby/acl/access_rule.rb +126 -0
- data/lib/gdata4ruby/base.rb +153 -0
- data/lib/gdata4ruby/gdata_object.rb +160 -0
- data/lib/gdata4ruby/request.rb +61 -0
- data/lib/gdata4ruby/service.rb +55 -0
- data/lib/gdata4ruby/utils/utils.rb +33 -0
- data/lib/gdata4ruby.rb +1 -0
- data/test/unit.rb +55 -0
- metadata +63 -0
data/CHANGELOG
ADDED
data/README
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
#=GData4Ruby
|
2
|
+
#
|
3
|
+
#==Introduction
|
4
|
+
#GData4Ruby is a full featured wrapper for the Google Data base API. GData4Ruby provides the ability
|
5
|
+
#to authenticate with GData using the ClientLogin method. The package also includes a base gdata object
|
6
|
+
#that can be subclassed to provide basic CRUD functions for all Google API service objects. Additionally,
|
7
|
+
#a basic ACL object is included for interacting with ACL feeds and setting access rules.
|
8
|
+
#
|
9
|
+
#==Author and Contact Information
|
10
|
+
#GData4Ruby was created and is maintained by {Mike Reich}[mailto:mike@seabourneconsulting.com]
|
11
|
+
#and is licenses under the GPL v2. Feel free to use and update, but be sure to contribute your
|
12
|
+
#code back to the project and attribute as required by the license.
|
13
|
+
#===Website
|
14
|
+
#http://cookingandcoding.com/gdata4ruby/
|
15
|
+
#
|
16
|
+
#==Description
|
17
|
+
#GData4Ruby has three major components: the service, the GData object and the AccessRule object. Each service
|
18
|
+
#represents a google account, and includes a username (email) and a password. You can use the GData service
|
19
|
+
#to authenticate either a google account or a google apps account.
|
20
|
+
#
|
21
|
+
#The GData object provides a base class for interacting with Google API objects, i.e. Documents, Events, etc. The
|
22
|
+
#GData object contains common attributes present in all Google API objects, and provides interfaces for basic CRUD
|
23
|
+
#functions. This class is meant to be subclassed.
|
24
|
+
#
|
25
|
+
#The AccessRule object provides a base class for interacting with Google Access Control Lists. ACLs provide the
|
26
|
+
#main permissions mechanism for most Google API services.
|
27
|
+
#
|
28
|
+
#==Examples
|
29
|
+
#Below are some common usage examples. For more examples, check the documentation.
|
30
|
+
#===Service
|
31
|
+
#1. Authenticate
|
32
|
+
# service = Service.new
|
33
|
+
# service.authenticate("user@gmail.com", "password", "cl")
|
34
|
+
#
|
35
|
+
#2. Authenticate with a specified GData version
|
36
|
+
# service = Service.new({:gdata_version => '3.0'})
|
37
|
+
# service.authenticate("user@gmail.com", "password", "cl")
|
38
|
+
|
@@ -0,0 +1,126 @@
|
|
1
|
+
# Author:: Mike Reich (mike@seabourneconsulting.com)
|
2
|
+
# Copyright:: Copyright (C) 2010 Mike Reich
|
3
|
+
# License:: GPL v2
|
4
|
+
#--
|
5
|
+
# Licensed under the General Public License (GPL), Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
#
|
15
|
+
# Feel free to use and update, but be sure to contribute your
|
16
|
+
# code back to the project and attribute as required by the license.
|
17
|
+
#++
|
18
|
+
require 'gdata4ruby/gdata_object'
|
19
|
+
|
20
|
+
module GData4Ruby
|
21
|
+
|
22
|
+
#Contains classes for interacting with Google ACL feeds
|
23
|
+
module ACL
|
24
|
+
|
25
|
+
#Represents an individual access rule entry in a Google ACL feed.
|
26
|
+
class AccessRule < GDataObject
|
27
|
+
XML = "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gAcl='http://schemas.google.com/acl/2007'>
|
28
|
+
<category scheme='http://schemas.google.com/g/2005#kind'
|
29
|
+
term='http://schemas.google.com/acl/2007#accessRule'/>
|
30
|
+
<gAcl:role value=''/>
|
31
|
+
<gAcl:scope type='user' value=''/>
|
32
|
+
</entry>"
|
33
|
+
|
34
|
+
#The Rule's user
|
35
|
+
attr_accessor :user
|
36
|
+
|
37
|
+
#The user's role
|
38
|
+
attr_accessor :role
|
39
|
+
|
40
|
+
#The parent GDataObject the rule applies to
|
41
|
+
attr_accessor :parent
|
42
|
+
|
43
|
+
#Creates a new AccessRule object. You must pass a valid Service and GDataObject, and can pass an optional hash
|
44
|
+
#of attributes to initialize the object with.
|
45
|
+
def initialize(service, parent, attributes = {})
|
46
|
+
super(service, attributes)
|
47
|
+
@xml = XML
|
48
|
+
raise ArgumentError, 'parent must be a GData4Ruby::GDataObject' if not parent.is_a? GData4Ruby::GDataObject
|
49
|
+
@parent = parent
|
50
|
+
@role = @user = nil
|
51
|
+
end
|
52
|
+
|
53
|
+
#Creates the AccessRule using the parent's acl_uri attribute.
|
54
|
+
def create
|
55
|
+
ret = service.send_request(Request.new(:post, @parent.acl_uri, to_xml))
|
56
|
+
if not ret or not load(ret.read_body)
|
57
|
+
raise SaveFailed, 'Could not create access rule'
|
58
|
+
end
|
59
|
+
return ret
|
60
|
+
end
|
61
|
+
|
62
|
+
#Loads data into the object. Accepts a string containing an XML <entry> object.
|
63
|
+
def load(string)
|
64
|
+
super(string)
|
65
|
+
@folders = []
|
66
|
+
xml = REXML::Document.new(string)
|
67
|
+
xml.root.elements.each(){}.map do |ele|
|
68
|
+
case ele.name
|
69
|
+
when 'role'
|
70
|
+
@role = ele.attributes['value']
|
71
|
+
when 'scope'
|
72
|
+
@user = ele.attributes['value'] ? ele.attributes['value'] : ele.attributes['type']
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
#Deletes the AccessRule
|
78
|
+
def delete
|
79
|
+
if @exists
|
80
|
+
service.send_request(Request.new(:delete, @parent.acl_uri+"/user:"+@user, nil, {"If-Match" => "*"}))
|
81
|
+
end
|
82
|
+
@exists = false
|
83
|
+
return true
|
84
|
+
end
|
85
|
+
|
86
|
+
#Finds an AccessRule based on the args passed.
|
87
|
+
#
|
88
|
+
#Args can be a hash containing either:
|
89
|
+
#*user*:: an email address/user id to search for. If found, returns the matching AccessRule object.
|
90
|
+
#*role*:: the role to search for. Returns an array of matching AccessRules, or an empty array if no matches are found.
|
91
|
+
def self.find(service, parent, args = {})
|
92
|
+
raise ArgumentError, 'Must supply a username or role to find by' if not args[:user] and not args[:role]
|
93
|
+
rules = []
|
94
|
+
ret = service.send_request(GData4Ruby::Request.new(:get, parent.acl_uri))
|
95
|
+
xml = REXML::Document.new(ret.read_body).root
|
96
|
+
xml.elements.each("entry") do |e|
|
97
|
+
e = GData4Ruby::Utils::add_namespaces(e)
|
98
|
+
rule = AccessRule.new(service, parent)
|
99
|
+
rule.load(e.to_s)
|
100
|
+
return rule if args[:user] and rule.user == args[:user]
|
101
|
+
rules << rule if args[:role] and rule.role == args[:role]
|
102
|
+
end
|
103
|
+
return args[:user] ? false : rules
|
104
|
+
end
|
105
|
+
|
106
|
+
#Returns a string containing the XML representation of the AccessRule
|
107
|
+
def to_xml
|
108
|
+
xml = REXML::Document.new(super)
|
109
|
+
xml.root.elements.each(){}.map do |ele|
|
110
|
+
case ele.name
|
111
|
+
when "role"
|
112
|
+
ele.attributes['value'] = @role
|
113
|
+
when 'scope'
|
114
|
+
if @user
|
115
|
+
ele.attributes['value'] = @user
|
116
|
+
else
|
117
|
+
ele.attributes['type'] = 'default'
|
118
|
+
ele.delete_attribute("value")
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
xml.to_s
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,153 @@
|
|
1
|
+
# Author:: Mike Reich (mike@seabourneconsulting.com)
|
2
|
+
# Copyright:: Copyright (C) 2010 Mike Reich
|
3
|
+
# License:: GPL v2
|
4
|
+
#--
|
5
|
+
# Licensed under the General Public License (GPL), Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
#
|
15
|
+
# Feel free to use and update, but be sure to contribute your
|
16
|
+
# code back to the project and attribute as required by the license.
|
17
|
+
#++
|
18
|
+
|
19
|
+
require "net/http"
|
20
|
+
require "net/https"
|
21
|
+
require 'gdata4ruby/request'
|
22
|
+
require 'gdata4ruby/utils/utils'
|
23
|
+
|
24
|
+
Net::HTTP.version_1_2
|
25
|
+
|
26
|
+
# GData4Ruby is a full featured wrapper for the base google data API
|
27
|
+
|
28
|
+
module GData4Ruby
|
29
|
+
|
30
|
+
class AuthenticationFailed < StandardError; end #:nodoc: all
|
31
|
+
|
32
|
+
class NotAuthenticated < StandardError; end
|
33
|
+
|
34
|
+
class InvalidService < StandardError; end
|
35
|
+
|
36
|
+
class HTTPRequestFailed < StandardError; end
|
37
|
+
|
38
|
+
class QueryParameterError < StandardError; end
|
39
|
+
|
40
|
+
#The ProxyInfo class contains information for configuring a proxy connection
|
41
|
+
|
42
|
+
class ProxyInfo
|
43
|
+
attr_accessor :address, :port, :username, :password
|
44
|
+
@address = nil
|
45
|
+
@port = nil
|
46
|
+
@username = nil
|
47
|
+
@password = nil
|
48
|
+
|
49
|
+
#The initialize function accepts four variables for configuring the ProxyInfo object.
|
50
|
+
#The proxy connection is initiated using the builtin Net::HTTP proxy support.
|
51
|
+
|
52
|
+
def initialize(address, port, username=nil, password=nil)
|
53
|
+
@address = address
|
54
|
+
@port = port
|
55
|
+
@username = username
|
56
|
+
@password = password
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
#The Base class includes the basic HTTP methods for communicating with the Google Data API.
|
61
|
+
#You shouldn't use this class directly, rather access the functionality through
|
62
|
+
#the Service subclass.
|
63
|
+
|
64
|
+
class Base
|
65
|
+
AUTH_URL = "https://www.google.com/accounts/ClientLogin"
|
66
|
+
@proxy_info = nil
|
67
|
+
@auth_token = nil
|
68
|
+
@debug = false
|
69
|
+
@gdata_version = '2.1'
|
70
|
+
|
71
|
+
#Contains the ProxyInfo object for using a proxy server
|
72
|
+
attr_accessor :proxy_info
|
73
|
+
|
74
|
+
#If set to true, debug will dump all raw HTTP requests and responses
|
75
|
+
attr_accessor :debug
|
76
|
+
|
77
|
+
#The GData version used by the service
|
78
|
+
attr_accessor :gdata_version
|
79
|
+
|
80
|
+
#Optionally, pass a hash of attributes to populate the class. If you want to use a GData version
|
81
|
+
#other than the default (2.1), pass a key/value pair, i.e. {:gdata_version => '1.0'}
|
82
|
+
def initialize(attributes = {})
|
83
|
+
@gdata_version = attributes[:gdata_version] ? attributes[:gdata_version] : '2.1'
|
84
|
+
end
|
85
|
+
|
86
|
+
#Sends a request to the Google Data System. Accepts a valid Request object, and returns a
|
87
|
+
#HTTPResult class.
|
88
|
+
def send_request(request)
|
89
|
+
raise ArgumentError 'Request must be a GData4Ruby::Request object' if not request.is_a?Request
|
90
|
+
puts "sending #{request.type} to url = #{request.url.to_s}" if @debug
|
91
|
+
do_request(request)
|
92
|
+
end
|
93
|
+
|
94
|
+
private
|
95
|
+
|
96
|
+
def do_request(request)
|
97
|
+
ret = nil
|
98
|
+
add_auth_header(request)
|
99
|
+
http = get_http_object(request.url)
|
100
|
+
puts "Sending request\nHeader: #{request.headers.inspect.to_s}\nContent: #{request.content.to_s}\n" if @debug
|
101
|
+
http.start do |ht|
|
102
|
+
ret = case request.type
|
103
|
+
when :get
|
104
|
+
ht.get(request.url.to_s, request.headers)
|
105
|
+
when :post
|
106
|
+
ht.post(request.url.to_s, request.content, request.headers)
|
107
|
+
when :put
|
108
|
+
ht.put(request.url.to_s, request.content, request.headers)
|
109
|
+
when :delete
|
110
|
+
ht.delete(request.url.to_s, request.headers)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
while ret.is_a?(Net::HTTPRedirection)
|
115
|
+
puts "Redirect received, resending post" if @debug
|
116
|
+
request.url = ret['location']
|
117
|
+
ret = do_request(request)
|
118
|
+
end
|
119
|
+
if not ret.is_a?(Net::HTTPSuccess)
|
120
|
+
puts "invalid response received: "+ret.code if @debug
|
121
|
+
raise HTTPRequestFailed, ret.body
|
122
|
+
end
|
123
|
+
puts "20x response received\nResponse: \n"+ret.read_body if @debug
|
124
|
+
return ret
|
125
|
+
end
|
126
|
+
|
127
|
+
def get_http_object(location)
|
128
|
+
if @proxy_info and @proxy_info.address
|
129
|
+
http = Net::HTTP.new(location.host, location.port, @proxy_info.address, @proxy_info.port, @proxy_info.username, @proxy_info.password)
|
130
|
+
else
|
131
|
+
http = Net::HTTP.new(location.host, location.port)
|
132
|
+
end
|
133
|
+
if location.scheme == 'https'
|
134
|
+
#fixed http/http misnaming via JohnMetta
|
135
|
+
puts "SSL True" if @debug
|
136
|
+
http.use_ssl = true
|
137
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
138
|
+
end
|
139
|
+
return http
|
140
|
+
end
|
141
|
+
|
142
|
+
def add_auth_header(request)
|
143
|
+
if @auth_token
|
144
|
+
if request.headers
|
145
|
+
request.headers.merge!({'Authorization' => "GoogleLogin auth=#{@auth_token}", "GData-Version" => @gdata_version})
|
146
|
+
else
|
147
|
+
content_type = (request.type == :get or request.type == :delete) ? 'application/x-www-form-urlencoded' : 'application/atom+xml'
|
148
|
+
request.headers = {'Authorization' => "GoogleLogin auth=#{@auth_token}", "GData-Version" => @gdata_version, 'Content-Type' => content_type}
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
@@ -0,0 +1,160 @@
|
|
1
|
+
# Author:: Mike Reich (mike@seabourneconsulting.com)
|
2
|
+
# Copyright:: Copyright (C) 2010 Mike Reich
|
3
|
+
# License:: GPL v2
|
4
|
+
#--
|
5
|
+
# Licensed under the General Public License (GPL), Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
#
|
15
|
+
# Feel free to use and update, but be sure to contribute your
|
16
|
+
# code back to the project and attribute as required by the license.
|
17
|
+
#++
|
18
|
+
|
19
|
+
require 'gdata4ruby/service'
|
20
|
+
|
21
|
+
module GData4Ruby
|
22
|
+
#The GDataObject class represents any <entry> object returned by a Google Service. Includes
|
23
|
+
#attributes for accessing the common elements/parameters of the object, and methods for CRUD
|
24
|
+
#operations.
|
25
|
+
class GDataObject
|
26
|
+
#A Service object
|
27
|
+
attr_accessor :service
|
28
|
+
|
29
|
+
#The entry title.
|
30
|
+
attr_accessor :title
|
31
|
+
|
32
|
+
#The current instance etag for the entry
|
33
|
+
attr_reader :etag
|
34
|
+
|
35
|
+
#The parent URI, if any
|
36
|
+
attr_reader :parent_uri
|
37
|
+
|
38
|
+
#The edit URI, for making changes to the entry
|
39
|
+
attr_reader :edit_uri
|
40
|
+
|
41
|
+
#A hash of additional feedLinks
|
42
|
+
attr_reader :feed_links
|
43
|
+
|
44
|
+
#The unique entry id, as represented by the <gd:resourceId> tag. Not to be confused
|
45
|
+
#with the Atom <id> tag, which is accessible as the feed_uri attribute.
|
46
|
+
attr_reader :id
|
47
|
+
|
48
|
+
#The entry's feed uri, otherwise known as the Atom <id> element value.
|
49
|
+
attr_reader :feed_uri
|
50
|
+
|
51
|
+
#A hash of categories
|
52
|
+
attr_reader :categories
|
53
|
+
|
54
|
+
#The feedLink that represents the entry's ACL feed.
|
55
|
+
attr_reader :acl_uri
|
56
|
+
|
57
|
+
#Indicates whether the object exists on the Google servers, i.e. has been created/saved.
|
58
|
+
def exists?
|
59
|
+
return @exists
|
60
|
+
end
|
61
|
+
|
62
|
+
#Initializes a new GDataObject. You must pass a valid Service object, and can pass
|
63
|
+
#an optional array of attributes to initialize values. To load data into an object,
|
64
|
+
#use the load method.
|
65
|
+
def initialize(service, attributes = {})
|
66
|
+
@xml ||= ''
|
67
|
+
@service ||= service
|
68
|
+
@exists = false
|
69
|
+
@title = @etag = @acl_uri = @edit_uri = @parent_uri = @feed_uri = nil
|
70
|
+
@categories = @feed_links = []
|
71
|
+
@include_etag = true
|
72
|
+
attributes.each do |key, value|
|
73
|
+
self.send("#{key}=", value)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
public
|
78
|
+
#Loads data into the object. Accepts a string containing an XML <entry> from a GData
|
79
|
+
#compliant feed.
|
80
|
+
def load(string)
|
81
|
+
@exists = @include_etag = true
|
82
|
+
@xml = string
|
83
|
+
xml = REXML::Document.new(string)
|
84
|
+
xml.root.elements.each(){}.map do |ele|
|
85
|
+
@etag = xml.root.attributes['etag'] if xml.root.attributes['etag']
|
86
|
+
case ele.name
|
87
|
+
when "id"
|
88
|
+
puts 'setting id' if service.debug
|
89
|
+
@feed_uri = ele.text
|
90
|
+
when 'resourceId'
|
91
|
+
@id = ele.text
|
92
|
+
when 'title'
|
93
|
+
@title = ele.text
|
94
|
+
when 'category'
|
95
|
+
@categories << {:label => ele.attributes['label'],
|
96
|
+
:scheme => ele.attributes['scheme'],
|
97
|
+
:term => ele.attributes['term']}
|
98
|
+
when 'link'
|
99
|
+
case ele.attributes['rel']
|
100
|
+
when 'http://schemas.google.com/docs/2007#parent'
|
101
|
+
@parent_uri = ele.attributes['href']
|
102
|
+
when 'edit'
|
103
|
+
@edit_uri = ele.attributes['href']
|
104
|
+
end
|
105
|
+
when 'feedLink'
|
106
|
+
@feed_links << {:rel => ele.attributes['rel'], :href => ele.attributes['href']}
|
107
|
+
@acl_uri = ele.attributes['href'] if ele.attributes['rel'].include? 'accessControlList'
|
108
|
+
end
|
109
|
+
end
|
110
|
+
return xml.root
|
111
|
+
end
|
112
|
+
|
113
|
+
#Saves the object if it exsits, otherwise creates it.
|
114
|
+
def save
|
115
|
+
if @exists
|
116
|
+
ret = service.send_request(Request.new(:put, @edit_uri, to_xml))
|
117
|
+
else
|
118
|
+
ret = create
|
119
|
+
end
|
120
|
+
if not ret or not load(ret.read_body)
|
121
|
+
raise SaveFailed, 'Could not save object'
|
122
|
+
end
|
123
|
+
return true
|
124
|
+
end
|
125
|
+
|
126
|
+
#Creates the object. This must be overridden in a subclass, as the feed url for creating new
|
127
|
+
#objects/entries is service dependent. In other words, each google service uses a different
|
128
|
+
#URI for saving new objects.
|
129
|
+
def create
|
130
|
+
return false
|
131
|
+
end
|
132
|
+
|
133
|
+
#Deletes the object.
|
134
|
+
def delete
|
135
|
+
if @exists
|
136
|
+
service.send_request(Request.new(:delete, @feed_uri, nil, {"If-Match" => "*"}))
|
137
|
+
end
|
138
|
+
@exists = false
|
139
|
+
return true
|
140
|
+
end
|
141
|
+
|
142
|
+
#Creates a new string containing the XML representation of the object as a GData compliant <entry>
|
143
|
+
#element.
|
144
|
+
def to_xml
|
145
|
+
xml = REXML::Document.new(@xml)
|
146
|
+
xml.root.elements.each(){}.map do |ele|
|
147
|
+
if @include_etag
|
148
|
+
xml.root.attributes['gd:etag'] = @etag if @etag and @etag != ''
|
149
|
+
else
|
150
|
+
xml.root.delete_attribute('gd:etag')
|
151
|
+
end
|
152
|
+
case ele.name
|
153
|
+
when "title"
|
154
|
+
ele.text = @title
|
155
|
+
end
|
156
|
+
end
|
157
|
+
xml.to_s
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# Author:: Mike Reich (mike@seabourneconsulting.com)
|
2
|
+
# Copyright:: Copyright (C) 2010 Mike Reich
|
3
|
+
# License:: GPL v2
|
4
|
+
#--
|
5
|
+
# Licensed under the General Public License (GPL), Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
#
|
15
|
+
# Feel free to use and update, but be sure to contribute your
|
16
|
+
# code back to the project and attribute as required by the license.
|
17
|
+
#++
|
18
|
+
require "uri"
|
19
|
+
|
20
|
+
module GData4Ruby
|
21
|
+
#The Request class holds all information needed to make a Request to a Google service.
|
22
|
+
class Request
|
23
|
+
#The HTTP request type, must be one of :get, :post, :put, :delete
|
24
|
+
attr_accessor :type
|
25
|
+
|
26
|
+
#The HTTP request content, only valid for :put and :post requests
|
27
|
+
attr_accessor :content
|
28
|
+
|
29
|
+
#Optional. Additional headers to pass with the request.
|
30
|
+
attr_accessor :headers
|
31
|
+
|
32
|
+
#Optional. Additional query parameters (i.e. "?param=value") to append to the request url
|
33
|
+
attr_reader :parameters
|
34
|
+
|
35
|
+
#Creates a new request object.
|
36
|
+
def initialize(type, url, content = nil, headers = nil, query_parameters = nil)
|
37
|
+
@parameters = nil
|
38
|
+
@headers = headers
|
39
|
+
@content = content
|
40
|
+
@type = type
|
41
|
+
@url = URI.parse(url)
|
42
|
+
self.parameters = query_parameters
|
43
|
+
end
|
44
|
+
|
45
|
+
#The HTTP url to send the request to
|
46
|
+
def url=(new_url)
|
47
|
+
@url = new_url
|
48
|
+
end
|
49
|
+
|
50
|
+
#A hash of additional query parameters (i.e. {'param' => 'value') to append to the request url
|
51
|
+
def parameters=(query_parameters)
|
52
|
+
raise ArgumentError, 'Query parameters must be a Hash' if query_parameters and not query_parameters.is_a? Hash
|
53
|
+
@parameters = "?#{query_parameters.to_a.collect{|a| a.join("=")}.join("&")}" if query_parameters
|
54
|
+
end
|
55
|
+
|
56
|
+
#The HTTP url to send the request to
|
57
|
+
def url
|
58
|
+
return URI.parse("#{@url+(@parameters ? @parameters : '')}")
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# Author:: Mike Reich (mike@seabourneconsulting.com)
|
2
|
+
# Copyright:: Copyright (C) 2010 Mike Reich
|
3
|
+
# License:: GPL v2
|
4
|
+
#--
|
5
|
+
# Licensed under the General Public License (GPL), Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
#
|
15
|
+
# Feel free to use and update, but be sure to contribute your
|
16
|
+
# code back to the project and attribute as required by the license.
|
17
|
+
#++
|
18
|
+
require 'gdata4ruby/base'
|
19
|
+
|
20
|
+
module GData4Ruby
|
21
|
+
#The service class is the main handler for all direct interactions with the
|
22
|
+
#Google Data API.
|
23
|
+
|
24
|
+
class Service < Base
|
25
|
+
#Convenience attribute contains the currently authenticated account name
|
26
|
+
attr_reader :account
|
27
|
+
|
28
|
+
# The token returned by the Google servers, used to authorize all subsequent messages
|
29
|
+
attr_reader :auth_token
|
30
|
+
|
31
|
+
#Accepts an optional attributes hash for initialization values, most likely :gdata_version
|
32
|
+
def initialize(attributes = {})
|
33
|
+
super(attributes)
|
34
|
+
attributes.each do |key, value|
|
35
|
+
self.send("#{key}=", value)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# The authenticate method passes the username and password to google servers.
|
40
|
+
# If authentication succeeds, returns true, otherwise raises the AuthenticationFailed error.
|
41
|
+
def authenticate(username, password, service)
|
42
|
+
@auth_token = nil
|
43
|
+
ret = nil
|
44
|
+
ret = send_request(Request.new(:post, AUTH_URL, "Email=#{username}&Passwd=#{password}&source=GCal4Ruby&service=#{service}&accountType=HOSTED_OR_GOOGLE"))
|
45
|
+
if ret.class == Net::HTTPOK
|
46
|
+
@auth_token = ret.read_body.to_a[2].gsub("Auth=", "").strip
|
47
|
+
@account = username
|
48
|
+
@password = password
|
49
|
+
return true
|
50
|
+
else
|
51
|
+
raise AuthenticationFailed
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# Author:: Mike Reich (mike@seabourneconsulting.com)
|
2
|
+
# Copyright:: Copyright (C) 2010 Mike Reich
|
3
|
+
# License:: GPL v2
|
4
|
+
#--
|
5
|
+
# Licensed under the General Public License (GPL), Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
#
|
15
|
+
# Feel free to use and update, but be sure to contribute your
|
16
|
+
# code back to the project and attribute as required by the license.
|
17
|
+
#++
|
18
|
+
module GData4Ruby
|
19
|
+
#A helper class that includes commonly used utility methods.
|
20
|
+
class Utils
|
21
|
+
#Adds common Google namespaces to an element. Useful for processing individual events returned from in a feed.
|
22
|
+
def self.add_namespaces(entry)
|
23
|
+
entry.attributes["xmlns:openSearch"] = "http://a9.com/-/spec/opensearch/1.1/"
|
24
|
+
entry.attributes["xmlns:gAcl"] = "http://schemas.google.com/acl/2007"
|
25
|
+
entry.attributes["xmlns:gCal"] = "http://schemas.google.com/gCal/2005"
|
26
|
+
entry.attributes["xmlns:gd"] = "http://schemas.google.com/g/2005"
|
27
|
+
entry.attributes["xmlns:app"] = "http://www.w3.org/2007/app"
|
28
|
+
entry.attributes["xmlns:docs"] = "http://schemas.google.com/docs/2007"
|
29
|
+
entry.attributes["xmlns"] = "http://www.w3.org/2005/Atom"
|
30
|
+
entry
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/gdata4ruby.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "gdata4ruby/service"
|
data/test/unit.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'gdata4ruby'
|
5
|
+
include GData4Ruby
|
6
|
+
|
7
|
+
@service = Service.new
|
8
|
+
@username = nil
|
9
|
+
@password = nil
|
10
|
+
|
11
|
+
def tester
|
12
|
+
if ARGV.include?("-d")
|
13
|
+
@service.debug = true
|
14
|
+
end
|
15
|
+
ARGV.each do |ar|
|
16
|
+
if ar.match("username=")
|
17
|
+
@username = ar.gsub("username=", "")
|
18
|
+
end
|
19
|
+
if ar.match("password=")
|
20
|
+
@password = ar.gsub("password=", "")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
service_test
|
24
|
+
end
|
25
|
+
|
26
|
+
def service_test
|
27
|
+
puts "---Starting Service Test---"
|
28
|
+
puts "1. Authenticate"
|
29
|
+
if @service.authenticate(@username, @password, 'cl')
|
30
|
+
successful
|
31
|
+
else
|
32
|
+
failed
|
33
|
+
end
|
34
|
+
|
35
|
+
puts "2. Authenticate with GData version 3.0"
|
36
|
+
@service = Service.new({:gdata_version => '3.0'})
|
37
|
+
if @service.authenticate(@username, @password, 'cl') and @service.gdata_version == '3.0'
|
38
|
+
successful
|
39
|
+
else
|
40
|
+
failed
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def failed(m = nil)
|
45
|
+
puts "Test Failed"
|
46
|
+
puts m if m
|
47
|
+
exit()
|
48
|
+
end
|
49
|
+
|
50
|
+
def successful(m = nil)
|
51
|
+
puts "Test Successful"
|
52
|
+
puts m if m
|
53
|
+
end
|
54
|
+
|
55
|
+
tester
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gdata4ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Reich
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-04-28 00:00:00 +10:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A full featured wrapper for interacting with the base Google Data API, including authentication and basic object handling
|
17
|
+
email: mike@seabourneconsulting.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- README
|
26
|
+
- CHANGELOG
|
27
|
+
- lib/gdata4ruby.rb
|
28
|
+
- lib/gdata4ruby/base.rb
|
29
|
+
- lib/gdata4ruby/service.rb
|
30
|
+
- lib/gdata4ruby/request.rb
|
31
|
+
- lib/gdata4ruby/gdata_object.rb
|
32
|
+
- lib/gdata4ruby/utils/utils.rb
|
33
|
+
- lib/gdata4ruby/acl/access_rule.rb
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: http://cookingandcoding.com/gdata4ruby/
|
36
|
+
licenses: []
|
37
|
+
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project: gdata4ruby
|
58
|
+
rubygems_version: 1.3.5
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: A full featured wrapper for interacting with the base Google Data API
|
62
|
+
test_files:
|
63
|
+
- test/unit.rb
|