Module: OpenWire::Envelope

Defined in:
lib/open_wire/envelope.rb

Class Method Summary collapse

Class Method Details

.inbound?(payload) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/open_wire/envelope.rb', line 47

def inbound?(payload)
  payload.is_a?(Hash) &&
    payload["protocol"].to_s == OpenWire::PROTOCOL &&
    payload["type"].to_s == "message.inbound"
end

.parse_inbound(payload) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/open_wire/envelope.rb', line 53

def parse_inbound(payload)
  data =
    case payload
    when String
      JSON.parse(payload)
    when Hash
      payload
    else
      raise WebhookError, "Inbound payload must be JSON object or string"
    end

  raise WebhookError, "Not an open-wire/1 message.inbound envelope" unless inbound?(data)

  InboundMessage.new(data)
rescue JSON::ParserError => e
  raise WebhookError, "Invalid JSON: #{e.message}"
end