Module: CS::EndPoint
- Includes:
- Serializer
- Included in:
- Group, Notification, Sensor, SensorData, Trigger, User
- Defined in:
- lib/cs/end_point.rb,
lib/cs/end_point/user.rb,
lib/cs/end_point/group.rb,
lib/cs/end_point/sensor.rb,
lib/cs/end_point/trigger.rb,
lib/cs/end_point/sensor_data.rb,
lib/cs/end_point/notification.rb
Defined Under Namespace
Modules: ClassMethod Classes: Group, Notification, Sensor, SensorData, Trigger, User
Instance Attribute Summary collapse
-
#session ⇒ Object
Returns the value of attribute session.
Instance Method Summary collapse
-
#create(options = {}) ⇒ Object
Create a new endpoint object to CS, just like #create! but it will return false if there is an error.
-
#create!(options = {}) ⇒ Object
Create a new end point object to CS.
-
#delete ⇒ Object
Delete the current end point object from CS, just like #delete! but it will return nil if there is an error.
-
#delete! ⇒ Object
Delete the current end point object from CS.
- #duplicate ⇒ Object
- #initialize(hash = {}) ⇒ Object
- #inspect ⇒ Object
-
#parameter(name) ⇒ Object
get value of property name.
-
#reload ⇒ Object
alias for #retrieve.
-
#reload! ⇒ Object
alias for #retrieve!.
-
#retrieve ⇒ Object
it will retrieve / reload current object form CS, just like #retrieve! but it will return false instead of raise an exception if there is an error.
-
#retrieve! ⇒ Object
Retrieve Data from CS of the current object based on the id of the object.
-
#save(options = {}) ⇒ Object
it will persist data to CS just like #save! but it will return false instead of exception if it encouter error while persiting data.
-
#save!(options = {}) ⇒ Object
Persist end point object to CS.
-
#to_parameters ⇒ Object
generate a hash representation of this end point.
-
#update(options = {}) ⇒ Object
Update current end point object to CS, just like #update! but it will return nil if there is an error.
-
#update!(options = {}) ⇒ Object
Update current end point object to CS.
-
#url_for(method, id = nil) ⇒ Object
return the commonsense URL for method vaild value for method is ‘:get`, `:post`, `:put`, or `:delete`.
Methods included from Serializer
Instance Attribute Details
#session ⇒ Object
Returns the value of attribute session.
8 9 10 |
# File 'lib/cs/end_point.rb', line 8 def session @session end |
Instance Method Details
#create(options = {}) ⇒ Object
Create a new endpoint object to CS, just like #create! but it will return false if there is an error.
98 99 100 |
# File 'lib/cs/end_point.rb', line 98 def create(={}) create!() rescue false end |
#create!(options = {}) ⇒ Object
Create a new end point object to CS. It will raise an exception if there is an error
example for Sensor object:
sensor = client.sensors.build
sensor.name = "accelerometer"
sensor.display_name = "Accelerometer"
sensor.device_type = "BMA123"
sensor.pager_type = "email"
sensor.data_type = "json"
sensor.data_structure = {"x-axis" => "Float", "y-axis" => "Float", "z-axis" => "Float"}
sensor.create! # this will create new sensor on CS
sensor.id # should give you the id of the sensor
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/cs/end_point.rb', line 79 def create!(={}) parameter = self.to_parameters parameter.merge!() res = session.post(post_url, parameter) if session.response_code != 201 errors = session.errors rescue nil raise Error::ResponseError, errors end location_header = session.response_headers["location"] id = scan_header_for_id(location_header) self.id = id[0] if id true end |
#delete ⇒ Object
Delete the current end point object from CS, just like #delete! but it will return nil if there is an error
195 196 197 |
# File 'lib/cs/end_point.rb', line 195 def delete delete! rescue false end |
#delete! ⇒ Object
Delete the current end point object from CS. It will throw an exception if there is an error
example for Sensor object:
sensor = client.sensors.find(1)
sensor.name = "new name"
sensor.delete!
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/cs/end_point.rb', line 177 def delete! check_session! raise Error::ResourceIdError unless @id res = session.delete(delete_url) if session.response_code != 200 errors = session.errors rescue nil raise Error::ResponseError, errors end self.id = nil true end |
#duplicate ⇒ Object
208 209 210 211 212 |
# File 'lib/cs/end_point.rb', line 208 def duplicate clone = self.dup clone.id = nil clone end |
#initialize(hash = {}) ⇒ Object
10 11 12 |
# File 'lib/cs/end_point.rb', line 10 def initialize(hash={}) from_hash(hash) end |
#inspect ⇒ Object
20 21 22 23 |
# File 'lib/cs/end_point.rb', line 20 def inspect inspection = self.to_h.collect {|k,v| "#{k}: #{v.inspect}"}.compact.join(", ") "#<#{self.class} #{inspection}>" end |
#parameter(name) ⇒ Object
get value of property name
26 27 28 |
# File 'lib/cs/end_point.rb', line 26 def parameter(name) self.instance_variable_get("@#{name}") end |
#reload ⇒ Object
alias for #retrieve
136 137 138 |
# File 'lib/cs/end_point.rb', line 136 def reload retrieve end |
#reload! ⇒ Object
alias for #retrieve!
125 126 127 |
# File 'lib/cs/end_point.rb', line 125 def reload! retieve! end |
#retrieve ⇒ Object
it will retrieve / reload current object form CS, just like #retrieve! but it will return false instead of raise an exception if there is an error.
131 132 133 |
# File 'lib/cs/end_point.rb', line 131 def retrieve retrieve! rescue false end |
#retrieve! ⇒ Object
Retrieve Data from CS of the current object based on the id of the object. It will return an exception if there is an error
example for Sensor object:
sensor = client.sensors.build
sensor.id = "1"
sensor.retrieve!
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/cs/end_point.rb', line 110 def retrieve! check_session! raise Error::ResourceIdError unless @id res = session.get(get_url) if session.response_code != 200 errors = session.errors rescue nil raise Error::ResponseError, errors end from_hash(res[resource.to_s]) if res true end |
#save(options = {}) ⇒ Object
it will persist data to CS just like #save! but it will return false instead of exception if it encouter error while persiting data
61 62 63 |
# File 'lib/cs/end_point.rb', line 61 def save(={}) save!() rescue false end |
#save!(options = {}) ⇒ Object
Persist end point object to CS. It will create a new record on CS if it’s a new object or it will update the object. It will throw an exception if it could not persist object to CS
example for Sensor object:
sensor = client.sensors.build
sensor.name = "accelerometer"
sensor.display_name = "Accelerometer"
sensor.device_type = "BMA123"
sensor.pager_type = "email"
sensor.data_type = "json"
sensor.data_structure = {"x-axis" => "Float", "y-axis" => "Float", "z-axis" => "Float"}
sensor.save! # this will create new sensor on CS
sensor.id # should give you the id of the sensor
sensor.name = "accelerometer edit"
sensor.save! # this will update the sensor
49 50 51 52 53 54 55 56 57 |
# File 'lib/cs/end_point.rb', line 49 def save!(={}) check_session! if @id self.update!() else self.create!() end end |
#to_parameters ⇒ Object
generate a hash representation of this end point
15 16 17 18 |
# File 'lib/cs/end_point.rb', line 15 def to_parameters r = self.resource rescue nil r.nil? ? self.to_h(false) : { self.resource => self.to_h(false) } end |
#update(options = {}) ⇒ Object
Update current end point object to CS, just like #update! but it will return nil if there is an error
165 166 167 |
# File 'lib/cs/end_point.rb', line 165 def update(={}) update!() rescue false end |
#update!(options = {}) ⇒ Object
Update current end point object to CS. It will throw an exception if there is an error
example for Sensor object:
sensor = client.sensors.find(1)
sensor.name = "new name"
sensor.update!
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/cs/end_point.rb', line 147 def update!(={}) check_session! raise Error::ResourceIdError unless @id parameter = self.to_parameters parameter.merge!() res = session.put(put_url, parameter) if session.response_code != 200 errors = session.errors rescue nil raise Error::ResponseError, errors end true end |
#url_for(method, id = nil) ⇒ Object
return the commonsense URL for method vaild value for method is ‘:get`, `:post`, `:put`, or `:delete`
201 202 203 204 205 206 |
# File 'lib/cs/end_point.rb', line 201 def url_for(method, id=nil) raise Error::ResourcesError if resources.nil? url = self.class.class_variable_get("@@#{method}_url".to_sym) url = url.sub(":id", "#{@id}") if id url end |