Class: GData4Ruby::ACL::AccessRule

Inherits:
GDataObject show all
Defined in:
lib/gdata4ruby/acl/access_rule.rb

Overview

Represents an individual access rule entry in a Google ACL feed.

Constant Summary collapse

XML =
"<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=''/>
  <gAcl:scope type='user' value=''/>
</entry>"

Instance Attribute Summary collapse

Attributes inherited from GDataObject

#acl_uri, #author_email, #author_name, #categories, #content_uri, #edit_uri, #etag, #feed_links, #feed_uri, #id, #kind, #parent_uri, #published, #service, #title, #updated

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GDataObject

#exists?, #save

Constructor Details

#initialize(service, parent, attributes = {}) ⇒ AccessRule

Creates a new AccessRule object. You must pass a valid Service and GDataObject, and can pass an optional hash of attributes to initialize the object with.

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
# File 'lib/gdata4ruby/acl/access_rule.rb', line 45

def initialize(service, parent, attributes = {})
  super(service, attributes)
  @xml = XML
  raise ArgumentError, 'parent must be a GData4Ruby::GDataObject' if not parent.is_a? GData4Ruby::GDataObject
  @parent = parent
  @role = @user = nil
end

Instance Attribute Details

#parentObject

The parent GDataObject the rule applies to



41
42
43
# File 'lib/gdata4ruby/acl/access_rule.rb', line 41

def parent
  @parent
end

#roleObject

The user’s role



38
39
40
# File 'lib/gdata4ruby/acl/access_rule.rb', line 38

def role
  @role
end

#userObject

The Rule’s user



35
36
37
# File 'lib/gdata4ruby/acl/access_rule.rb', line 35

def user
  @user
end

Class Method Details

.find(service, parent, args = {}) ⇒ Object

Finds an AccessRule based on the args passed.

Args can be a hash containing either:

user

an email address/user id to search for. If found, returns the matching AccessRule object.

role

the role to search for. Returns an array of matching AccessRules, or an empty array if no matches are found.

Raises:

  • (ArgumentError)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gdata4ruby/acl/access_rule.rb', line 92

def self.find(service, parent, args = {})
  raise ArgumentError, 'Must supply a username or role to find by' if not args[:user] and not args[:role]
  rules = []
  ret = service.send_request(GData4Ruby::Request.new(:get, parent.acl_uri))
  xml = REXML::Document.new(ret).root
  xml.elements.each("entry") do |e|
    e = GData4Ruby::Utils::add_namespaces(e)
    rule = AccessRule.new(service, parent)
    rule.load(e.to_s)
    return rule if args[:user] and rule.user == args[:user]
    rules << rule if args[:role] and rule.role == args[:role]
  end
  return args[:user] ? false : rules
end

Instance Method Details

#createObject

Creates the AccessRule using the parent’s acl_uri attribute.



54
55
56
57
58
59
60
# File 'lib/gdata4ruby/acl/access_rule.rb', line 54

def create
  ret = service.send_request(Request.new(:post, @parent.acl_uri, to_xml))
  if not ret or not load(ret)
    raise SaveFailed, 'Could not create access rule'
  end
  return ret
end

#deleteObject

Deletes the AccessRule



78
79
80
81
82
83
84
85
# File 'lib/gdata4ruby/acl/access_rule.rb', line 78

def delete
  if @exists
    @role = 'none'
    service.send_request(Request.new(:put, @edit_uri, self.to_xml, {"If-Match" => "*", 'Content-Type' => 'application/atom+xml'}))
  end
  @exists = false
  return true
end

#load(string) ⇒ Object

Loads data into the object. Accepts a string containing an XML <entry> object.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gdata4ruby/acl/access_rule.rb', line 63

def load(string)
  super(string)
  @folders = []
  xml = REXML::Document.new(string)
  xml.root.elements.each(){}.map do |ele|
    case ele.name
      when 'role'
        @role = ele.attributes['value']
      when 'scope'
        @user = ele.attributes['value'] ? ele.attributes['value'] : ele.attributes['type']
    end
  end
end

#to_xmlObject

Returns a string containing the XML representation of the AccessRule



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/gdata4ruby/acl/access_rule.rb', line 108

def to_xml
  xml = REXML::Document.new(super)
  xml.root.elements.each(){}.map do |ele|
    case ele.name
    when "role"
      ele.attributes['value'] = @role           
    when 'scope'
      if @user and @user != 'default'
        ele.attributes['value'] = @user 
      else
        ele.attributes['type'] = 'default'
        ele.delete_attribute("value")
      end
    end
  end
  xml.to_s
end