ModraUp Ruby API Documentation Ruby API Reference

Class: ModraUp::Http::Request

Inherits:
Object
  • Object
show all

Overview

Asynchronous HTTP request. Request#start takes an optional block that is called in the Ruby thread after completion. |request, response|.

SketchUp's official implementation may fail due to GC silent failures.

Note: ModraUp extension. Uncompleted requests will intern, and the plugin will still callback even if it drops the reference.

Examples:

@request = ModraUp::Http::Request.new("http://127.0.0.1:8080")
@request.start do |request, response|
  puts response.body
end

Class Method Summary # collapse

Instance Method Summary # collapse

Class Method Details

Request.new(url, method = ModraUp::Http::GET) ⇒ request

The default port is 80; other ports should be specified in the url. The method can be a custom verb or ModraUp::Http::GET / POST / PUT / DELETE / HEAD / OPTIONS / PATCH.

Parameters:

  • url (String)
  • method (String)

Returns:

  • (ModraUp::Http::Request)

Examples:

ModraUp::Http::Request.new("http://127.0.0.1:8080", ModraUp::Http::GET)

Instance Method Details

request.url ⇒ String

Copy the URL request.

Returns:

  • (String)

Examples:

ModraUp::Http::Request.new("http://127.0.0.1:8080").url

request.body ⇒ String

The body of the request to be sent.

Returns:

  • (String)

Examples:

req = ModraUp::Http::Request.new("http://127.0.0.1:8080")
req.body = "hello"
req.body  #=> "hello"

request.body=(body) ⇒ String

Set the request body.

Parameters:

  • body (String)

Returns:

  • (String)

Examples:

req = ModraUp::Http::Request.new("http://127.0.0.1:8080")
req.body = "hello"

request.headers ⇒ Hash

The headers to be sent in the request.

Returns:

  • (Hash)

Examples:

req = ModraUp::Http::Request.new("http://127.0.0.1:8080")
req.headers = { "X-Test" => "1" }
req.headers["X-Test"]  #=> "1"

request.headers=(headers) ⇒ Boolean

Set the request headers. Keys will be to_s.

Parameters:

  • headers (Hash)

Returns:

  • (Boolean)

Raises:

  • [TypeError] not a Hash

Examples:

req = ModraUp::Http::Request.new("http://127.0.0.1:8080")
req.headers = { :Accept => "text/plain" }

request.method ⇒ String

The HTTP method to be used. This will override Object#method.

Returns:

  • (String)

Examples:

ModraUp::Http::Request.new("http://127.0.0.1:8080").method  #=> "GET"

request.method=(method) ⇒ Boolean

Set the HTTP method.

Parameters:

  • method (String)

Returns:

  • (Boolean)

Examples:

req = ModraUp::Http::Request.new("http://127.0.0.1:8080")
req.method = ModraUp::Http::POST

request.status ⇒ Integer

Internal status: ModraUp::Http::STATUS_UNKNOWN / STATUS_SUCCESS / STATUS_PENDING / STATUS_CANCELED / STATUS_FAILED.

Returns:

  • (Integer)

Examples:

ModraUp::Http::Request.new("http://127.0.0.1:8080").status

request.cancel ⇒ true

Cancel the request. Do not call within the completion callback.

Returns:

  • (true)

Examples:

req = ModraUp::Http::Request.new("http://127.0.0.1:8080")
req.start
req.cancel

request.set_download_progress_callback {|current, total| ... } ⇒ Boolean

Download progress. If the server does not provide a length, total is set to -1.

Returns:

  • (Boolean)

Yields:

  • @yieldparam [Integer] current bytes_received
  • @yieldparam [Integer] total Total bytes, -1 if unknown

Examples:

req = ModraUp::Http::Request.new("http://127.0.0.1:8080")
req.set_download_progress_callback { |current, total| [current, total] }

request.set_upload_progress_callback {|current, total| ... } ⇒ Boolean

Upload progress.

Returns:

  • (Boolean)

Yields:

  • @yieldparam [Integer] current bytes_sent
  • @yieldparam [Integer] total Total bytes, -1 if unknown

Examples:

req = ModraUp::Http::Request.new("http://127.0.0.1:8080")
req.set_upload_progress_callback { |current, total| [current, total] }

request.start {|request, response| ... } ⇒ Booleanrequest.start ⇒ Boolean

Start the request. Returns false if already in progress or URL is empty. Do not call #cancel from the callback.

Returns:

  • (Boolean)

Yields:

  • @yieldparam [ModraUp::Http::Request] request
  • @yieldparam [ModraUp::Http::Response] response

Examples:

@request = ModraUp::Http::Request.new("http://127.0.0.1:8080")
@request.start { |request, response| puts response.body }