Class: Dommy::Request

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

Overview

‘Request` polyfill — minimal Fetch API Request object so callers constructing `new Request(url, init)` get a value with `.url`, `.method`, `.headers`, `.body`. The stub-based `fetch` doesn’t consume it directly (it still takes ‘(url, init)`), but having Request available means JS code that constructs one before passing to fetch keeps working.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, init = nil) ⇒ Request

Returns a new instance of Request.



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/dommy/fetch.rb', line 130

def initialize(url, init = nil)
  opts = init.is_a?(Hash) ? init : {}
  @url = url.to_s
  @method = (opts["method"] || opts[:method] || "GET").to_s.upcase
  @body = opts["body"] || opts[:body]
  raw_headers = opts["headers"] || opts[:headers] || {}
  @headers = Headers.new(raw_headers)
  @credentials = (opts["credentials"] || opts[:credentials] || "same-origin").to_s
  @mode = (opts["mode"] || opts[:mode] || "cors").to_s
  @cache = (opts["cache"] || opts[:cache] || "default").to_s
  @redirect = (opts["redirect"] || opts[:redirect] || "follow").to_s
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



128
129
130
# File 'lib/dommy/fetch.rb', line 128

def body
  @body
end

#cacheObject (readonly)

Returns the value of attribute cache.



143
144
145
# File 'lib/dommy/fetch.rb', line 143

def cache
  @cache
end

#credentialsObject (readonly)

Returns the value of attribute credentials.



143
144
145
# File 'lib/dommy/fetch.rb', line 143

def credentials
  @credentials
end

#headersObject (readonly)

Returns the value of attribute headers.



143
144
145
# File 'lib/dommy/fetch.rb', line 143

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



128
129
130
# File 'lib/dommy/fetch.rb', line 128

def method
  @method
end

#modeObject (readonly)

Returns the value of attribute mode.



143
144
145
# File 'lib/dommy/fetch.rb', line 143

def mode
  @mode
end

#redirectObject (readonly)

Returns the value of attribute redirect.



143
144
145
# File 'lib/dommy/fetch.rb', line 143

def redirect
  @redirect
end

#urlObject (readonly)

Returns the value of attribute url.



128
129
130
# File 'lib/dommy/fetch.rb', line 128

def url
  @url
end

Instance Method Details

#__js_call__(method, _args) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/dommy/fetch.rb', line 166

def __js_call__(method, _args)
  case method
  when "clone"
    Request.new(
      @url,
      "method" => @method,
      "body" => @body,
      "headers" => @headers.to_h,
      "credentials" => @credentials,
      "mode" => @mode,
      "cache" => @cache,
      "redirect" => @redirect
    )
  end
end

#__js_get__(key) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/dommy/fetch.rb', line 145

def __js_get__(key)
  case key
  when "url"
    @url
  when "method"
    @method
  when "headers"
    @headers
  when "body"
    @body
  when "credentials"
    @credentials
  when "mode"
    @mode
  when "cache"
    @cache
  when "redirect"
    @redirect
  end
end