Class: Communicator::InboundMessage
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Communicator::InboundMessage
- Defined in:
- lib/communicator/inbound_message.rb
Class Method Summary collapse
-
.create_from_json!(json_message) ⇒ Object
Creates an inbound message from a remote json hash.
-
.create_from_json_collection!(json_messages) ⇒ Object
Expects an already demarshalled collection array containing remote message data, which will then all be processed indivdually using create_from_json!.
-
.last_id ⇒ Object
Find the last ID present locally.
-
.valid_next_id?(from_id) ⇒ Boolean
Checks whether the given id is properly in line with the local last id The check will only be actually performed when there are any locally stored inbound messages.
Instance Method Summary collapse
- #message_content ⇒ Object
-
#process! ⇒ Object
Figure out who is the receiver of this message and process the message.
Class Method Details
.create_from_json!(json_message) ⇒ Object
Creates an inbound message from a remote json hash
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/communicator/inbound_message.rb', line 15 def self.create_from_json!() inbound_msg = Communicator::InboundMessage.new(:body => ["body"]) inbound_msg.id = ["id"] inbound_msg.save! Communicator.logger.info "Created inbound message from json, local id is #{inbound_msg.id}" inbound_msg rescue => err if respond_to?(:report_exception) report_exception err, "Validation Errors" => "Errors: #{inbound_msg.errors.map{|k,v| "#{k}: #{v}"}.join(", ")}", "Inbound Message Record" => inbound_msg.attributes.inspect, "JSON Message" => .inspect end raise err end |
.create_from_json_collection!(json_messages) ⇒ Object
Expects an already demarshalled collection array containing remote message data, which will then all be processed indivdually using create_from_json!
34 35 36 |
# File 'lib/communicator/inbound_message.rb', line 34 def self.create_from_json_collection!() .map {|| create_from_json!() } end |
.last_id ⇒ Object
Find the last ID present locally
39 40 41 |
# File 'lib/communicator/inbound_message.rb', line 39 def self.last_id count > 0 ? find(:first, :order => 'id DESC').id : 0 end |
.valid_next_id?(from_id) ⇒ Boolean
Checks whether the given id is properly in line with the local last id The check will only be actually performed when there are any locally stored inbound messages
45 46 47 |
# File 'lib/communicator/inbound_message.rb', line 45 def self.valid_next_id?(from_id) Communicator::InboundMessage.count == 0 or from_id.to_i == Communicator::InboundMessage.last_id + 1 end |
Instance Method Details
#message_content ⇒ Object
49 50 51 |
# File 'lib/communicator/inbound_message.rb', line 49 def ||= JSON.parse(body).with_indifferent_access end |
#process! ⇒ Object
Figure out who is the receiver of this message and process the message
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/communicator/inbound_message.rb', line 54 def process! if processed_at.present? # Do not process if already processed! Communicator.logger.info "InboundMessage #{id} has already been processed, not processing again!" return false end source, content = .first Communicator.receiver_for(source).find_or_initialize_by_id(content["id"]).(content) self.processed_at = Time.now self.save! Communicator.logger.info "Processed inbound message ##{id} successfully" rescue => err Communicator.logger.warn "Failed to store inbound message ##{id}! Errors: #{self.errors.map{|k,v| "#{k}: #{v}"}.join(", ")}" if respond_to?(:report_exception) report_exception err, "Validation Errors" => "Errors: #{self.errors.map{|k,v| "#{k}: #{v}"}.join(", ")}", "Inbound Message" => self.attributes.inspect, "JSON Content" => self..inspect end raise err end |