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.



126
127
128
129
130
131
132
133
# File 'lib/dommy/fetch.rb', line 126

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



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/dommy/fetch.rb', line 156

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



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/dommy/fetch.rb', line 135

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



152
153
154
# File 'lib/dommy/fetch.rb', line 152

def __js_set__(_key, _value)
  nil
end