class GroupDocs::Api::Request

Attributes

access[RW]

@attr [Hash] access Hash of access credentials

options[RW]

@attr [Hash] options Hash of options

resource[R]

@attr_reader [RestClient::Resource] resource Entry point for all API requests

response[RW]

@attr [String] response Response from server in JSON format

Public Class Methods

new(options = {}) { |options| ... } click to toggle source

Creates new API request.

@example

api = GroupDocs::Api::Request.new do |request|
  request[:method] = :POST
  request[:path] = "/storage/{{client_id}}/info"
end

@param [Hash] options @option options [Symbol, String] :method HTTP method. One of :GET, :DOWNLOAD, :POST, :PUT or :DELETE. @option options [String] :path Path to send request to @option options [Hash] :headers Additional HTTP headers @option options [Hash] :access Access credentials hash @option options [Hash, String, Array, File] :request_body payload @option options [Boolean] :plain Send payload as plain text (i.e. do not convert to JSON). Defaults to false. @option options [Boolean] :sign Should URL be signed. Defaults to true.

@yieldparam [Hash] options

# File lib/groupdocs/api/request.rb, line 38
def initialize(options = {}, &blk)
  @options = options
  yield @options if block_given?
  @options[:access] ||= {}
  @options[:sign] = true if @options[:sign].nil?
  @resource = RestClient::Resource.new(GroupDocs.api_server)
end

Public Instance Methods

execute!() click to toggle source

Executes API request to server.

It performs the following actions step by step:

* Prepends path with version if it's set
* Prepares and signs URL
* Prepare request (add headers, converts payload to JSON, etc.)
* Sends request to server
* Parses response

@return [Hash, String] Parsed response

# File lib/groupdocs/api/request.rb, line 81
def execute!
  prepend_version
  prepare_and_sign_url
  prepare_request
  send_request
  parse_response
end
prepare_and_sign_url() click to toggle source

Prepares, signs and returns absolute URL.

It performs the following actions step by step:

* Parses path (i.e. replaces client ID)
* URL encodes path
* Signs URL

@return [String]

# File lib/groupdocs/api/request.rb, line 56
def prepare_and_sign_url
  unless @signed
    # the order is important here and if client_id is replaced after path
    # is encoded, there might be bad requests
    replace_client_id if @options[:sign]
    url_encode_path
    sign_url          if @options[:sign]
    @signed = true
  end

  options[:path]
end