Class: Messaging::Message::FromJson

Inherits:
Object
  • Object
show all
Defined in:
lib/messaging/message/from_json.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ FromJson

Returns a new instance of FromJson.



9
10
11
12
# File 'lib/messaging/message/from_json.rb', line 9

def initialize(json)
  @attributes = JSON.parse(json, symbolize_names: true)
  @klass = message_class_from_name(@attributes[:message_name])
end

Class Method Details

.call(json) ⇒ Object



5
6
7
# File 'lib/messaging/message/from_json.rb', line 5

def self.call(json)
  new(json).to_message
end

Instance Method Details

#empty_messageObject



14
15
16
# File 'lib/messaging/message/from_json.rb', line 14

def empty_message
  Hash[@klass.attribute_set.map { |a| [a.name, nil] }]
end

#message_class_from_name(message_name) ⇒ Object



26
27
28
# File 'lib/messaging/message/from_json.rb', line 26

def message_class_from_name(message_name)
  "::#{message_name.camelize}".constantize
end

#new_without_defaults(attributes) ⇒ Object



18
19
20
# File 'lib/messaging/message/from_json.rb', line 18

def new_without_defaults(attributes)
  @klass.new(empty_message.merge(attributes))
end

#to_messageObject



22
23
24
# File 'lib/messaging/message/from_json.rb', line 22

def to_message
  new_without_defaults(@attributes)
end