class Payload
  attr_reader :header, :body

  def initialize(header, body)
    @header = header
    @body = body
    check
  end

  def check
    signature = Signature.new(header, body)
    halt 401 unless signature.valid?
  end

  def installation_id
    parse["installation"]["id"]
  end

  def parse
    JSON.parse body
  rescue JSON::ParserError => e
    [400, { "Content-Type" => "application/json" },
     [{ status: 400, error: "There was a problem in the JSON: #{e}" }.to_json]]
  end
end
