module GroupDocs::Api::Helpers::REST

Constants

DEFAULT_HEADERS

Private Instance Methods

parse_response() click to toggle source

Parses response from API server.

@api private

# File lib/groupdocs/api/helpers/rest_helper.rb, line 77
def parse_response
  # for DOWNLOAD requests, just return response
  if options[:method] == :download
    response
  # for all other requests, parse JSON
  else
    json = JSON.parse(response, :symbolize_names => true)
    json[:status] == 'Ok' ? json[:result] : raise_bad_request_error(json)
  end
end
prepare_request() click to toggle source

Prepares headers, method and payload for request.

@api private

# File lib/groupdocs/api/helpers/rest_helper.rb, line 22
def prepare_request
  if options[:headers].is_a?(Hash)
    options[:headers].merge!(DEFAULT_HEADERS)
  else
    options[:headers] = DEFAULT_HEADERS.dup
  end

  options[:method] = options[:method].to_s.downcase.to_sym

  if options[:request_body] && !options[:request_body].is_a?(Object::File)
    unless options[:plain]
      options[:request_body] = options[:request_body].to_json
      options[:headers][:content_type]= 'application/json'
    end
    options[:headers][:content_length] = options[:request_body].length
  end
end
raise_bad_request_error(json) click to toggle source

@raise [GroupDocs::BadResponseError] @api private

# File lib/groupdocs/api/helpers/rest_helper.rb, line 92
def raise_bad_request_error(json)
  raise BadResponseError, json[:error_message]
end
send_request() click to toggle source

Changed in release 1.5.8

Sends request to API server.

@api private

# File lib/groupdocs/api/helpers/rest_helper.rb, line 47
def send_request

  self.response = case options[:method]
    when :get, :download
      resource[options[:path]].get(options[:headers])
    when :post
      resource[options[:path]].post(options[:request_body], options[:headers])
    when :put
      resource[options[:path]].put(options[:request_body], options[:headers])
    when :delete
      if options[:request_body]
      url = GroupDocs.api_server + options[:path]
      RestClient::Request.execute(options.merge(
                    :method => :delete,
                    :url => url,
                    :payload => options[:request_body],
                    :headers => options[:headers]))
      else
        resource[options[:path]].delete(options[:headers])
      end
    else
      raise UnsupportedMethodError, "Unsupported HTTP method: #{options[:method].inspect}"
  end
end