Class: Dommy::Headers

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

Overview

Minimal ‘Headers` proxy. Consumer code typically calls `headers.call(:entries)` and iterates via `Array.from(…)`, so we just need `entries` and `get`.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Headers

Returns a new instance of Headers.



259
260
261
# File 'lib/dommy/fetch.rb', line 259

def initialize(hash)
  @hash = hash.is_a?(Hash) ? hash.transform_keys(&:to_s) : {}
end

Class Method Details

.canonical(name) ⇒ Object



299
300
301
# File 'lib/dommy/fetch.rb', line 299

def self.canonical(name)
  name.split("-").map(&:capitalize).join("-")
end

Instance Method Details

#__js_call__(method, args) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/dommy/fetch.rb', line 275

def __js_call__(method, args)
  case method
  when "get"
    name = args[0].to_s
    @hash[name] || @hash[Headers.canonical(name)]
  when "entries"
    @hash.to_a
  when "has"
    @hash.key?(args[0].to_s)
  when "forEach"
    # Browser API: forEach(callback) — callback(value, key)
    cb = args[0]
    @hash.each do |k, v|
      if cb.respond_to?(:__js_call__)
        cb.__js_call__("call", [v, k])
      elsif cb.respond_to?(:call)
        cb.call(v, k)
      end
    end

    nil
  end
end

#__js_get__(_key) ⇒ Object



267
268
269
# File 'lib/dommy/fetch.rb', line 267

def __js_get__(_key)
  nil
end

#__js_set__(_key, _value) ⇒ Object



271
272
273
# File 'lib/dommy/fetch.rb', line 271

def __js_set__(_key, _value)
  nil
end

#to_hObject



263
264
265
# File 'lib/dommy/fetch.rb', line 263

def to_h
  @hash.dup
end