Class: ActionChannels::Message

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Message

Returns a new instance of Message.



32
33
34
35
36
37
# File 'lib/action_channels/message.rb', line 32

def initialize(attrs)
  @channel = attrs.fetch(:channel)
  @type = attrs.fetch(:type)
  @author = attrs[:author]
  @details = attrs.fetch :details, {}
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



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

def author
  @author
end

#channelObject

Returns the value of attribute channel.



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

def channel
  @channel
end

#detailsObject

Returns the value of attribute details.



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

def details
  @details
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.parse(message_raw) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/action_channels/message.rb', line 13

def parse(message_raw)
  command_json  = JSON.parse message_raw, symbolize_names: true

  new(
    channel: command_json[:channel],
    type: command_json[:type],
    details: command_json.fetch(:details, {})
  )
rescue JSON::ParserError => exp
  raise Errors::NotParseMessage, exp.message
end

.parse_and_setup_author(message_raw, author) ⇒ Object



25
26
27
28
29
# File 'lib/action_channels/message.rb', line 25

def parse_and_setup_author(message_raw, author)
  command = parse(message_raw)
  command.author = author
  command
end

Instance Method Details

#systemic?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/action_channels/message.rb', line 43

def systemic?
  %w(subscribe unsubscribe).include? type
end

#to_rawObject



39
40
41
# File 'lib/action_channels/message.rb', line 39

def to_raw
  JSON.generate(channel: channel, type: type, details: details)
end