Sha256: 88e42aeddb577978637c3695c8040f3837e875eccc3d1adf8675c0994f28a86d

Contents?: true

Size: 1.19 KB

Versions: 3

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module Wayfarer
  module Middleware
    class Fetch
      extend Base

      module API
        def agent
          context.instance
        end

        def context
          task.metadata.context
        end

        def page(live: false)
          return task.metadata.page unless live

          task.metadata.page = context.live&.page || task.metadata.page
        end

        def http
          @http ||= Wayfarer::Networking::Follow.new(
            Wayfarer::Networking::Context.new(
              Wayfarer::Networking::HTTP.new
            )
          )
        end
      end

      def call(task)
        pool.with do |context|
          result = task.metadata.controller.run_callbacks(:fetch) do
            context.fetch(task.url)
          end

          case result
          when Networking::Result::Redirect
            task.metadata.controller.stage(result.redirect_url)
          when Networking::Result::Success
            task.metadata.context = context
            task.metadata.page = result.page
            yield if block_given?
          end
        end
      end

    private

      def pool
        Wayfarer::Networking::Pool.instance
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wayfarer-0.4.6 lib/wayfarer/middleware/fetch.rb
wayfarer-0.4.5 lib/wayfarer/middleware/fetch.rb
wayfarer-0.4.4 lib/wayfarer/middleware/fetch.rb