Class: Browser::HTTP::Request

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize {|request| ... } ⇒ Request

Create a request with the optionally given configuration block.

Yields:

  • (request)

    if the block has a parameter the request is passed otherwise it's instance_exec'd


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

#headersHeaders (readonly)

Returns the request headers.

Returns:

  • (Headers)

    the request headers


15
16
17
# File 'opal/browser/http/request.rb', line 15

def headers
  @headers
end

#methodSymbol (readonly)

Returns the HTTP method for this request.

Returns:

  • (Symbol)

    the HTTP method for this request


23
24
25
# File 'opal/browser/http/request.rb', line 23

def method
  @method
end

#responseResponse (readonly)

Returns the response associated with this request.

Returns:

  • (Response)

    the response associated with this request


19
20
21
# File 'opal/browser/http/request.rb', line 19

def response
  @response
end

#urlString, #to_s (readonly)

Returns the URL for this request.

Returns:

  • (String, #to_s)

    the URL for this request


27
28
29
# File 'opal/browser/http/request.rb', line 27

def url
  @url
end

Instance Method Details

#abortObject

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.

Returns:

  • (Boolean)

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.

Returns:

  • (Boolean)

107
108
109
# File 'opal/browser/http/request.rb', line 107

def binary?
  @binary
end

#cacheable?Boolean

Check if the request is cacheable.

Returns:

  • (Boolean)

117
118
119
# File 'opal/browser/http/request.rb', line 117

def cacheable?
  @cacheable
end

#completed?Boolean

Check if the request has completed.

Returns:

  • (Boolean)

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.

Parameters:

  • value (String) (defaults to: nil)

    when passed it sets, when omitted it gets

Returns:


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.

Parameters:

  • value (String) (defaults to: nil)

    when passed it sets, when omitted it gets

Returns:


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.

Parameters:

  • value (String) (defaults to: nil)

    when passed it sets, when omitted it gets

Returns:


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.

Parameters:

  • what (Symbol, String)

    the event name

Yield Parameters:

  • response (Response)

    the response for the event


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.

Parameters:

  • method (Symbol) (defaults to: nil)

    the HTTP method to use

  • url (String, #to_s) (defaults to: nil)

    the URL to send the request to

  • asynchronous (Boolean) (defaults to: nil)

    whether the request is asynchronous or not

  • user (String) (defaults to: nil)

    the user to use for authentication

  • password (String) (defaults to: nil)

    the password to use for authentication

Returns:

  • (self)

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.

Returns:

  • (Boolean)

72
73
74
# File 'opal/browser/http/request.rb', line 72

def opened?
  @opened
end

#parameters(hash = nil) ⇒ Hash

Set the request parameters.

Parameters:

  • hash (Hash) (defaults to: nil)

    the parameters

Returns:


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.

Parameters:

  • value (String) (defaults to: nil)

    when passed it sets, when omitted it gets

Returns:


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.

Parameters:

  • hash (Hash) (defaults to: nil)

    the query

Returns:


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.

Parameters:

  • parameters (String, Hash) (defaults to: @parameters)

    the data to send

Returns:


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.

Returns:

  • (Boolean)

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.

Returns:

  • (Boolean)

92
93
94
# File 'opal/browser/http/request.rb', line 92

def synchronous?
  !@asynchronous
end

#transportObject

Raises:

  • (NotImplementedError)

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.

Parameters:

  • value (String) (defaults to: nil)

    when passed it sets, when omitted it gets

Returns:


131
132
133
# File 'opal/browser/http/request.rb', line 131

def user(value = nil)
  value ? @user = value : @user
end