Class: GoogleDrive::Acl

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Util
Defined in:
lib/google_drive/acl.rb

Overview

ACL (access control list) of a spreadsheet.

Use GoogleDrive::Spreadsheet#acl to get GoogleDrive::Acl object. See GoogleDrive::Spreadsheet#acl for usage example.

This code is based on github.com/guyboertje/gdata-spreadsheet-ruby .

Constant Summary

Constants included from Util

Util::DOCS_BASE_URL, Util::EXT_TO_CONTENT_TYPE

Instance Method Summary collapse

Methods included from Util

concat_url, encode_query, h, to_v3_url

Constructor Details

#initialize(session, acls_feed_url) ⇒ Acl

:nodoc:



22
23
24
25
26
27
28
# File 'lib/google_drive/acl.rb', line 22

def initialize(session, acls_feed_url) #:nodoc:
  @session = session
  @acls_feed_url = acls_feed_url
  header = {"GData-Version" => "3.0"}
  doc = @session.request(:get, @acls_feed_url, :header => header, :auth => :writely)
  @acls = doc.css("entry").map(){ |e| AclEntry.new(entry_to_params(e)) }
end

Instance Method Details

#delete(entry) ⇒ Object

Deletes an ACL entry.

e.g.

spreadsheet.acl.delete(spreadsheet.acl[1])


72
73
74
75
76
# File 'lib/google_drive/acl.rb', line 72

def delete(entry)
  header = {"GData-Version" => "3.0"}
  @session.request(:delete, entry.edit_url, :header => header, :auth => :writely)
  @acls.delete(entry)
end

#inspectObject



103
104
105
# File 'lib/google_drive/acl.rb', line 103

def inspect
  return "\#<%p %p>" % [self.class, @acls]
end

#push(entry) ⇒ Object

Adds a new entry. entry is either a GoogleDrive::AclEntry or a Hash with keys :scope_type, :scope and :role. See GoogleDrive::AclEntry#scope_type and GoogleDrive::AclEntry#role for the document of the fields.

NOTE: This sends email to the new people.

e.g.

spreadsheet.acl.push(
    {:scope_type => "user", :scope => "[email protected]", :role => "reader"})
spreadsheet.acl.push(
    {:scope_type => "user", :scope => "[email protected]", :role => "writer"})


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/google_drive/acl.rb', line 43

def push(entry)
  
  entry = AclEntry.new(entry) if entry.is_a?(Hash)
  
  header = {"GData-Version" => "3.0", "Content-Type" => "application/atom+xml"}
  value_attr = entry.scope ? "value='#{h(entry.scope)}'" : ""
  xml = <<-EOS
    <entry
        xmlns='http://www.w3.org/2005/Atom'
        xmlns:gAcl='http://schemas.google.com/acl/2007'>
      <category scheme='http://schemas.google.com/g/2005#kind'
          term='http://schemas.google.com/acl/2007#accessRule'/>
      <gAcl:role value='#{h(entry.role)}'/>
      <gAcl:scope type='#{h(entry.scope_type)}' #{value_attr}/>
    </entry>
  EOS
  doc = @session.request(
      :post, @acls_feed_url, :data => xml, :header => header, :auth => :writely)
  
  entry.params = entry_to_params(doc.root)
  @acls.push(entry)
  return entry
  
end

#update_role(entry, role) ⇒ Object

:nodoc:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/google_drive/acl.rb', line 78

def update_role(entry, role) #:nodoc:
  
  header = {"GData-Version" => "3.0", "Content-Type" => "application/atom+xml"}
  value_attr = entry.scope ? "value='#{h(entry.scope)}'" : ""
  xml = <<-EOS
    <entry
        xmlns='http://www.w3.org/2005/Atom'
        xmlns:gAcl='http://schemas.google.com/acl/2007'
        xmlns:gd='http://schemas.google.com/g/2005'
        gd:etag='#{h(entry.etag)}'>
      <category
          scheme='http://schemas.google.com/g/2005#kind'
          term='http://schemas.google.com/acl/2007#accessRule'/>
      <gAcl:role value='#{h(role)}'/>
      <gAcl:scope type='#{h(entry.scope_type)}' #{value_attr}/>
    </entry>
  EOS
  doc = @session.request(
      :put, entry.edit_url, :data => xml, :header => header, :auth => :writely)
  
  entry.params = entry_to_params(doc.root)
  return entry
  
end