Sha256: b3b0852d32f364f2bac6195c28163ce7b55ba6e93c1212e13b4a5a94816adce6

Contents?: true

Size: 980 Bytes

Versions: 1

Compression:

Stored size: 980 Bytes

Contents

# 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"].first
      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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wayfarer-0.4.1 lib/wayfarer/page.rb