Class: Interapp::SendMessageService

Inherits:
Object
  • Object
show all
Defined in:
app/services/interapp/send_message_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, peer_identifier:) ⇒ SendMessageService

Returns a new instance of SendMessageService.



8
9
10
11
12
13
# File 'app/services/interapp/send_message_service.rb', line 8

def initialize(data:, peer_identifier:)
  find_peer(peer_identifier)
  @payload = JSON.dump(data)
  @message = Message.new(payload: @payload, peer: peer)
  @message.sign
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



6
7
8
# File 'app/services/interapp/send_message_service.rb', line 6

def message
  @message
end

#payloadObject (readonly)

Returns the value of attribute payload.



6
7
8
# File 'app/services/interapp/send_message_service.rb', line 6

def payload
  @payload
end

#peerObject (readonly)

Returns the value of attribute peer.



6
7
8
# File 'app/services/interapp/send_message_service.rb', line 6

def peer
  @peer
end

Instance Method Details

#performObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/services/interapp/send_message_service.rb', line 15

def perform
  response = Net::HTTP.post(
    URI(peer.endpoint),
    message.payload,
    {
      "X-Interapp-Identifier" => Interapp.configuration.identifier,
      "X-Interapp-Signature" => message.signature,
      "Content-Type" => "application/json",
      "Accept" => "application/json"
    }
  )
  JSON.parse(response.body) if response
end