Class: Dommy::Response

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

Overview

‘Response` polyfill — just enough surface for Fetchy: `[:status]` / `[:ok]` / `[:url]` / `[:headers]` (with `.entries()` / `.get(name)`) and `.text()` / `.json()` / `.body` / `.arrayBuffer()` which all return Promise-like values.

Instance Method Summary collapse

Constructor Details

#initialize(window, body:, status: 200, status_text: "", headers: nil, url: "") ⇒ Response

Returns a new instance of Response.



188
189
190
191
192
193
194
195
# File 'lib/dommy/fetch.rb', line 188

def initialize(window, body:, status: 200, status_text: "", headers: nil, url: "")
  @window = window
  @body = body.to_s
  @status = status
  @status_text = status_text.to_s
  @headers = Headers.new(headers || {})
  @url = url.to_s
end

Instance Method Details

#__js_call__(method, _args) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/dommy/fetch.rb', line 218

def __js_call__(method, _args)
  case method
  when "text"
    immediate(@body)
  when "json"
    begin
      immediate(JSON.parse(@body))
    rescue JSON::ParserError => e
      err = ErrorValue.new("JSON parse: #{e.message}")
      rejected(err)
    end

  when "arrayBuffer", "blob"
    immediate(@body)
  when "clone"
    Response.new(
      @window,
      body: @body,
      status: @status,
      status_text: @status_text,
      headers: @headers.to_h,
      url: @url
    )
  end
end

#__js_get__(key) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/dommy/fetch.rb', line 197

def __js_get__(key)
  case key
  when "status"
    @status
  when "ok"
    @status >= 200 && @status < 300
  when "statusText"
    @status_text
  when "url"
    @url
  when "headers"
    @headers
  when "body"
    @body
  end
end

#__js_set__(_key, _value) ⇒ Object



214
215
216
# File 'lib/dommy/fetch.rb', line 214

def __js_set__(_key, _value)
  nil
end