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.



197
198
199
# File 'lib/dommy/fetch.rb', line 197

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

Class Method Details

.canonical(name) ⇒ Object



237
238
239
# File 'lib/dommy/fetch.rb', line 237

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

Instance Method Details

#__js_call__(method, args) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/dommy/fetch.rb', line 213

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



205
206
207
# File 'lib/dommy/fetch.rb', line 205

def __js_get__(_key)
  nil
end

#__js_set__(_key, _value) ⇒ Object



209
210
211
# File 'lib/dommy/fetch.rb', line 209

def __js_set__(_key, _value)
  nil
end

#to_hObject



201
202
203
# File 'lib/dommy/fetch.rb', line 201

def to_h
  @hash.dup
end