Class: Megam::Sensors

Inherits:
ServerAPI show all
Defined in:
lib/megam/core/sensors.rb

Instance Attribute Summary

Attributes inherited from ServerAPI

#api_key, #email, #host, #org_id, #password

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ServerAPI

#megam_rest

Constructor Details

#initialize(email = nil, api_key = nil, host = nil) ⇒ Sensors

Returns a new instance of Sensors.



19
20
21
22
23
24
25
# File 'lib/megam/core/sensors.rb', line 19

def initialize(email = nil, api_key = nil, host = nil)
  @id = nil
  @sensor_type = nil
  @payload = {}
  @created_at = nil
  super(email, api_key, host)
end

Class Method Details

.create(params) ⇒ Object



118
119
120
121
# File 'lib/megam/core/sensors.rb', line 118

def self.create(params)
  sensors = from_hash(params, params['email'], params['api_key'], params['host'])
  sensors.create
end

.from_hash(o, tmp_email = nil, tmp_api_key = nil, tmp_host = nil) ⇒ Object



104
105
106
107
108
# File 'lib/megam/core/sensors.rb', line 104

def self.from_hash(o, tmp_email = nil, tmp_api_key = nil, tmp_host = nil)
  asm = new(tmp_email, tmp_api_key, tmp_host)
  asm.from_hash(o)
  asm
end

.json_create(o) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/megam/core/sensors.rb', line 95

def self.json_create(o)
  asm = new
  asm.id(o['id']) if o.key?('id')
  asm.sensor_type(o['sensor_type']) if o.key?('sensor_type')
  asm.payload(o['payload']) if o.key?('payload')
  asm.created_at(o['created_at']) if o.key?('created_at')
  asm
end

.list(o) ⇒ Object



129
130
131
132
# File 'lib/megam/core/sensors.rb', line 129

def self.list(params)
  sensors = self.new(params["email"], params["api_key"], params["host"])
  sensors.megam_rest.get_sensors
end

.show(params) ⇒ Object

Load a account by email_p



124
125
126
127
# File 'lib/megam/core/sensors.rb', line 124

def self.show(params)
  sensors = new(params['email'], params['api_key'], params['host'])
  sensors.megam_rest.get_sensor(params['id'])
end

.update(params) ⇒ Object



134
135
136
137
# File 'lib/megam/core/sensors.rb', line 134

def self.update(params)
  sensors = from_hash(params, params['email'] || params[:email], params['api_key'] || params[:api_key], params['host'] || params[:host])
  sensors.update
end

Instance Method Details

#createObject



149
150
151
# File 'lib/megam/core/sensors.rb', line 149

def create
  megam_rest.post_sensors(to_hash)
end

#created_at(arg = nil) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/megam/core/sensors.rb', line 55

def created_at(arg = nil)
  if !arg.nil?
    @created_at = arg
  else
    @created_at
  end
end

#error?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/megam/core/sensors.rb', line 63

def error?
  crocked = true if some_msg.key?(:msg_type) && some_msg[:msg_type] == 'error'
end

#for_jsonObject



85
86
87
88
89
90
91
92
93
# File 'lib/megam/core/sensors.rb', line 85

def for_json
  result = {
    'id' => id,
    'sensor_type' => sensor_type,
    'payload' => payload,
    'created_at' => created_at
  }
  result
end

#from_hash(o) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/megam/core/sensors.rb', line 110

def from_hash(o)
  @id = o['id'] if o.key?('id')
  @sensor_type = o['sensor_type'] if o.key?('sensor_type')
  @payload = o['payload'] if o.key?('payload')
  @created_at = o['created_at'] if o.key?('created_at')
  self
end

#id(arg = nil) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/megam/core/sensors.rb', line 31

def id(arg = nil)
  if !arg.nil?
    @id = arg
  else
    @id
  end
end

#payload(arg = nil) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/megam/core/sensors.rb', line 47

def payload(arg = nil)
  if !arg.nil?
    @payload = arg
  else
    @payload
  end
end

#sensor_type(arg = nil) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/megam/core/sensors.rb', line 39

def sensor_type(arg = nil)
  if !arg.nil?
    @sensor_type = arg
  else
    @sensor_type
  end
end

#sensorsObject



27
28
29
# File 'lib/megam/core/sensors.rb', line 27

def sensors
  self
end

#to_hashObject

Transform the ruby obj -> to a Hash



68
69
70
71
72
73
74
75
76
77
# File 'lib/megam/core/sensors.rb', line 68

def to_hash
  index_hash = {}
  index_hash['json_claz'] = self.class.name
  index_hash['id'] = id

  index_hash['sensor_type'] = sensor_type
  index_hash['payload'] = payload
  index_hash['created_at'] = created_at
  index_hash
end

#to_json(*a) ⇒ Object

Serialize this object as a hash: called from JsonCompat. Verify if this called from JsonCompat during testing.



81
82
83
# File 'lib/megam/core/sensors.rb', line 81

def to_json(*a)
  for_json.to_json(*a)
end

#to_sObject



153
154
155
# File 'lib/megam/core/sensors.rb', line 153

def to_s
  Megam::Stuff.styled_hash(to_hash)
end

#updateObject

Create the node via the REST API



145
146
147
# File 'lib/megam/core/sensors.rb', line 145

def update
  megam_rest.update_(to_hash)
end