Class: Browser::HTTP::Request

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

Instance Method Summary collapse

Methods included from Event::Target

#off, #on!, #one, #trigger, #trigger!

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



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

#headersHeaders (readonly)

Returns the request headers.

Returns:

  • (Headers)

    the request headers



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

def headers
  @headers
end

#methodSymbol (readonly)

Returns the HTTP method for this request.

Returns:

  • (Symbol)

    the HTTP method for this request



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

def method
  @method
end

#responseResponse (readonly)

Returns the response associated with this request.

Returns:

  • (Response)

    the response associated with this request



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

def response
  @response
end

#urlString, #to_s (readonly)

Returns the URL for this request.

Returns:

  • (String, #to_s)

    the URL for this request



31
32
33
# File 'opal/browser/http/request.rb', line 31

def url
  @url
end

Instance Method Details

#abortObject

Abort the request.



333
334
335
# File 'opal/browser/http/request.rb', line 333

def abort
  `#@native.abort()`
end

#asynchronous!Object

Make the request asynchronous.



101
102
103
# File 'opal/browser/http/request.rb', line 101

def asynchronous!
  @asynchronous = true
end

#asynchronous?Boolean

Check the request is asynchronous.

Returns:

  • (Boolean)


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

def asynchronous?
  @asynchronous
end

#binary!Object

Make the request binary.



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

def binary!
  @binary = true
end

#binary?Boolean

Check the request is binary.

Returns:

  • (Boolean)


111
112
113
# File 'opal/browser/http/request.rb', line 111

def binary?
  @binary
end

#cacheable?Boolean

Check if the request is cacheable.

Returns:

  • (Boolean)


121
122
123
# File 'opal/browser/http/request.rb', line 121

def cacheable?
  @cacheable
end

#completed?Boolean

Check if the request has completed.

Returns:

  • (Boolean)


86
87
88
# File 'opal/browser/http/request.rb', line 86

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:



162
163
164
# File 'opal/browser/http/request.rb', line 162

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:



171
172
173
# File 'opal/browser/http/request.rb', line 171

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:



153
154
155
# File 'opal/browser/http/request.rb', line 153

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

#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

#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



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

#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)


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

#opened?Boolean

Check if the request has been opened.

Returns:

  • (Boolean)


76
77
78
# File 'opal/browser/http/request.rb', line 76

def opened?
  @opened
end

#parameters(hash = nil) ⇒ Hash

Set the request parameters.

Parameters:

  • hash (Hash) (defaults to: nil)

    the parameters

Returns:



180
181
182
# File 'opal/browser/http/request.rb', line 180

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:



144
145
146
# File 'opal/browser/http/request.rb', line 144

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:



189
190
191
# File 'opal/browser/http/request.rb', line 189

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:



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

#sent?Boolean

Check if the request has been sent.

Returns:

  • (Boolean)


81
82
83
# File 'opal/browser/http/request.rb', line 81

def sent?
  @sent
end

#synchronous!Object

Make the request synchronous.



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

def synchronous!
  @asynchronous = false
end

#synchronous?Boolean

Check the request is synchronous.

Returns:

  • (Boolean)


96
97
98
# File 'opal/browser/http/request.rb', line 96

def synchronous?
  !@asynchronous
end

#transportObject

Raises:

  • (NotImplementedError)


62
63
64
# File 'opal/browser/http/request.rb', line 62

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:



135
136
137
# File 'opal/browser/http/request.rb', line 135

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