Class: Dommy::Request
- Inherits:
-
Object
- Object
- Dommy::Request
- 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
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#credentials ⇒ Object
readonly
Returns the value of attribute credentials.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#redirect ⇒ Object
readonly
Returns the value of attribute redirect.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #__js_call__(method, _args) ⇒ Object
- #__js_get__(key) ⇒ Object
-
#initialize(url, init = nil) ⇒ Request
constructor
A new instance of Request.
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
#body ⇒ Object (readonly)
Returns the value of attribute body.
128 129 130 |
# File 'lib/dommy/fetch.rb', line 128 def body @body end |
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
143 144 145 |
# File 'lib/dommy/fetch.rb', line 143 def cache @cache end |
#credentials ⇒ Object (readonly)
Returns the value of attribute credentials.
143 144 145 |
# File 'lib/dommy/fetch.rb', line 143 def credentials @credentials end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
143 144 145 |
# File 'lib/dommy/fetch.rb', line 143 def headers @headers end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
128 129 130 |
# File 'lib/dommy/fetch.rb', line 128 def method @method end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
143 144 145 |
# File 'lib/dommy/fetch.rb', line 143 def mode @mode end |
#redirect ⇒ Object (readonly)
Returns the value of attribute redirect.
143 144 145 |
# File 'lib/dommy/fetch.rb', line 143 def redirect @redirect end |
#url ⇒ Object (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 |