Class: Innologix::M2m

Inherits:
Object
  • Object
show all
Defined in:
lib/innologix/m2m.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h = {}) ⇒ M2m

Returns a new instance of M2m.



16
17
18
19
# File 'lib/innologix/m2m.rb', line 16

def initialize(h = {})
  h.each { |k, v| public_send("#{k}=", v) }
  @client = Client.default
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



13
14
15
# File 'lib/innologix/m2m.rb', line 13

def client
  @client
end

#created_atObject

Returns the value of attribute created_at.



8
9
10
# File 'lib/innologix/m2m.rb', line 8

def created_at
  @created_at
end

#errorObject

Returns the value of attribute error.



14
15
16
# File 'lib/innologix/m2m.rb', line 14

def error
  @error
end

#groupObject

Returns the value of attribute group.



11
12
13
# File 'lib/innologix/m2m.rb', line 11

def group
  @group
end

#group_idObject

Returns the value of attribute group_id.



7
8
9
# File 'lib/innologix/m2m.rb', line 7

def group_id
  @group_id
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/innologix/m2m.rb', line 3

def id
  @id
end

#ipObject

Returns the value of attribute ip.



5
6
7
# File 'lib/innologix/m2m.rb', line 5

def ip
  @ip
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/innologix/m2m.rb', line 4

def name
  @name
end

#statusObject

Returns the value of attribute status.



6
7
8
# File 'lib/innologix/m2m.rb', line 6

def status
  @status
end

#updated_atObject

Returns the value of attribute updated_at.



9
10
11
# File 'lib/innologix/m2m.rb', line 9

def updated_at
  @updated_at
end

Instance Method Details

#add_devices(devices = [], callback = nil) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/innologix/m2m.rb', line 93

def add_devices(devices = [], callback = nil)
  path = '/m2ms/' + id.to_s + '/add_devices'
  method = 'patch'
  form_params = {devices: devices, callback: callback}
  options = {form_params: form_params}
  result = client.call_api(path, method, options)
  if result[:error].nil?
    from_hash(result)
  else
    RequestError.new(result)
  end
end

#check_status(callback = nil) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/innologix/m2m.rb', line 132

def check_status(callback = nil)
  path = '/m2ms/' + id.to_s + '/check_status'
  method = 'patch'
  form_params = {callback: callback}
  options = {form_params: form_params}
  result = client.call_api(path, method, options)
  if result[:error].nil?
    result[:message]
  else
    RequestError.new(result)
  end
end

#createObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/innologix/m2m.rb', line 56

def create
  path = '/m2ms'
  method = 'post'
  form_params = {name: name, ip: ip, group_id: group_id}
  options = {form_params: {m2m: form_params}}
  result = client.call_api(path, method, options)
  if result[:error].nil?
    from_hash(result)
  else
    RequestError.new(result)
  end
end

#deleteObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/innologix/m2m.rb', line 82

def delete
  path = '/m2ms/' + id.to_s
  method = 'delete'
  result = client.call_api(path, method)
  if result[:error].nil?
    from_hash(result)
  else
    RequestError.new(result)
  end
end

#devices(offset = 0, limit = 10, params = {}) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/innologix/m2m.rb', line 160

def devices(offset = 0, limit = 10, params = {})
  path = '/devices/list'
  method = 'get'
  options = {query_params: {m2m_id: self.id, offset: offset, limit: limit}.merge(params)}
  result = client.call_api(path, method, options)
  list =[]
  if result[:error].nil?
    result[:devices].each do |device|
      list.push(Innologix::Device.new(device))
    end
  end
  meta = OpenStruct.new
  meta.offset = result[:meta][:offset]
  meta.limit = result[:meta][:limit]
  meta.total = result[:meta][:total]

  result = OpenStruct.new
  result.devices = list
  result.meta = meta
  result
end

#from_hash(attributes) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/innologix/m2m.rb', line 145

def from_hash(attributes)
  m2m = Innologix::M2m.new
  m2m.id = attributes[:id]
  m2m.name = attributes[:name]
  m2m.ip = attributes[:ip]
  m2m.status = attributes[:status]
  m2m.group_id = attributes[:group_id]
  m2m.created_at = attributes[:created_at]
  m2m.updated_at = attributes[:updated_at]
  if attributes[:group] != nil
    m2m.group = Innologix::Group.new(attributes[:group])
  end
  m2m
end

#get(id) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/innologix/m2m.rb', line 45

def get(id)
  path = '/m2ms/' + id.to_s
  method = 'get'
  result = client.call_api(path, method)
  if result[:error].nil?
    from_hash(result)
  else
    RequestError.new(result)
  end
end

#list(offset = 0, limit = 10, params = {}) ⇒ Object



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

def list(offset = 0, limit = 10, params = {})
  path = '/m2ms'
  method = 'get'
  options = {query_params: {offset: offset, limit: limit}.merge(params)}
  result = client.call_api(path, method, options)
  if result[:error].nil?
    list =[]
    result[:m2ms].each do |m2m|
      list.push(from_hash(m2m))
    end
    meta = OpenStruct.new
    meta.offset = result[:meta][:offset]
    meta.limit = result[:meta][:limit]
    meta.total = result[:meta][:total]

    result = OpenStruct.new
    result.m2ms = list
    result.meta = meta
    result
  else
    RequestError.new(result)
  end
end

#remove_devices(devices = [], callback = nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/innologix/m2m.rb', line 106

def remove_devices(devices = [], callback = nil)
  path = '/m2ms/' + id.to_s + '/remove_devices'
  method = 'patch'
  form_params = {devices: devices, callback: callback}
  options = {form_params: form_params}
  result = client.call_api(path, method, options)
  if result[:error].nil?
    from_hash(result)
  else
    RequestError.new(result)
  end
end

#restart(callback = nil) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/innologix/m2m.rb', line 119

def restart(callback = nil)
  path = '/m2ms/' + id.to_s + '/restart'
  method = 'patch'
  form_params = {callback: callback}
  options = {form_params: form_params}
  result = client.call_api(path, method, options)
  if result[:error].nil?
    result[:message]
  else
    RequestError.new(result)
  end
end

#total_devicesObject



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/innologix/m2m.rb', line 182

def total_devices
  path = '/devices/total'
  method = 'get'
  options = {query_params: {m2m_id: self.id}}
  result = client.call_api(path, method, options)
  if result[:error].nil?
    result
  else
    0
  end
end

#updateObject



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/innologix/m2m.rb', line 69

def update
  path = '/m2ms/' + id.to_s
  method = 'put'
  form_params = {name: name, ip: ip, group_id: group_id}
  options = {form_params: {m2m: form_params}}
  result = client.call_api(path, method, options)
  if result[:error].nil?
    from_hash(result)
  else
    RequestError.new(result)
  end
end