Class: WolasChannel
- Inherits:
-
Object
- Object
- WolasChannel
- Defined in:
- lib/wolas_channel.rb,
lib/wolas_channel/version.rb
Overview
This class handles mapping the channel items from and to the database
Constant Summary collapse
- VERSION =
"0.1.11"
Instance Method Summary collapse
-
#add_channel(type = 'Not Specified') ⇒ Object
This method allows a tenant to add a channel to their tenancy.
-
#delete_entity(entity_id) ⇒ Object
This method allows the update of an entity.
-
#get_channel(channel_id) ⇒ Object
This method returns all entries in a channel.
-
#get_channel_kv(channel_id, *kv) ⇒ Object
This method allows an entity to be received by a channel and key/value match.
-
#get_channel_recent(channel_id) ⇒ Object
This method returns the 20 most recent entries for a channel.
-
#get_entity(entity_id) ⇒ Object
This method returns an entity based off an id.
-
#initialize(tenant_id) ⇒ WolasChannel
constructor
Initialize a tenant_id when the channel service is instantiated.
-
#insert_entity(obj, *channel_ids) ⇒ Object
This method allows the creation of a new entity against a channel / channels.
-
#return_channel(*channel_ids) ⇒ Object
This method allows a tenant to return the name of channels, based on ids.
-
#tenant_channels ⇒ Object
This method returns all the current channels for a tenant.
-
#update_entity(entity_id, key, value) ⇒ Object
This method allows the update of an entity.
Constructor Details
#initialize(tenant_id) ⇒ WolasChannel
Initialize a tenant_id when the channel service is instantiated
21 22 23 |
# File 'lib/wolas_channel.rb', line 21 def initialize(tenant_id) @tenant_id = tenant_id end |
Instance Method Details
#add_channel(type = 'Not Specified') ⇒ Object
This method allows a tenant to add a channel to their tenancy
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/wolas_channel.rb', line 26 def add_channel(type = 'Not Specified') begin # Connect to the database connect = Connection.new.open insert = "INSERT INTO `channel`.`channels` (`tenant_id`, `type`) VALUES (?, ?)" # This method performs the insert. Mysql2 does not return an object on a successful # insert prepare_exe(connect, insert, @tenant_id, type) # Return the last id from mysql2 object channel_id = connect.last_id return { channel_id: channel_id, type: type } ensure connect.close end end |
#delete_entity(entity_id) ⇒ Object
This method allows the update of an entity
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/wolas_channel.rb', line 380 def delete_entity(entity_id) begin # Connect to the database. Instance variable used to pass connection to # private methods @connect = Connection.new.open # Determine if the tenant has access to the entity requested tenant_entity(entity_id) # update the existing entity body update = "UPDATE `channel`.`entities` SET `entities`.`active` = ? WHERE `entities`.`entity_id` = ?" prepare_exe(@connect, update, 0, entity_id) # Return the updated entity to the entity = entity_construct(entity_id) return entity ensure @connect.close end end |
#get_channel(channel_id) ⇒ Object
This method returns all entries in a channel. If the channel id of the entity returned does not match that of the tenant making the request, an error is raised
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/wolas_channel.rb', line 112 def get_channel(channel_id) begin # Connect to the database. Instance variable used to pass connection to # private methods @connect = Connection.new.open # Determine if this channel is valid for the tenant and returns its type type = tenant_channel(channel_id) return nil if type == nil # Now the entities are requested ent_q = "SELECT `entities`.`entity_id`, `entities`.`body`, `entities`.`created` FROM `channel`.`entities` \ INNER JOIN `channel`.`channel_entities` on `entities`.`entity_id` = `channel_entities`.`entity_id` \ INNER JOIN `channel`.`channels` on `channel_entities`.`channel_id` = `channels`.`channel_id` \ WHERE `channels`.`channel_id` = ? AND `entities`.`active` = ?" ent = prepare_exe(@connect, ent_q, channel_id, 1) # And their metadata data_q = "SELECT `data`.`entity_id`, `data`.`key`, `data`.`value` FROM `channel`.`data` \ INNER JOIN `channel`.`channel_entities` on `data`.`entity_id` = `channel_entities`.`entity_id` \ INNER JOIN `channel`.`channels` on `channel_entities`.`channel_id` = `channels`.`channel_id` \ WHERE `channels`.`channel_id` = ?" data = prepare_exe(@connect, data_q, channel_id) # Return nil if nothing is returned from the select from the entities table return nil if ent.count == 0 # Construct the object requested using the generic method entitys = object_constructor(ent, data) return entitys ensure @connect.close end end |
#get_channel_kv(channel_id, *kv) ⇒ Object
This method allows an entity to be received by a channel and key/value match
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/wolas_channel.rb', line 185 def get_channel_kv(channel_id, *kv) begin # Connect to the database. Instance variable used to pass the connection to # private methods @connect = Connection.new.open # Determine if the tenant is able to view this entity based off the # channel and return the type of the entity type = tenant_channel(channel_id) return nil if type == nil return nil if kv.length == 0 # This count determines the number of key / value matches that exist per # entity. If the number of matches equals the number of kvs passed to the # method, then the search criteria is met and the entity is returned. match_q = "SELECT `entities`.`entity_id`, count(*) AS `matches` FROM `channel`.`entities` \ INNER JOIN `channel`.`data` ON `entities`.`entity_id` = `data`.`entity_id` \ INNER JOIN `channel`.`channel_entities` ON `data`.`entity_id` = `channel_entities`.`entity_id` \ INNER JOIN `channel`.`channels` ON `channel_entities`.`channel_id` = `channels`.`channel_id` \ WHERE `channels`.`channel_id` = ? AND `entities`.`active` = ? AND (" kv_arr = [] kv.each_with_index do |hash, index| if index != kv.length - 1 match_q = match_q + "(`data`.`key` = ? AND `data`.`value` = ?) OR " else match_q = match_q + "(`data`.`key` = ? AND `data`.`value` = ?))" end # Push the key followed by the value into the array to match ?s kv_arr << hash[:key] kv_arr << hash[:value] end match_q = match_q + "GROUP BY `entities`.`entity_id`" match = prepare_exe(@connect, match_q, channel_id, 1, *kv_arr) match_raw = match.map{ |e| e } entity_ids = [] # The entity id is only push into the array if the matches count is equal to # the number of key / values used in the search match_raw.each do |hash| if hash['matches'] == kv.length entity_ids << hash['entity_id'] end end # Return nil if nothing is pushed into the entity_ids array return nil if entity_ids.count == 0 # Determine the number of question marks required for the in statements, # based of the number of entity_ids q_marks = in_prepare(entity_ids) # Now the entities are requested ent_q = "SELECT `entities`.`entity_id`, `entities`.`body`, `entities`.`created` FROM `channel`.`entities` \ INNER JOIN `channel`.`channel_entities` on `entities`.`entity_id` = `channel_entities`.`entity_id` \ INNER JOIN `channel`.`channels` on `channel_entities`.`channel_id` = `channels`.`channel_id` \ WHERE `channels`.`channel_id` = ? AND `entities`.`entity_id` IN (#{q_marks})" ent = prepare_exe(@connect, ent_q, channel_id, *entity_ids) # And their metadata data_q = "SELECT `data`.`entity_id`, `data`.`key`, `data`.`value` FROM `channel`.`data` \ INNER JOIN `channel`.`channel_entities` on `data`.`entity_id` = `channel_entities`.`entity_id` \ INNER JOIN `channel`.`channels` on `channel_entities`.`channel_id` = `channels`.`channel_id` \ WHERE `channels`.`channel_id` = ? AND `data`.`entity_id` IN (#{q_marks})" data = prepare_exe(@connect, data_q, channel_id, *entity_ids) # Construct the object requested using the generic method entitys = object_constructor(ent, data) return entitys ensure @connect.close end end |
#get_channel_recent(channel_id) ⇒ Object
This method returns the 20 most recent entries for a channel
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/wolas_channel.rb', line 147 def get_channel_recent(channel_id) begin # Connect to the database. Instance variable used to pass connection to # private methods @connect = Connection.new.open # Determine if this channel is valid for the tenant and returns its type type = tenant_channel(channel_id) return nil if type == nil # Now the 20 most recent entities are requested ent_q = "SELECT `entities`.`entity_id`, `entities`.`body`, `entities`.`created` FROM `channel`.`entities` \ INNER JOIN `channel`.`channel_entities` on `entities`.`entity_id` = `channel_entities`.`entity_id` \ INNER JOIN `channel`.`channels` on `channel_entities`.`channel_id` = `channels`.`channel_id` \ WHERE `channels`.`channel_id` = ? AND `entities`.`active` = ? \ ORDER BY `entities`.`created` DESC LIMIT 20" ent = prepare_exe(@connect, ent_q, channel_id, 1) # All meta data is selected for the channel as the entity_ids are unknown. # However any data that does not match an entity id from the query above will # not be inserted anywhere. data_q = "SELECT `data`.`entity_id`, `data`.`key`, `data`.`value` FROM `channel`.`data` \ INNER JOIN `channel`.`channel_entities` on `data`.`entity_id` = `channel_entities`.`entity_id` \ INNER JOIN `channel`.`channels` on `channel_entities`.`channel_id` = `channels`.`channel_id` \ WHERE `channels`.`channel_id` = ?" data = prepare_exe(@connect, data_q, channel_id) # Return nil if nothing is returned from the select from the entities table return nil if ent.count == 0 # Construct the object requested using the generic method entitys = object_constructor(ent, data) return entitys ensure @connect.close end end |
#get_entity(entity_id) ⇒ Object
This method returns an entity based off an id. If the channel id of the entity does not match that of the tenant making the request, an error is raised
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/wolas_channel.rb', line 91 def get_entity(entity_id) begin # Connect to the database. Instance variable used to pass the connection to # private methods @connect = Connection.new.open # Determine if the tenant is able to view this entity based off the # channel and return the type of the entity check = tenant_entity(entity_id) return nil if check == [nil, nil] # Now the entity is requested entity = entity_construct(entity_id) return entity ensure @connect.close end end |
#insert_entity(obj, *channel_ids) ⇒ Object
This method allows the creation of a new entity against a channel / channels. The tenant id must match the id of the channel passed or an error is raised
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/wolas_channel.rb', line 260 def insert_entity(obj, *channel_ids) begin # Connect to the database. Instance variable used to pass connection to # private methods @connect = Connection.new.open # Determine if the tenant has access to all channels requested channel_ids.each do |channel_id| tenant_channel(channel_id) end # Scan object for type, if it doesn't exist, make it equal to default type = obj[:type] || obj['type'] unless type type = 'Text' obj[:type] = 'Text' end # Throw errors if Video and Image types do not have required field if type == 'Video' || type == 'Image' bucket_key = obj[:bucket_key] || obj['bucket_key'] link = obj[:link] || obj['link'] unless bucket_key || link = 'A video or image entity object must include an S3 bucketkey or link' raise ErrorHandling::InvalidObject, end end # Define body and created created = Time.now.utc body = obj[:body] || obj['body'] unless body raise ErrorHandling::InvalidObject, 'The entity object must include a body field' end insert = "INSERT INTO `channel`.`entities` (`body`, `created`, `active`) VALUES (?, ?, ?)" insert_sql = prepare_exe(@connect, insert, body, created, 1) # The last_id mysql2 method provides direct access to the last id inserted entity_id = @connect.last_id # Delete body key from object in preparation for k/v insert obj['body'] ? obj.delete('body') : obj.delete(:body) # Insert remaining as k/vs, with entity id from above obj.each do |key, value| insert = "INSERT INTO `channel`.`data` (`key`, `value`, `entity_id`)\ VALUES (?, ?, ?)" # This method performs the insert. Mysql2 does not return an object on a successful # insert prepare_exe(@connect, insert, key.to_s, value, entity_id) end # Insert link entries based on channel_ids passed to method channel_ids.each do |channel_id| insert = "INSERT INTO `channel`.`channel_entities` (`channel_id`, `entity_id`)\ VALUES (?, ?)" # This method performs the insert. Mysql2 does not return an object on a successful # insert prepare_exe(@connect, insert, channel_id, entity_id) end # Now the entity that was inserted is requested entity = entity_construct(entity_id) return entity ensure @connect.close end end |
#return_channel(*channel_ids) ⇒ Object
This method allows a tenant to return the name of channels, based on ids
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/wolas_channel.rb', line 65 def return_channel(*channel_ids) begin # Connect to the database @connect = Connection.new.open # Determine if the tenant has access to all channels requested channel_ids.each do |channel_id| tenant_channel(channel_id) end q_marks = in_prepare(channel_ids) query = "SELECT `channels`.`channel_id`, `channels`.`type` FROM `channel`.`channels` \ WHERE `channels`.`channel_id` IN (#{q_marks})" # This method performs the insert. Mysql2 does not return an object on a successful # insert result = prepare_exe(@connect, query, *channel_ids) res = result.each { |r| r } return res ensure @connect.close end end |
#tenant_channels ⇒ Object
This method returns all the current channels for a tenant
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/wolas_channel.rb', line 46 def tenant_channels begin # Connect to the database @connect = Connection.new.open query = "SELECT `channels`.`channel_id`, `channels`.`type` FROM `channel`.`channels` \ WHERE `channels`.`tenant_id` = ?" # This method performs the insert. Mysql2 does not return an object on a successful # insert result = prepare_exe(@connect, query, @tenant_id) res = result.each { |r| r } return res ensure @connect.close end end |
#update_entity(entity_id, key, value) ⇒ Object
This method allows the update of an entity
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/wolas_channel.rb', line 329 def update_entity(entity_id, key, value) begin # Connect to the database. Instance variable used to pass connection to # private methods @connect = Connection.new.open # Determine if the tenant has access to the entity requested check = tenant_entity(entity_id) # If the key requested for the update is the body, the entity itself is updated. # If it is not, the key is searched for in the data table and either updated or # inserted if key == 'body' || key == :body # update the existing entity body update = "UPDATE `channel`.`entities` SET `entities`.`body` = ? WHERE `entities`.`entity_id` = ?" prepare_exe(@connect, update, value, entity_id) # Return the updated entity to the entity = entity_construct(entity_id) else # Lookup the key to be changed on the entity, insert it if it doesn't exist ent_q = "SELECT * FROM `channel`.`data` WHERE `data`.`entity_id` = ? AND `data`.`key` = ?" ent = prepare_exe(@connect, ent_q, entity_id, key) if ent.count != 0 # update the existing key value update = "UPDATE `channel`.`data` SET `data`.`value`= ? \ WHERE `data`.`entity_id` = ? AND `data`.`key` = ?" prepare_exe(@connect, update, value, entity_id, key.to_s) # Return the updated entity to the entity = entity_construct(entity_id) else # insert the new key as it does not exist for the entity insert = "INSERT INTO `channel`.`data` (`key`, `value`, `entity_id`)\ VALUES (?, ?, ?)" prepare_exe(@connect, insert, key.to_s, value, entity_id) # Return the updated entity to the entity = entity_construct(entity_id) end end return entity ensure @connect.close end end |