fix indentation and remove commented out code
fixes: CNVS-12670, CNVS-12677 test plan: - formatting only, no qa needed Change-Id: I2fa467873daf62182587f00f71a47b19dc073232 Reviewed-on: https://gerrit.instructure.com/33867 Tested-by: Jenkins <jenkins@instructure.com> Reviewed-by: Simon Williams <simon@instructure.com> Product-Review: Simon Williams <simon@instructure.com> QA-Review: Simon Williams <simon@instructure.com>
This commit is contained in:
parent
50bead3e73
commit
7722732692
|
@ -19,333 +19,331 @@
|
||||||
# See Google Docs API documentation here:
|
# See Google Docs API documentation here:
|
||||||
# http://code.google.com/apis/documents/docs/2.0/developers_guide_protocol.html
|
# http://code.google.com/apis/documents/docs/2.0/developers_guide_protocol.html
|
||||||
module GoogleDocs
|
module GoogleDocs
|
||||||
class Connection
|
class Connection
|
||||||
def initialize(oauth_gdocs_access_token, oauth_gdocs_access_token_secret)
|
def initialize(oauth_gdocs_access_token, oauth_gdocs_access_token_secret)
|
||||||
@oauth_gdocs_access_token = oauth_gdocs_access_token
|
@oauth_gdocs_access_token = oauth_gdocs_access_token
|
||||||
@oauth_gdocs_access_token_secret = oauth_gdocs_access_token_secret
|
@oauth_gdocs_access_token_secret = oauth_gdocs_access_token_secret
|
||||||
end
|
|
||||||
|
|
||||||
def retrieve_access_token
|
|
||||||
consumer = GoogleDocs::Connection.consumer
|
|
||||||
return nil unless consumer
|
|
||||||
@access_token ||= OAuth::AccessToken.new(consumer, @oauth_gdocs_access_token, @oauth_gdocs_access_token_secret)
|
|
||||||
end
|
|
||||||
|
|
||||||
def get_service_user_info(access_token)
|
|
||||||
doc = create_doc("Temp Doc: #{Time.now.strftime("%d %b %Y, %I:%M %p")}", access_token)
|
|
||||||
delete_doc(doc, access_token)
|
|
||||||
service_user_id = doc.entry.authors[0].email rescue nil
|
|
||||||
service_user_name = doc.entry.authors[0].email rescue nil
|
|
||||||
return service_user_id, service_user_name
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.get_access_token(token, secret, oauth_verifier)
|
|
||||||
consumer = GoogleDocs::Connection.consumer
|
|
||||||
request_token = OAuth::RequestToken.new(consumer,
|
|
||||||
token,
|
|
||||||
secret)
|
|
||||||
request_token.get_access_token(:oauth_verifier => oauth_verifier)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.request_token(oauth_callback)
|
|
||||||
consumer = GoogleDocs::Connection.consumer
|
|
||||||
consumer.get_request_token({:oauth_callback => oauth_callback}, {:scope => "https://docs.google.com/feeds/ https://spreadsheets.google.com/feeds/"})
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
def download(document_id)
|
|
||||||
access_token = retrieve_access_token
|
|
||||||
entry = fetch_list(access_token).entries.map { |e| GoogleDocs::Entry.new(e) }.find { |e| e.document_id == document_id }
|
|
||||||
if entry
|
|
||||||
response = access_token.get(entry.download_url)
|
|
||||||
response = access_token.get(response['Location']) if response.is_a?(Net::HTTPFound)
|
|
||||||
[response, entry.display_name, entry.extension]
|
|
||||||
else
|
|
||||||
[nil, nil, nil]
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def fetch_list(access_token)
|
def retrieve_access_token
|
||||||
response = access_token.get('https://docs.google.com/feeds/documents/private/full')
|
consumer = GoogleDocs::Connection.consumer
|
||||||
Atom::Feed.load_feed(response.body)
|
return nil unless consumer
|
||||||
end
|
@access_token ||= OAuth::AccessToken.new(consumer, @oauth_gdocs_access_token, @oauth_gdocs_access_token_secret)
|
||||||
|
end
|
||||||
|
|
||||||
private :fetch_list
|
def get_service_user_info(access_token)
|
||||||
|
doc = create_doc("Temp Doc: #{Time.now.strftime("%d %b %Y, %I:%M %p")}", access_token)
|
||||||
|
delete_doc(doc, access_token)
|
||||||
|
service_user_id = doc.entry.authors[0].email rescue nil
|
||||||
|
service_user_name = doc.entry.authors[0].email rescue nil
|
||||||
|
return service_user_id, service_user_name
|
||||||
|
end
|
||||||
|
|
||||||
def folderize_list(docs)
|
def self.get_access_token(token, secret, oauth_verifier)
|
||||||
root = Folder.new('/')
|
consumer = GoogleDocs::Connection.consumer
|
||||||
folders = {nil => root}
|
request_token = OAuth::RequestToken.new(consumer,
|
||||||
|
token,
|
||||||
|
secret)
|
||||||
|
request_token.get_access_token(:oauth_verifier => oauth_verifier)
|
||||||
|
end
|
||||||
|
|
||||||
docs.entries.each do |entry|
|
def self.request_token(oauth_callback)
|
||||||
entry = GoogleDocs::Entry.new(entry)
|
consumer = GoogleDocs::Connection.consumer
|
||||||
if !folders.has_key?(entry.folder)
|
consumer.get_request_token({:oauth_callback => oauth_callback}, {:scope => "https://docs.google.com/feeds/ https://spreadsheets.google.com/feeds/"})
|
||||||
folder = Folder.new(entry.folder)
|
end
|
||||||
root.add_folder folder
|
|
||||||
folders[entry.folder] = folder
|
|
||||||
|
def download(document_id)
|
||||||
|
access_token = retrieve_access_token
|
||||||
|
entry = fetch_list(access_token).entries.map { |e| GoogleDocs::Entry.new(e) }.find { |e| e.document_id == document_id }
|
||||||
|
if entry
|
||||||
|
response = access_token.get(entry.download_url)
|
||||||
|
response = access_token.get(response['Location']) if response.is_a?(Net::HTTPFound)
|
||||||
|
[response, entry.display_name, entry.extension]
|
||||||
else
|
else
|
||||||
folder = folders[entry.folder]
|
[nil, nil, nil]
|
||||||
end
|
end
|
||||||
folder.add_file entry
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return root
|
def fetch_list(access_token)
|
||||||
end
|
response = access_token.get('https://docs.google.com/feeds/documents/private/full')
|
||||||
|
Atom::Feed.load_feed(response.body)
|
||||||
private :folderize_list
|
|
||||||
|
|
||||||
def list(access_token=nil)
|
|
||||||
access_token ||= retrieve_access_token
|
|
||||||
folderize_list(fetch_list(access_token))
|
|
||||||
end
|
|
||||||
|
|
||||||
private :list
|
|
||||||
|
|
||||||
def list_with_extension_filter(extensions, access_token=nil)
|
|
||||||
access_token ||= retrieve_access_token
|
|
||||||
docs = list(access_token)
|
|
||||||
if extensions && extensions.length > 0
|
|
||||||
docs = docs.select { |e| extensions.include?(e.extension) }
|
|
||||||
end
|
|
||||||
docs
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.consumer(key = nil, secret = nil)
|
|
||||||
if key.nil? || secret.nil?
|
|
||||||
return nil if GoogleDocs::Connection.config.nil?
|
|
||||||
key ||= GoogleDocs::Connection.config['api_key']
|
|
||||||
secret ||= GoogleDocs::Connection.config['secret_key']
|
|
||||||
end
|
end
|
||||||
|
|
||||||
require 'oauth'
|
private :fetch_list
|
||||||
require 'oauth/consumer'
|
|
||||||
|
|
||||||
OAuth::Consumer.new(key, secret, {
|
def folderize_list(docs)
|
||||||
:site => 'https://www.google.com',
|
root = Folder.new('/')
|
||||||
:request_token_path => '/accounts/OAuthGetRequestToken',
|
folders = {nil => root}
|
||||||
:access_token_path => '/accounts/OAuthGetAccessToken',
|
|
||||||
:authorize_path => '/accounts/OAuthAuthorizeToken',
|
|
||||||
:signature_method => 'HMAC-SHA1'
|
|
||||||
})
|
|
||||||
end
|
|
||||||
|
|
||||||
class Google
|
docs.entries.each do |entry|
|
||||||
class Google::Batch
|
entry = GoogleDocs::Entry.new(entry)
|
||||||
class Google::Batch::Operation
|
if !folders.has_key?(entry.folder)
|
||||||
attr_accessor :type
|
folder = Folder.new(entry.folder)
|
||||||
|
root.add_folder folder
|
||||||
|
folders[entry.folder] = folder
|
||||||
|
else
|
||||||
|
folder = folders[entry.folder]
|
||||||
|
end
|
||||||
|
folder.add_file entry
|
||||||
|
end
|
||||||
|
|
||||||
def initialize(operation_type="insert")
|
return root
|
||||||
self.type = operation_type
|
end
|
||||||
|
|
||||||
|
private :folderize_list
|
||||||
|
|
||||||
|
def list(access_token=nil)
|
||||||
|
access_token ||= retrieve_access_token
|
||||||
|
folderize_list(fetch_list(access_token))
|
||||||
|
end
|
||||||
|
|
||||||
|
private :list
|
||||||
|
|
||||||
|
def list_with_extension_filter(extensions, access_token=nil)
|
||||||
|
access_token ||= retrieve_access_token
|
||||||
|
docs = list(access_token)
|
||||||
|
if extensions && extensions.length > 0
|
||||||
|
docs = docs.select { |e| extensions.include?(e.extension) }
|
||||||
|
end
|
||||||
|
docs
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.consumer(key = nil, secret = nil)
|
||||||
|
if key.nil? || secret.nil?
|
||||||
|
return nil if GoogleDocs::Connection.config.nil?
|
||||||
|
key ||= GoogleDocs::Connection.config['api_key']
|
||||||
|
secret ||= GoogleDocs::Connection.config['secret_key']
|
||||||
|
end
|
||||||
|
|
||||||
|
require 'oauth'
|
||||||
|
require 'oauth/consumer'
|
||||||
|
|
||||||
|
OAuth::Consumer.new(key, secret, {
|
||||||
|
:site => 'https://www.google.com',
|
||||||
|
:request_token_path => '/accounts/OAuthGetRequestToken',
|
||||||
|
:access_token_path => '/accounts/OAuthGetAccessToken',
|
||||||
|
:authorize_path => '/accounts/OAuthAuthorizeToken',
|
||||||
|
:signature_method => 'HMAC-SHA1'
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
class Google
|
||||||
|
class Google::Batch
|
||||||
|
class Google::Batch::Operation
|
||||||
|
attr_accessor :type
|
||||||
|
|
||||||
|
def initialize(operation_type="insert")
|
||||||
|
self.type = operation_type
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_xml(*opts)
|
||||||
|
n = XML::Node.new("batch:operation")
|
||||||
|
n['type'] = type
|
||||||
|
n
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Google::GAcl
|
||||||
|
class Google::GAcl::Role
|
||||||
|
attr_accessor :role
|
||||||
|
|
||||||
|
def initialize()
|
||||||
|
self.role = "writer"
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_xml(*opts)
|
||||||
|
n = XML::Node.new("gAcl:role")
|
||||||
|
n['value'] = role
|
||||||
|
n
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_xml(*opts)
|
class Google::GAcl::Scope
|
||||||
n = XML::Node.new("batch:operation")
|
attr_accessor :type, :value
|
||||||
n['type'] = type
|
|
||||||
n
|
def initialize(email)
|
||||||
|
self.type = "user"
|
||||||
|
self.value = email
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_xml(*opts)
|
||||||
|
n = XML::Node.new("gAcl:scope")
|
||||||
|
n['type'] = type
|
||||||
|
n['value'] = value
|
||||||
|
n
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Google::GAcl
|
class Entry < Atom::Entry
|
||||||
class Google::GAcl::Role
|
namespace Atom::NAMESPACE
|
||||||
attr_accessor :role
|
element "id"
|
||||||
|
element "batch:id"
|
||||||
|
element "batch:operation", :class => Google::Batch::Operation
|
||||||
|
element "gAcl:role", :class => Google::GAcl::Role
|
||||||
|
element "gAcl:scope", :class => Google::GAcl::Scope
|
||||||
|
elements :categories
|
||||||
|
add_extension_namespace :batch, 'http://schemas.google.com/gdata/batch'
|
||||||
|
add_extension_namespace :gAcl, 'http://schemas.google.com/acl/2007'
|
||||||
|
end
|
||||||
|
class Feed < Atom::Feed
|
||||||
|
namespace Atom::NAMESPACE
|
||||||
|
elements :entries
|
||||||
|
elements :categories
|
||||||
|
|
||||||
def initialize()
|
add_extension_namespace :batch, 'http://schemas.google.com/gdata/batch'
|
||||||
self.role = "writer"
|
add_extension_namespace :gAcl, 'http://schemas.google.com/acl/2007'
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_doc(name, access_token)
|
||||||
|
url = "https://docs.google.com/feeds/documents/private/full"
|
||||||
|
entry = Atom::Entry.new do |entry|
|
||||||
|
entry.title = name
|
||||||
|
entry.categories << Atom::Category.new do |category|
|
||||||
|
category.scheme = "http://schemas.google.com/g/2005#kind"
|
||||||
|
category.term = "http://schemas.google.com/docs/2007#document"
|
||||||
|
category.label = "document"
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
xml = entry.to_xml
|
||||||
|
begin
|
||||||
|
response = access_token.post(url, xml, {'Content-Type' => 'application/atom+xml'})
|
||||||
|
rescue => e
|
||||||
|
raise "Unable to post to Google API #{url}:\n#{xml}" +
|
||||||
|
"\n\n(" + e.to_s + ")\n"
|
||||||
|
end
|
||||||
|
begin
|
||||||
|
GoogleDocs::Entry.new(Atom::Entry.load_entry(response.body))
|
||||||
|
rescue => e
|
||||||
|
raise "Unable to load GoogleDocEntry from response: \n" + response.body +
|
||||||
|
"\n\n(" + e.to_s + ")\n"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def to_xml(*opts)
|
def delete_doc(entry, access_token = retrieve_access_token)
|
||||||
n = XML::Node.new("gAcl:role")
|
access_token.delete(entry.edit_url, {'GData-Version' => '2', 'If-Match' => '*'})
|
||||||
n['value'] = role
|
end
|
||||||
n
|
|
||||||
|
def acl_remove(document_id, users)
|
||||||
|
access_token = retrieve_access_token
|
||||||
|
url = "https://docs.google.com/feeds/acl/private/full/#{document_id}/batch"
|
||||||
|
|
||||||
|
Struct.new('UserStruct', :id, :gmail, :google_docs_address)
|
||||||
|
users.each_with_index do |user, idx|
|
||||||
|
if user.is_a? String
|
||||||
|
users[idx] = Struct::UserStruct.new(user, user)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Google::GAcl::Scope
|
request_feed = Feed.new do |feed|
|
||||||
attr_accessor :type, :value
|
feed.categories << Atom::Category.new { |category|
|
||||||
|
category.scheme = "http://schemas.google.com/g/2005#kind"
|
||||||
|
category.term = "http://schemas.google.com/acl/2007#accessRule"
|
||||||
|
}
|
||||||
|
users.each do |user|
|
||||||
|
next unless user_identifier = user.google_docs_address || user.gmail
|
||||||
|
|
||||||
def initialize(email)
|
feed.entries << Entry.new do |entry|
|
||||||
self.type = "user"
|
entry.id = "https://docs.google.com/feeds/acl/private/full/#{CGI.escape(document_id)}/user%3A#{CGI.escape(user_identifier)}"
|
||||||
self.value = email
|
entry.batch_operation = Google::Batch::Operation.new('delete')
|
||||||
end
|
entry.gAcl_role = Google::GAcl::Role.new
|
||||||
|
entry.gAcl_scope = Google::GAcl::Scope.new(user_identifier)
|
||||||
def to_xml(*opts)
|
end
|
||||||
n = XML::Node.new("gAcl:scope")
|
|
||||||
n['type'] = type
|
|
||||||
n['value'] = value
|
|
||||||
n
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
response = access_token.post(url, request_feed.to_xml, {'Content-Type' => 'application/atom+xml'})
|
||||||
end
|
feed = Atom::Feed.load_feed(response.body)
|
||||||
|
res = []
|
||||||
|
|
||||||
class Entry < Atom::Entry
|
feed.entries.each do |entry|
|
||||||
namespace Atom::NAMESPACE
|
user = users.to_a.find { |u| u.id == entry['http://schemas.google.com/gdata/batch', 'id'][0].to_i }
|
||||||
element "id"
|
res << user if user
|
||||||
element "batch:id"
|
|
||||||
element "batch:operation", :class => Google::Batch::Operation
|
|
||||||
element "gAcl:role", :class => Google::GAcl::Role
|
|
||||||
element "gAcl:scope", :class => Google::GAcl::Scope
|
|
||||||
elements :categories
|
|
||||||
add_extension_namespace :batch, 'http://schemas.google.com/gdata/batch'
|
|
||||||
add_extension_namespace :gAcl, 'http://schemas.google.com/acl/2007'
|
|
||||||
end
|
|
||||||
class Feed < Atom::Feed
|
|
||||||
namespace Atom::NAMESPACE
|
|
||||||
elements :entries
|
|
||||||
elements :categories
|
|
||||||
|
|
||||||
add_extension_namespace :batch, 'http://schemas.google.com/gdata/batch'
|
|
||||||
add_extension_namespace :gAcl, 'http://schemas.google.com/acl/2007'
|
|
||||||
end
|
|
||||||
|
|
||||||
def create_doc(name, access_token)
|
|
||||||
url = "https://docs.google.com/feeds/documents/private/full"
|
|
||||||
entry = Atom::Entry.new do |entry|
|
|
||||||
entry.title = name
|
|
||||||
entry.categories << Atom::Category.new do |category|
|
|
||||||
category.scheme = "http://schemas.google.com/g/2005#kind"
|
|
||||||
category.term = "http://schemas.google.com/docs/2007#document"
|
|
||||||
category.label = "document"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
xml = entry.to_xml
|
|
||||||
begin
|
|
||||||
response = access_token.post(url, xml, {'Content-Type' => 'application/atom+xml'})
|
|
||||||
rescue => e
|
|
||||||
raise "Unable to post to Google API #{url}:\n#{xml}" +
|
|
||||||
"\n\n(" + e.to_s + ")\n"
|
|
||||||
end
|
|
||||||
begin
|
|
||||||
GoogleDocs::Entry.new(Atom::Entry.load_entry(response.body))
|
|
||||||
rescue => e
|
|
||||||
raise "Unable to load GoogleDocEntry from response: \n" + response.body +
|
|
||||||
"\n\n(" + e.to_s + ")\n"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def delete_doc(entry, access_token = retrieve_access_token)
|
|
||||||
access_token.delete(entry.edit_url, {'GData-Version' => '2', 'If-Match' => '*'})
|
|
||||||
end
|
|
||||||
|
|
||||||
def acl_remove(document_id, users)
|
|
||||||
access_token = retrieve_access_token
|
|
||||||
url = "https://docs.google.com/feeds/acl/private/full/#{document_id}/batch"
|
|
||||||
|
|
||||||
Struct.new('UserStruct', :id, :gmail, :google_docs_address)
|
|
||||||
users.each_with_index do |user, idx|
|
|
||||||
if user.is_a? String
|
|
||||||
users[idx] = Struct::UserStruct.new(user, user)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
request_feed = Feed.new do |feed|
|
|
||||||
feed.categories << Atom::Category.new { |category|
|
|
||||||
category.scheme = "http://schemas.google.com/g/2005#kind"
|
|
||||||
category.term = "http://schemas.google.com/acl/2007#accessRule"
|
|
||||||
}
|
|
||||||
users.each do |user|
|
|
||||||
next unless user_identifier = user.google_docs_address || user.gmail
|
|
||||||
|
|
||||||
feed.entries << Entry.new do |entry|
|
|
||||||
entry.id = "https://docs.google.com/feeds/acl/private/full/#{CGI.escape(document_id)}/user%3A#{CGI.escape(user_identifier)}"
|
|
||||||
entry.batch_operation = Google::Batch::Operation.new('delete')
|
|
||||||
entry.gAcl_role = Google::GAcl::Role.new
|
|
||||||
entry.gAcl_scope = Google::GAcl::Scope.new(user_identifier)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
response = access_token.post(url, request_feed.to_xml, {'Content-Type' => 'application/atom+xml'})
|
|
||||||
feed = Atom::Feed.load_feed(response.body)
|
|
||||||
res = []
|
|
||||||
|
|
||||||
feed.entries.each do |entry|
|
|
||||||
user = users.to_a.find { |u| u.id == entry['http://schemas.google.com/gdata/batch', 'id'][0].to_i }
|
|
||||||
res << user if user
|
|
||||||
end
|
|
||||||
|
|
||||||
res
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
# Public: Add users to a Google Doc ACL list.
|
|
||||||
#
|
|
||||||
# document_id - The id of the Google Doc to add users to.
|
|
||||||
# users - An array of user objects.
|
|
||||||
# domain - The string domain to restrict additions to (e.g. "example.com").
|
|
||||||
# Accounts not on this domain will be ignored.
|
|
||||||
#
|
|
||||||
# Returns nothing.
|
|
||||||
def acl_add(document_id, users, domain = nil)
|
|
||||||
access_token = retrieve_access_token
|
|
||||||
url = "https://docs.google.com/feeds/acl/private/full/#{document_id}/batch"
|
|
||||||
domain_regex = domain ? %r{@#{domain}$} : /./
|
|
||||||
allowed_users = []
|
|
||||||
|
|
||||||
user_added = false
|
|
||||||
request_feed = Feed.new do |feed|
|
|
||||||
feed.categories << Atom::Category.new do |category|
|
|
||||||
category.scheme = "http://schemas.google.com/g/2005#kind"
|
|
||||||
category.term = "http://schemas.google.com/acl/2007#accessRule"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
allowed_users = users.select do |user|
|
res
|
||||||
address = user.google_docs_address || user.gmail
|
|
||||||
address ? address.match(domain_regex) : nil
|
|
||||||
end
|
|
||||||
|
|
||||||
allowed_users.each do |user|
|
|
||||||
user_added = true
|
|
||||||
feed.entries << user_feed_entry(user)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return unless user_added
|
|
||||||
|
|
||||||
response = access_token.post(url, request_feed.to_xml, {'Content-Type' => 'application/atom+xml'})
|
# Public: Add users to a Google Doc ACL list.
|
||||||
feed = Atom::Feed.load_feed(response.body)
|
|
||||||
feed.entries.inject([]) do |response, entry|
|
|
||||||
user = allowed_users.find do |u|
|
|
||||||
u.id == entry['http://schemas.google.com/gdata/batch', 'id'][0].to_i
|
|
||||||
end
|
|
||||||
response << user if user
|
|
||||||
response
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def user_feed_entry(user)
|
|
||||||
Entry.new do |entry|
|
|
||||||
entry.batch_id = user.id
|
|
||||||
entry.batch_operation = Google::Batch::Operation.new
|
|
||||||
entry.gAcl_role = Google::GAcl::Role.new
|
|
||||||
entry.gAcl_scope = Google::GAcl::Scope.new(user.google_docs_address || user.gmail)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
private :user_feed_entry
|
|
||||||
|
|
||||||
def verify_access_token
|
|
||||||
access_token = retrieve_access_token
|
|
||||||
access_token.head("https://www.google.com/accounts/AuthSubTokenInfo").is_a? Net::HTTPSuccess
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.config_check(settings)
|
|
||||||
consumer = GoogleDocs::Connection.consumer(settings[:api_key], settings[:secret_key])
|
|
||||||
token = consumer.get_request_token({}, {:scope => "https://docs.google.com/feeds/"}) rescue nil
|
|
||||||
token ? nil : "Configuration check failed, please check your settings"
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.config=(config)
|
|
||||||
if !config.is_a?(Proc)
|
|
||||||
raise "Config must be a Proc"
|
|
||||||
end
|
|
||||||
@config = config
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.config
|
|
||||||
@config.call()
|
|
||||||
#
|
#
|
||||||
#Canvas::Plugin.find(:google_docs).try(:settings) || Setting.from_config('google_docs')
|
# document_id - The id of the Google Doc to add users to.
|
||||||
|
# users - An array of user objects.
|
||||||
|
# domain - The string domain to restrict additions to (e.g. "example.com").
|
||||||
|
# Accounts not on this domain will be ignored.
|
||||||
|
#
|
||||||
|
# Returns nothing.
|
||||||
|
def acl_add(document_id, users, domain = nil)
|
||||||
|
access_token = retrieve_access_token
|
||||||
|
url = "https://docs.google.com/feeds/acl/private/full/#{document_id}/batch"
|
||||||
|
domain_regex = domain ? %r{@#{domain}$} : /./
|
||||||
|
allowed_users = []
|
||||||
|
|
||||||
|
user_added = false
|
||||||
|
request_feed = Feed.new do |feed|
|
||||||
|
feed.categories << Atom::Category.new do |category|
|
||||||
|
category.scheme = "http://schemas.google.com/g/2005#kind"
|
||||||
|
category.term = "http://schemas.google.com/acl/2007#accessRule"
|
||||||
|
end
|
||||||
|
|
||||||
|
allowed_users = users.select do |user|
|
||||||
|
address = user.google_docs_address || user.gmail
|
||||||
|
address ? address.match(domain_regex) : nil
|
||||||
|
end
|
||||||
|
|
||||||
|
allowed_users.each do |user|
|
||||||
|
user_added = true
|
||||||
|
feed.entries << user_feed_entry(user)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return unless user_added
|
||||||
|
|
||||||
|
response = access_token.post(url, request_feed.to_xml, {'Content-Type' => 'application/atom+xml'})
|
||||||
|
feed = Atom::Feed.load_feed(response.body)
|
||||||
|
feed.entries.inject([]) do |response, entry|
|
||||||
|
user = allowed_users.find do |u|
|
||||||
|
u.id == entry['http://schemas.google.com/gdata/batch', 'id'][0].to_i
|
||||||
|
end
|
||||||
|
response << user if user
|
||||||
|
response
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def user_feed_entry(user)
|
||||||
|
Entry.new do |entry|
|
||||||
|
entry.batch_id = user.id
|
||||||
|
entry.batch_operation = Google::Batch::Operation.new
|
||||||
|
entry.gAcl_role = Google::GAcl::Role.new
|
||||||
|
entry.gAcl_scope = Google::GAcl::Scope.new(user.google_docs_address || user.gmail)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private :user_feed_entry
|
||||||
|
|
||||||
|
def verify_access_token
|
||||||
|
access_token = retrieve_access_token
|
||||||
|
access_token.head("https://www.google.com/accounts/AuthSubTokenInfo").is_a? Net::HTTPSuccess
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.config_check(settings)
|
||||||
|
consumer = GoogleDocs::Connection.consumer(settings[:api_key], settings[:secret_key])
|
||||||
|
token = consumer.get_request_token({}, {:scope => "https://docs.google.com/feeds/"}) rescue nil
|
||||||
|
token ? nil : "Configuration check failed, please check your settings"
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.config=(config)
|
||||||
|
if !config.is_a?(Proc)
|
||||||
|
raise "Config must be a Proc"
|
||||||
|
end
|
||||||
|
@config = config
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.config
|
||||||
|
@config.call()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
|
|
|
@ -16,90 +16,90 @@
|
||||||
# with this program. If not, see <http://www.gnu.org/licenses/>.
|
# with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
module GoogleDocs
|
module GoogleDocs
|
||||||
class Entry
|
class Entry
|
||||||
def self.extension_looker_upper
|
def self.extension_looker_upper
|
||||||
@extension_looker_upper
|
@extension_looker_upper
|
||||||
end
|
|
||||||
|
|
||||||
def self.extension_looker_upper=(extension_looker_upper)
|
|
||||||
@extension_looker_upper=extension_looker_upper
|
|
||||||
end
|
|
||||||
|
|
||||||
attr_reader :document_id, :folder, :entry
|
|
||||||
|
|
||||||
def initialize(entry)
|
|
||||||
if entry.is_a?(String)
|
|
||||||
@entry = Atom::Entry.load_entry(entry)
|
|
||||||
else
|
|
||||||
@entry = entry
|
|
||||||
end
|
end
|
||||||
set_document_id_from @entry
|
|
||||||
@folder = @entry.categories.find { |c| c.scheme.match(/\Ahttp:\/\/schemas.google.com\/docs\/2007\/folders/) }.label rescue nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def alternate_url
|
def self.extension_looker_upper=(extension_looker_upper)
|
||||||
link = @entry.links.find { |link| link.rel == "alternate" && link.type == "text/html" }
|
@extension_looker_upper=extension_looker_upper
|
||||||
link || "http://docs.google.com"
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def edit_url
|
attr_reader :document_id, :folder, :entry
|
||||||
"https://docs.google.com/feeds/documents/private/full/#{@document_id}"
|
|
||||||
end
|
|
||||||
|
|
||||||
def content_type
|
def initialize(entry)
|
||||||
@entry.content && @entry.content.type
|
if entry.is_a?(String)
|
||||||
end
|
@entry = Atom::Entry.load_entry(entry)
|
||||||
|
else
|
||||||
def extension
|
@entry = entry
|
||||||
if @extension.nil?
|
|
||||||
# first, try and chose and extension by content-types we can scribd
|
|
||||||
if !content_type.nil? && !content_type.strip.empty? && self.class.extension_looker_upper && mimetype = self.class.extension_looker_upper.find_by_name(content_type)
|
|
||||||
@extension = mimetype.extension
|
|
||||||
end
|
end
|
||||||
# second, look at the document id itself for any clues
|
set_document_id_from @entry
|
||||||
if !@document_id.nil? && !@document_id.strip.empty?
|
@folder = @entry.categories.find { |c| c.scheme.match(/\Ahttp:\/\/schemas.google.com\/docs\/2007\/folders/) }.label rescue nil
|
||||||
@extension ||= case @document_id
|
end
|
||||||
when /\Aspreadsheet/ then
|
|
||||||
"xls"
|
def alternate_url
|
||||||
when /\Apresentation/ then
|
link = @entry.links.find { |link| link.rel == "alternate" && link.type == "text/html" }
|
||||||
"ppt"
|
link || "http://docs.google.com"
|
||||||
when /\Adocument/ then
|
end
|
||||||
"doc"
|
|
||||||
end
|
def edit_url
|
||||||
|
"https://docs.google.com/feeds/documents/private/full/#{@document_id}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def content_type
|
||||||
|
@entry.content && @entry.content.type
|
||||||
|
end
|
||||||
|
|
||||||
|
def extension
|
||||||
|
if @extension.nil?
|
||||||
|
# first, try and chose and extension by content-types we can scribd
|
||||||
|
if !content_type.nil? && !content_type.strip.empty? && self.class.extension_looker_upper && mimetype = self.class.extension_looker_upper.find_by_name(content_type)
|
||||||
|
@extension = mimetype.extension
|
||||||
|
end
|
||||||
|
# second, look at the document id itself for any clues
|
||||||
|
if !@document_id.nil? && !@document_id.strip.empty?
|
||||||
|
@extension ||= case @document_id
|
||||||
|
when /\Aspreadsheet/ then
|
||||||
|
"xls"
|
||||||
|
when /\Apresentation/ then
|
||||||
|
"ppt"
|
||||||
|
when /\Adocument/ then
|
||||||
|
"doc"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
# finally, just declare it unknown
|
||||||
|
@extension ||= "unknown"
|
||||||
end
|
end
|
||||||
# finally, just declare it unknown
|
@extension == "unknown" ? nil : @extension
|
||||||
@extension ||= "unknown"
|
|
||||||
end
|
end
|
||||||
@extension == "unknown" ? nil : @extension
|
|
||||||
end
|
|
||||||
|
|
||||||
def display_name
|
def display_name
|
||||||
@entry.title || "google_doc.#{extension}"
|
@entry.title || "google_doc.#{extension}"
|
||||||
end
|
|
||||||
|
|
||||||
def download_url
|
|
||||||
url = @entry.content.src
|
|
||||||
if url && (parsed_url = (URI.parse(url) rescue nil)) && (ext = extension)
|
|
||||||
parsed_url.query = [parsed_url.query, "exportFormat=#{ext}", "format=#{ext}"].compact.join("&")
|
|
||||||
url = parsed_url.to_s
|
|
||||||
end
|
end
|
||||||
url
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_hash
|
def download_url
|
||||||
{
|
url = @entry.content.src
|
||||||
"name" => display_name,
|
if url && (parsed_url = (URI.parse(url) rescue nil)) && (ext = extension)
|
||||||
"document_id" => @document_id,
|
parsed_url.query = [parsed_url.query, "exportFormat=#{ext}", "format=#{ext}"].compact.join("&")
|
||||||
"extension" => extension,
|
url = parsed_url.to_s
|
||||||
"alternate_url" => alternate_url
|
end
|
||||||
}
|
url
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
def to_hash
|
||||||
|
{
|
||||||
|
"name" => display_name,
|
||||||
|
"document_id" => @document_id,
|
||||||
|
"extension" => extension,
|
||||||
|
"alternate_url" => alternate_url
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
def set_document_id_from(entry)
|
private
|
||||||
doc_id = entry.simple_extensions["{http://schemas.google.com/g/2005,resourceId}"]
|
|
||||||
@document_id = doc_id.first.to_s
|
def set_document_id_from(entry)
|
||||||
|
doc_id = entry.simple_extensions["{http://schemas.google.com/g/2005,resourceId}"]
|
||||||
|
@document_id = doc_id.first.to_s
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
|
@ -47,8 +47,6 @@ describe GoogleDocs::Connection do
|
||||||
GoogleDocs::Connection.config = Proc.new do
|
GoogleDocs::Connection.config = Proc.new do
|
||||||
config
|
config
|
||||||
end
|
end
|
||||||
#Setting.stubs(:from_config).returns(config)
|
|
||||||
#Canvas::Plugin.stubs(:find).returns(nil)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#retrieve_access_token" do
|
describe "#retrieve_access_token" do
|
||||||
|
|
Loading…
Reference in New Issue