Class: GProv::Provision::Group

Inherits:
EntryBase show all
Defined in:
lib/gprov/provision/group.rb

Instance Attribute Summary

Attributes inherited from EntryBase

#connection, #status

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from EntryBase

#attributes_from_hash, #initialize, #xml_to_hash

Methods included from EntryBase::ClassMethods

#attribute_names, #attribute_titles, #attributes, #xmlattr

Constructor Details

This class inherits a constructor from GProv::Provision::EntryBase

Class Method Details

.all(connection, options = {}) ⇒ Object

Retrieves all users within a domain



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/gprov/provision/group.rb', line 32

def self.all(connection, options={})

  # TODO Fail if unrecognized options passed
  url = "/group/2.0/:domain"
  if member = options[:member]
    url << "/?member=#{member}"

    if direct_only = options[:direct_only]
      url << "&directOnly=#{direct_only}"
    end
  end

  feed = GProv::Provision::Feed.new(connection, url, "/xmlns:feed/xmlns:entry")
  entries = feed.fetch
  entries.map { |xml| new(:status => :clean, :connection => connection, :source => xml) }
end

.get(connection, group_id) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/gprov/provision/group.rb', line 49

def self.get(connection, group_id)
  response = connection.get("/group/2.0/:domain/#{group_id}")
  document = Nokogiri::XML(response.body)
  xml = document.root

  new(:status => :clean, :connection => connection, :source => xml)
end

Instance Method Details

#add_member(membername) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/gprov/provision/group.rb', line 94

def add_member(membername)
  member = GProv::Provision::Member.new(
    :connection => @connection,
    :source => {
      :group_id => @group_id,
      :member_id => membername,
    }
  )
  member.create!
end

#add_owner(ownername) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/gprov/provision/group.rb', line 120

def add_owner(ownername)
  owner = GProv::Provision::Owner.new(
    :connection => @connection,
    :source => {
      :group_id => @group_id,
      :email => ownername,
    }
  )
  owner.create!
end

#create!Object



79
80
81
82
# File 'lib/gprov/provision/group.rb', line 79

def create!
  response = connection.post("/group/2.0/:domain", {:body => to_nokogiri.to_xml})
  status = :clean
end

#del_member(membername) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/gprov/provision/group.rb', line 105

def del_member(membername)
  member = GProv::Provision::Member.new(
    :connection => @connection,
    :source => {
      :group_id => @group_id,
      :member_id => membername,
    }
  )
  member.delete!
end

#del_owner(ownername) ⇒ Object



131
132
133
134
135
136
137
138
139
140
# File 'lib/gprov/provision/group.rb', line 131

def del_owner(ownername)
  owner = GProv::Provision::Owner.new(
    :connection => @connection,
    :source => {
      :group_id => @group_id,
      :email => ownername,
    }
  )
  owner.delete!
end

#delete!Object



89
90
91
92
# File 'lib/gprov/provision/group.rb', line 89

def delete!
  response = connection.delete("/group/2.0/:domain/#{group_id}")
  status = :deleted
end

#list_membersObject



116
117
118
# File 'lib/gprov/provision/group.rb', line 116

def list_members
  GProv::Provision::Member.all(@connection, @group_id)
end

#list_ownersObject



142
143
144
# File 'lib/gprov/provision/group.rb', line 142

def list_owners
  GProv::Provision::Owner.all(@connection, @group_id)
end

#to_nokogiriObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gprov/provision/group.rb', line 57

def to_nokogiri
  base_document = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
    xml.entry('xmlns:atom' => 'http://www.w3.org/2005/Atom',
              'xmlns:apps' => 'http://schemas.google.com/apps/2006',
              'xmlns:gd'   => "http://schemas.google.com/g/2005" ) {

      # Namespaces cannot be used until they are declared, so we need to
      # retroactively declare the namespace of the parent
      xml.parent.namespace = xml.parent.namespace_definitions.select {|ns| ns.prefix == "atom"}.first
      xml.category("scheme" => "http://schemas.google.com/g/2005#kind",
                   "term"   =>"http://schemas.google.com/apps/2006#user")

      xml['apps'].property("name" => "groupId",         "value" => @group_id)
      xml['apps'].property("name" => "groupName",       "value" => @group_name)
      xml['apps'].property("name" => "emailPermission", "value" => @email_permission)
      xml['apps'].property("name" => "description",     "value" => @description)
    }
  end

  base_document
end

#update!Object



84
85
86
87
# File 'lib/gprov/provision/group.rb', line 84

def update!
  response = connection.put("/group/2.0/:domain", {:body => to_nokogiri.to_xml})
  status = :clean
end