Class: SimpleMDM::AppGroup

Inherits:
Base
  • Object
show all
Defined in:
lib/simplemdm/app_group.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

build

Class Method Details

.allObject



10
11
12
13
14
# File 'lib/simplemdm/app_group.rb', line 10

def self.all
  hash, code = fetch("app_groups")

  hash['data'].collect { |d| build d }
end

.find(id) ⇒ Object



16
17
18
19
20
# File 'lib/simplemdm/app_group.rb', line 16

def self.find(id)
  hash, code = fetch("app_groups/#{id}")

  build hash['data']
end

Instance Method Details

#add_app(app) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/simplemdm/app_group.rb', line 67

def add_app(app)
  raise "You must save this app group before changing associations" if new?
  raise "The object you provided is not an app" unless app.kind_of?(SimpleMDM::App)
  raise "You must save the app before associating it" if app.id.nil?

  hash, code = fetch("app_groups/#{self.id}/apps/#{app.id}", :post)

  if code == 204
    self['app_ids'] = self['app_ids'] | [app.id]
    true
  else
    false
  end
end

#add_device(device) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/simplemdm/app_group.rb', line 127

def add_device(device)
  raise "You must save this app group before changing associations." if new?
  raise "The object you provided is not a device" unless device.kind_of?(SimpleMDM::Device)
  raise "You must save the device before associating it" if device.id.nil?

  hash, code = fetch("app_groups/#{self.id}/devices/#{device.id}", :post)

  if code == 204
    self['device_ids'] = self['device_ids'] | [device.id]
    true
  else
    false
  end
end

#add_device_group(device_group) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/simplemdm/app_group.rb', line 97

def add_device_group(device_group)
  raise "You must save this app group before changing associations." if new?
  raise "The object you provided is not a device group" unless device_group.kind_of?(SimpleMDM::DeviceGroup)
  raise "You must save the device_group before associating it" if device_group.id.nil?

  hash, code = fetch("app_groups/#{self.id}/device_groups/#{device_group.id}", :post)

  if code == 204
    self['device_group_ids'] = self['device_group_ids'] | [device_group.id]
    true
  else
    false
  end
end

#auto_deploy=(val) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/simplemdm/app_group.rb', line 51

def auto_deploy=(val)
  if val != self.auto_deploy
    @dirty = true
  end

  self['auto_deploy'] = val
end

#build(hash = nil) ⇒ Object



4
5
6
7
8
# File 'lib/simplemdm/app_group.rb', line 4

def build(hash = nil)
  @dirty = false

  super
end

#destroyObject



59
60
61
62
63
64
65
# File 'lib/simplemdm/app_group.rb', line 59

def destroy
  raise "You cannot delete an app group that hasn't been created yet" if new?

  hash, code = fetch("app_groups/#{self.id}", :delete)

  code == 204
end

#name=(val) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/simplemdm/app_group.rb', line 43

def name=(val)
  if val != self.name
    @dirty = true
  end

  self['name'] = val
end

#push_appsObject



157
158
159
160
161
162
163
# File 'lib/simplemdm/app_group.rb', line 157

def push_apps
  raise "You cannot push apps for an app group that hasn't been created yet" if new?

  hash, code = fetch("app_groups/#{self.id}/push_apps", :post)

  code == 202
end

#remove_app(app) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/simplemdm/app_group.rb', line 82

def remove_app(app)
  raise "You must save this app group before changing associations." if new?
  raise "The object you provided is not an app" unless app.kind_of?(SimpleMDM::App)
  raise "The app you provided doesn't exist" if app.id.nil?

  hash, code = fetch("app_groups/#{self.id}/apps/#{app.id}", :delete)

  if code == 204
    self['app_ids'].delete(app.id)
    true
  else
    false
  end
end

#remove_device(device) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/simplemdm/app_group.rb', line 142

def remove_device(device)
  raise "You must save this app group before changing associations" if new?
  raise "The object you provided is not a device" unless device.kind_of?(SimpleMDM::Device)
  raise "The device you provided doesn't exist" if device.id.nil?

  hash, code = fetch("app_groups/#{self.id}/devices/#{device.id}", :delete)

  if code == 204
    self['device_ids'].delete(device.id)
    true
  else
    false
  end
end

#remove_device_group(device_group) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/simplemdm/app_group.rb', line 112

def remove_device_group(device_group)
  raise "You must save this app group before changing associations" if new?
  raise "The object you provided is not a device group" unless device_group.kind_of?(SimpleMDM::DeviceGroup)
  raise "The device group you provided doesn't exist" if device_group.id.nil?

  hash, code = fetch("app_groups/#{self.id}/device_groups/#{device_group.id}", :delete)

  if code == 204
    self['device_group_ids'].delete(device_group.id)
    true
  else
    false
  end
end

#saveObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/simplemdm/app_group.rb', line 22

def save
  if @dirty || new?
    params = {}
    params[:name]        = self.name unless self.name.nil?
    params[:auto_deploy] = self.auto_deploy unless self.auto_deploy.nil?

    if new?
      hash, code = fetch("app_groups", :post, params)

      self.id = hash['data']['id']
      self.merge!(hash['data']['attributes'])
    else
      fetch("app_groups/#{self.id}", :patch, params)
    end

    @dirty = false
  end

  self
end