Class: Dommy::Resources::FetchHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/dommy/resources.rb

Overview

Maps a Resources adapter onto the __fetch_handler__ callable contract (call(url, init) -> entry Hash | nil) consumed by FetchFn / XHR, so the same Resources resolves window.fetch. A nil Response passes through to the stub maps.

Instance Method Summary collapse

Constructor Details

#initialize(resources, executor: nil, scheduler: nil) ⇒ FetchHandler

executor (responds to submit(job) { |result| }) and scheduler opt this handler into off-thread network: when both are present and the adapter supports request_job, fetch / XHR run the request on a worker and resolve through a DeferredResponse. Without them everything stays synchronous (the deterministic default the tests rely on).



50
51
52
53
54
# File 'lib/dommy/resources.rb', line 50

def initialize(resources, executor: nil, scheduler: nil)
  @resources = resources
  @executor = executor
  @scheduler = scheduler
end

Instance Method Details

#call(url, init = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/dommy/resources.rb', line 56

def call(url, init = nil)
  init = {} unless init.is_a?(Hash)
  method = (init["method"] || "GET").to_s.upcase
  headers = init["headers"].is_a?(Hash) ? init["headers"] : {}
  body = init["body"]&.to_s

  return call_async(method, url, headers, body) if async?

  response = @resources.request(method: method, url: url.to_s, headers: headers, body: body)
  response && to_entry(response, url)
end