# frozen_string_literal: true module Wayfarer class Page attr_reader :url, :status_code, :body, :headers def initialize(url:, status_code:, body:, headers:) @url = url @status_code = status_code @body = body @headers = headers.transform_keys(&:downcase) end def doc return @doc if @doc # If no Content-Type field is present, assume HTML/XML return @doc = Wayfarer::Parsing::XML.parse_html(body) unless headers["content-type"] content_type = headers["content-type"] sub_type = MIME::Types[content_type].first.sub_type @doc = case sub_type when "json" then Wayfarer::Parsing::JSON.parse(body) when "xml" then Wayfarer::Parsing::XML.parse_xml(body) else Wayfarer::Parsing::XML.parse_html(body) end end def meta @meta ||= MetaInspector.new(url, document: body) end end end