Class: Browser::HTTP::Request
- Includes:
- Event::Target, Native::Wrapper
- Defined in:
- opal/browser/http/request.rb
Constant Summary collapse
- HEADERS =
Default headers.
{ 'X-Requested-With' => 'XMLHttpRequest', 'X-Opal-Version' => RUBY_ENGINE_VERSION, 'Accept' => 'text/javascript, text/html, application/xml, text/xml, */*' }
- STATES =
%w[uninitialized loading loaded interactive complete]
Instance Attribute Summary collapse
-
#headers ⇒ Headers
readonly
The request headers.
-
#method ⇒ Symbol
readonly
The HTTP method for this request.
-
#response ⇒ Response
readonly
The response associated with this request.
-
#url ⇒ String, #to_s
readonly
The URL for this request.
Instance Method Summary collapse
-
#abort ⇒ Object
Abort the request.
-
#asynchronous! ⇒ Object
Make the request asynchronous.
-
#asynchronous? ⇒ Boolean
Check the request is asynchronous.
-
#binary! ⇒ Object
Make the request binary.
-
#binary? ⇒ Boolean
Check the request is binary.
-
#cacheable? ⇒ Boolean
Check if the request is cacheable.
-
#completed? ⇒ Boolean
Check if the request has completed.
-
#content_type(value = nil) ⇒ String
Get or set the Content-Type of the request.
-
#encoding(value = nil) ⇒ String
Get or set the encoding of the request.
-
#initialize {|request| ... } ⇒ Request
constructor
Create a request with the optionally given configuration block.
-
#mime_type(value = nil) ⇒ String
Get or set the MIME type of the request.
-
#no_cache! ⇒ Object
Disable caching for this request.
-
#on(what) {|response| ... } ⇒ Object
Register an event on the request.
-
#open(method = nil, url = nil, asynchronous = nil, user = nil, password = nil) ⇒ self
Open the request.
-
#opened? ⇒ Boolean
Check if the request has been opened.
-
#parameters(hash = nil) ⇒ Hash
Set the request parameters.
-
#password(value = nil) ⇒ String
Get or set the password used for authentication.
-
#query(hash = nil) ⇒ Hash
Set the URI query.
-
#send(parameters = @parameters) ⇒ Response
Send the request with optional parameters.
-
#sent? ⇒ Boolean
Check if the request has been sent.
-
#synchronous! ⇒ Object
Make the request synchronous.
-
#synchronous? ⇒ Boolean
Check the request is synchronous.
- #transport ⇒ Object
-
#user(value = nil) ⇒ String
Get or set the user used for authentication.
Methods included from Event::Target
#off, #on!, #one, #trigger, #trigger!
Constructor Details
permalink #initialize {|request| ... } ⇒ Request
Create a request with the optionally given configuration block.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'opal/browser/http/request.rb', line 37 def initialize(&block) super(transport) @parameters = {} @query = {} @headers = Headers[HEADERS] @method = :get @asynchronous = true @binary = false @cacheable = true @opened = false @sent = false @completed = false @callbacks = Hash.new { |h, k| h[k] = [] } if block.arity == 0 instance_exec(&block) else block.call(self) end if block end |
Instance Attribute Details
permalink #headers ⇒ Headers (readonly)
Returns the request headers.
19 20 21 |
# File 'opal/browser/http/request.rb', line 19 def headers @headers end |
permalink #method ⇒ Symbol (readonly)
Returns the HTTP method for this request.
27 28 29 |
# File 'opal/browser/http/request.rb', line 27 def method @method end |
Instance Method Details
permalink #abort ⇒ Object
Abort the request.
333 334 335 |
# File 'opal/browser/http/request.rb', line 333 def abort `#@native.abort()` end |
permalink #asynchronous! ⇒ Object
Make the request asynchronous.
101 102 103 |
# File 'opal/browser/http/request.rb', line 101 def asynchronous! @asynchronous = true end |
permalink #asynchronous? ⇒ Boolean
Check the request is asynchronous.
91 92 93 |
# File 'opal/browser/http/request.rb', line 91 def asynchronous? @asynchronous end |
permalink #binary! ⇒ Object
Make the request binary.
116 117 118 |
# File 'opal/browser/http/request.rb', line 116 def binary! @binary = true end |
permalink #binary? ⇒ Boolean
Check the request is binary.
111 112 113 |
# File 'opal/browser/http/request.rb', line 111 def binary? @binary end |
permalink #cacheable? ⇒ Boolean
Check if the request is cacheable.
121 122 123 |
# File 'opal/browser/http/request.rb', line 121 def cacheable? @cacheable end |
permalink #completed? ⇒ Boolean
Check if the request has completed.
86 87 88 |
# File 'opal/browser/http/request.rb', line 86 def completed? @completed end |
permalink #content_type(value = nil) ⇒ String
Get or set the Content-Type of the request.
162 163 164 |
# File 'opal/browser/http/request.rb', line 162 def content_type(value = nil) value ? @content_type = value : @content_type end |
permalink #encoding(value = nil) ⇒ String
Get or set the encoding of the request.
171 172 173 |
# File 'opal/browser/http/request.rb', line 171 def encoding(value = nil) value ? @encoding = value : @encoding end |
permalink #mime_type(value = nil) ⇒ String
Get or set the MIME type of the request.
153 154 155 |
# File 'opal/browser/http/request.rb', line 153 def mime_type(value = nil) value ? @mime_type = value : @mime_type end |
permalink #no_cache! ⇒ Object
Disable caching for this request.
126 127 128 |
# File 'opal/browser/http/request.rb', line 126 def no_cache! @cacheable = false end |
permalink #on(what) {|response| ... } ⇒ Object
Register an event on the request.
198 199 200 201 202 203 204 |
# File 'opal/browser/http/request.rb', line 198 def on(what, *, &block) if STATES.include?(what) || %w[success failure].include?(what) || Integer === what @callbacks[what] << block else super end end |
permalink #open(method = nil, url = nil, asynchronous = nil, user = nil, password = nil) ⇒ self
Open the request.
215 216 217 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 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'opal/browser/http/request.rb', line 215 def open(method = nil, url = nil, asynchronous = nil, user = nil, password = nil) raise 'the request has already been opened' if opened? @method = method unless method.nil? @url = url unless url.nil? @asynchronous = asynchronous unless asynchronous.nil? @user = user unless user.nil? @password = password unless password.nil? url = @url # add a dummy random parameter to the query to try circumvent caching unless cacheable? @query[:_] = rand end # add the encoded query to the @url, prepending the right character if # there was already a query in the defined @url or not unless @query.empty? if url.include? ?? url += ?& else url += ?? end url += FormData.build_query(@query) end `#@native.open(#{@method.to_s.upcase}, #{url.to_s}, #{@asynchronous}, #{@user.to_n}, #{@password.to_n})` # if there are no registered callbacks no point in setting the event # handler unless @callbacks.empty? `#@native.onreadystatechange = #{callback}` end @opened = true self end |
permalink #opened? ⇒ Boolean
Check if the request has been opened.
76 77 78 |
# File 'opal/browser/http/request.rb', line 76 def opened? @opened end |
permalink #parameters(hash = nil) ⇒ Hash
Set the request parameters.
180 181 182 |
# File 'opal/browser/http/request.rb', line 180 def parameters(hash = nil) hash ? @parameters = hash : @parameters end |
permalink #password(value = nil) ⇒ String
Get or set the password used for authentication.
144 145 146 |
# File 'opal/browser/http/request.rb', line 144 def password(value = nil) value ? @password = value : @password end |
permalink #query(hash = nil) ⇒ Hash
Set the URI query.
189 190 191 |
# File 'opal/browser/http/request.rb', line 189 def query(hash = nil) hash ? @query = hash : @query end |
permalink #send(parameters = @parameters) ⇒ Response
Send the request with optional parameters.
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'opal/browser/http/request.rb', line 261 def send(parameters = @parameters) raise 'the request has not been opened' unless opened? raise 'the request has already been sent' if sent? # try to circumvent caching setting an If-Modified-Since header with a very # old date unless cacheable? `#@native.setRequestHeader("If-Modified-Since", "Tue, 11 Sep 2001 12:46:00 GMT")` end @headers.each {|name, value| `#@native.setRequestHeader(#{name.to_s}, #{value.to_s})` } if @content_type header = @content_type header += "; charset=#{@encoding}" if @encoding `#@native.setRequestHeader('Content-Type', header)` end if binary? if Buffer.supported? `#@native.responseType = 'arraybuffer'` else `#@native.overrideMimeType('text/plain; charset=x-user-defined')` end end if mime_type && !binary? `#@native.overrideMimeType(#@mime_type)` end @sent = true @response = Response.new(self) if String === parameters data = parameters elsif (Hash === parameters && !parameters.empty?) || FormData === parameters data = if Hash === parameters if FormData.contain_files?(parameters) FormData.build_form_data(parameters) else FormData.build_query(parameters) end else #if FormData === parameters parameters end unless @content_type if FormData === data # I thought it's done this way, but it isn't. It actually is # "multipart/form-data; boundary=-----------.......". Let's miss it # purposefully, because it's filled in automatically in this example. # `#@native.setRequestHeader('Content-Type', 'multipart/form-data')` else `#@native.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')` end end data = data.to_n else data = `null` end `#@native.send(#{data})` @response end |
permalink #sent? ⇒ Boolean
Check if the request has been sent.
81 82 83 |
# File 'opal/browser/http/request.rb', line 81 def sent? @sent end |
permalink #synchronous! ⇒ Object
Make the request synchronous.
106 107 108 |
# File 'opal/browser/http/request.rb', line 106 def synchronous! @asynchronous = false end |
permalink #synchronous? ⇒ Boolean
Check the request is synchronous.
96 97 98 |
# File 'opal/browser/http/request.rb', line 96 def synchronous? !@asynchronous end |
permalink #transport ⇒ Object
62 63 64 |
# File 'opal/browser/http/request.rb', line 62 def transport `new XMLHttpRequest()` end |
permalink #user(value = nil) ⇒ String
Get or set the user used for authentication.
135 136 137 |
# File 'opal/browser/http/request.rb', line 135 def user(value = nil) value ? @user = value : @user end |