Class: Browser::HTTP::Request
- Includes:
- Native
- 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, */*' }
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.
Constructor Details
#initialize {|request| ... } ⇒ Request
Create a request with the optionally given configuration block.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'opal/browser/http/request.rb', line 33 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
#headers ⇒ Headers (readonly)
Returns the request headers.
15 16 17 |
# File 'opal/browser/http/request.rb', line 15 def headers @headers end |
#method ⇒ Symbol (readonly)
Returns the HTTP method for this request.
23 24 25 |
# File 'opal/browser/http/request.rb', line 23 def method @method end |
#response ⇒ Response (readonly)
Returns the response associated with this request.
19 20 21 |
# File 'opal/browser/http/request.rb', line 19 def response @response end |
#url ⇒ String, #to_s (readonly)
Returns the URL for this request.
27 28 29 |
# File 'opal/browser/http/request.rb', line 27 def url @url end |
Instance Method Details
#abort ⇒ Object
Abort the request.
310 311 312 |
# File 'opal/browser/http/request.rb', line 310 def abort `#@native.abort()` end |
#asynchronous! ⇒ Object
Make the request asynchronous.
97 98 99 |
# File 'opal/browser/http/request.rb', line 97 def asynchronous! @asynchronous = true end |
#asynchronous? ⇒ Boolean
Check the request is asynchronous.
87 88 89 |
# File 'opal/browser/http/request.rb', line 87 def asynchronous? @asynchronous end |
#binary! ⇒ Object
Make the request binary.
112 113 114 |
# File 'opal/browser/http/request.rb', line 112 def binary! @binary = true end |
#binary? ⇒ Boolean
Check the request is binary.
107 108 109 |
# File 'opal/browser/http/request.rb', line 107 def binary? @binary end |
#cacheable? ⇒ Boolean
Check if the request is cacheable.
117 118 119 |
# File 'opal/browser/http/request.rb', line 117 def cacheable? @cacheable end |
#completed? ⇒ Boolean
Check if the request has completed.
82 83 84 |
# File 'opal/browser/http/request.rb', line 82 def completed? @completed end |
#content_type(value = nil) ⇒ String
Get or set the Content-Type of the request.
158 159 160 |
# File 'opal/browser/http/request.rb', line 158 def content_type(value = nil) value ? @content_type = value : @content_type end |
#encoding(value = nil) ⇒ String
Get or set the encoding of the request.
167 168 169 |
# File 'opal/browser/http/request.rb', line 167 def encoding(value = nil) value ? @encoding = value : @encoding end |
#mime_type(value = nil) ⇒ String
Get or set the MIME type of the request.
149 150 151 |
# File 'opal/browser/http/request.rb', line 149 def mime_type(value = nil) value ? @mime_type = value : @mime_type end |
#no_cache! ⇒ Object
Disable caching for this request.
122 123 124 |
# File 'opal/browser/http/request.rb', line 122 def no_cache! @cacheable = false end |
#on(what) {|response| ... } ⇒ Object
Register an event on the request.
194 195 196 |
# File 'opal/browser/http/request.rb', line 194 def on(what, &block) @callbacks[what] << block end |
#open(method = nil, url = nil, asynchronous = nil, user = nil, password = nil) ⇒ self
Open the request.
207 208 209 210 211 212 213 214 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 |
# File 'opal/browser/http/request.rb', line 207 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 += @query.encode_uri 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 |
#opened? ⇒ Boolean
Check if the request has been opened.
72 73 74 |
# File 'opal/browser/http/request.rb', line 72 def opened? @opened end |
#parameters(hash = nil) ⇒ Hash
Set the request parameters.
176 177 178 |
# File 'opal/browser/http/request.rb', line 176 def parameters(hash = nil) hash ? @parameters = hash : @parameters end |
#password(value = nil) ⇒ String
Get or set the password used for authentication.
140 141 142 |
# File 'opal/browser/http/request.rb', line 140 def password(value = nil) value ? @password = value : @password end |
#query(hash = nil) ⇒ Hash
Set the URI query.
185 186 187 |
# File 'opal/browser/http/request.rb', line 185 def query(hash = nil) hash ? @query = hash : @query end |
#send(parameters = @parameters) ⇒ Response
Send the request with optional parameters.
253 254 255 256 257 258 259 260 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 |
# File 'opal/browser/http/request.rb', line 253 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? data = parameters.map {|vals| vals.map(&:encode_uri_component).join(?=) }.join(?&) unless @content_type `#@native.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')` end else data = `null` end `#@native.send(#{data})` @response end |
#sent? ⇒ Boolean
Check if the request has been sent.
77 78 79 |
# File 'opal/browser/http/request.rb', line 77 def sent? @sent end |
#synchronous! ⇒ Object
Make the request synchronous.
102 103 104 |
# File 'opal/browser/http/request.rb', line 102 def synchronous! @asynchronous = false end |
#synchronous? ⇒ Boolean
Check the request is synchronous.
92 93 94 |
# File 'opal/browser/http/request.rb', line 92 def synchronous? !@asynchronous end |
#transport ⇒ Object
58 59 60 |
# File 'opal/browser/http/request.rb', line 58 def transport `new XMLHttpRequest()` end |
#user(value = nil) ⇒ String
Get or set the user used for authentication.
131 132 133 |
# File 'opal/browser/http/request.rb', line 131 def user(value = nil) value ? @user = value : @user end |