Class: Sketchup::Http::Request

Inherits:
Object
  • Object
show all

Overview

Http::Request objects allows you to send HTTP request to HTTP servers.

Version:

  • SketchUp 2017

Instance Method Summary # collapse

Constructor Details

#initialize(url, method) ⇒ Request

Note:

If no reference is kept to the Sketchup::Http::Request, it can be garbage collected, making the download silently fail. This is especially noticeable for larger downloads that takes longer time.

The new method is used to create a new Sketchup::Http::Request.

The default port is 80, to use a different port define it in the URL when creating a new Sketchup::Http::Request.

The method parameter accepts any custom http method or one of the following:

  • Sketchup::Http::GET

  • Sketchup::Http::POST

  • Sketchup::Http::PUT

  • Sketchup::Http::DELETE

  • Sketchup::Http::HEAD

  • Sketchup::Http::OPTIONS

Examples:

@request = Sketchup::Http::Request.new("http://localhost:8080", Sketchup::Http::GET)

@request.start do |request, response|
  puts "body: #{response.body}"
end

Parameters:

  • url (String)

    The targetted URL.

  • method (String)

    Optionally, the request method to use, the default is Sketchup::Http::GET

Version:

  • SketchUp 2017

Instance Method Details

#bodyString

Gets the http body that is going to be used when sending the request.

Examples:

@request = Sketchup::Http::Request.new("http://localhost:8080")

@request.start do |request, response|
  puts "body: #{request.body}"
end

Returns:

Version:

  • SketchUp 2017

#body=(body) ⇒ String

Sets the http body that is going to be used when sending the request.

Examples:

@request = Sketchup::Http::Request.new("http://localhost:8080")
@request.body = "Hello World"

@request.start do |request, response|
  puts "body: #{request.body}"
end

Parameters:

  • body (String)

    A String containing the body.

Returns:

Version:

  • SketchUp 2017

#canceltrue

Cancels the request.

Examples:

@request = Sketchup::Http::Request.new("http://localhost:8080")
@request.start do |request, response|
  puts "body: #{response.body}"
end

@request.cancel

Returns:

  • (true)

Version:

  • SketchUp 2017

#headersHash

Returns the http headers that are going to be used when sending the request.

Examples:

@request = Sketchup::Http::Request.new("http://localhost:8080")
@request.headers = { :key1 => "value1", :key2 => "value2" }

@request.headers.each do |key, value|
  puts "#{key}: #{value}"
end

Returns:

  • (Hash)

Version:

  • SketchUp 2017

#headers=(headers) ⇒ Boolean

Sets the http headers that are going to be used when sending the request.

Examples:

@request = Sketchup::Http::Request.new("http://localhost:8080")
@request.headers = { :key1 => "value1", :key2 => "value2" }

@request.headers.each do |key, value|
  puts "#{key}: #{value}"
end

Parameters:

  • headers (Hash)

    A key/value pair hash.

Returns:

  • (Boolean)

Version:

  • SketchUp 2017

#methodString

Returns the http method that is going to be used when sending the request.

Examples:

@request = Sketchup::Http::Request.new("http://localhost:8080")

@request.start do |request, response|
  puts "request.method: #{request.method}"
end

Returns:

Version:

  • SketchUp 2017

#method=(method) ⇒ Boolean

Sets the http method that is going to be used when sending the request. The value can be any custom http method or one of the following:

  • Sketchup::Http::GET

  • Sketchup::Http::POST

  • Sketchup::Http::PUT

  • Sketchup::Http::DELETE

  • Sketchup::Http::HEAD

  • Sketchup::Http::OPTIONS

Examples:

@request = Sketchup::Http::Request.new("http://localhost:8080")
@request.method = Sketchup::Http::POST

@request.start do |request, response|
  puts "request.method: #{request.method}"
end

Parameters:

  • method (String)

    A string containing the http method name.

Returns:

  • (Boolean)

Version:

  • SketchUp 2017

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

Note:

total is -1 if the server doesn't specify a file size in the response header.

Adds a download progress callback block that will get called everytime we have received data from the server until the download finishes.

Examples:

@request = Sketchup::Http::Request.new("http://localhost:8080")

@request.set_download_progress_callback do |current, total|
  if total == -1
    puts "#{current}B"
  else
    percentage = (current.to_f / total * 100).round
    puts "#{current}B / #{total}B (#{percentage}%)"
  end
end

@request.start

Yields:

  • A block to be invoked everytime data is downloaded from the server.

Yield Parameters:

  • current (Integer)

    Current bytes transferred.

  • total (Integer)

    Total bytes to transfer.

Returns:

  • (Boolean)

Version:

  • SketchUp 2017

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

Adds a upload progress callback block that will get called everytime we have uploaded data to the server until the upload finishes.

Examples:

@request = Sketchup::Http::Request.new("http://localhost:8080")

@request.set_upload_progress_callback do |current, total|
  puts "upload current: #{current}"
  puts "upload total: #{total}"
end

request.start

Yields:

  • A block to be invoked everytime data is sent to the server.

Yield Parameters:

  • current (Integer)

    Current bytes transferred.

  • total (Integer)

    Total bytes to transfer.

Returns:

  • (Boolean)

Version:

  • SketchUp 2017

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

Starts the request and optionally add a callback block.

Examples:

@request = Sketchup::Http::Request.new("http://localhost:8080")

@request.start do |request, response|
  puts "body: #{response.body}"
end

Yields:

  • A block that is invoked when the request has finished.

Yield Parameters:

  • request (Request)

    The request object.

  • response (Response)

    The response object, use this to read the server response.

Returns:

  • (Boolean)

Version:

  • SketchUp 2017

#statusint

Returns the internal status code. It can be one of the following:

  • Sketchup::Http::STATUS_UNKNOWN

  • Sketchup::Http::STATUS_SUCCESS

  • Sketchup::Http::STATUS_PENDING

  • Sketchup::Http::STATUS_CANCELED

  • Sketchup::Http::STATUS_FAILED

Examples:

@request = Sketchup::Http::Request.new("http://localhost:8080")
@request.start
puts "response.status: #{@request.status}"

Returns:

  • (int)

Version:

  • SketchUp 2017

#urlString

Returns a copy of the Request's URL.

Examples:

@request = Sketchup::Http::Request.new("http://localhost:8080")

@request.start do |request, response|
  puts "url: #{request.url}"
end

Returns:

Version:

  • SketchUp 2017