class Conclas::Api

module to abstract request

Public Class Methods

new(token, timeout=60, use_ssl=false) click to toggle source
# File lib/rb_conclas/api.rb, line 22
def initialize(token, timeout=60, use_ssl=false)
  @timeout = timeout
  if Utils::StringHelper.is_empty token
    raise EmptyParameterException, "The parameter 'token' needs a value"
  end
  @token = token
  @use_ssl = use_ssl
  @status_code = nil
  @headers = nil
  @result = nil
  @req = nil
end

Public Instance Methods

check_request() click to toggle source
# File lib/rb_conclas/api.rb, line 67
def check_request
  if @req.code.to_i.between?(400, 499)
    raise Exceptions::HTTPError, "Client Error #{@req.code}(#{@req.message})."
  elsif @req.code.to_i.between?(500, 600)
    raise Exceptions::HTTPError, "Server Error #{@req.code}(#{@req.message})."
  else
    true
  end
end
direct_categorise(contents) click to toggle source

main methods

# File lib/rb_conclas/api.rb, line 108
def direct_categorise(contents)
  check_api_args_method(contents)
  hashe_data = {:contents => contents}
  #if !start_from_category.nil?
  #  hashe_data[:start_from_category] = start_from_category
  #end
  json_body_request = Utils::JsonMaker.new(hashe_data).get_json
  url = BASE_URL + METHODS[:direct]
  make_request(url, json_body_request)
end
headers() click to toggle source
# File lib/rb_conclas/api.rb, line 40
def headers
  @headers
end
indirect_categorise(contents, callback) click to toggle source
# File lib/rb_conclas/api.rb, line 119
def indirect_categorise(contents, callback)
  check_api_args_method(contents, callback)
  hashe_data = {:contents => contents, :callback => callback}
  #if !start_from_category.nil?
  #  hashe_data[:start_from_category] = start_from_category
  #end
  json_body_request = Utils::JsonMaker.new(hashe_data).get_json
  url = BASE_URL + METHODS[:indirect]
  make_request(url, json_body_request)
end
result() click to toggle source
# File lib/rb_conclas/api.rb, line 44
def result
  @result
end
status_code() click to toggle source

properties

# File lib/rb_conclas/api.rb, line 36
def status_code
  @status_code
end

Private Instance Methods

check_api_args_method(contents, start_from_category=nil, callback=nil) click to toggle source
# File lib/rb_conclas/api.rb, line 83
def check_api_args_method(contents, start_from_category=nil, callback=nil)
  if contents.length == 0 || !contents.is_a?(Array)
    raise InvalidParameterException, "The parameter 'contents' needs a value"
  end
  
  if contents.length > 1000
    raise DocMaxException, "The content size exceeded the limit."
  end
  #if !start_from_category.nil?
  #  if Utils::StringHelper.is_empty start_from_category
  #    raise Exceptions::InvalidParameterException, "The parameter 'start_from_category' needs a value"
  #  end
  #end

  if !callback.nil?
    if Utils::StringHelper.is_empty callback
      raise Exceptions::InvalidParameterException, "The parameter 'callback' needs a value"
    end
  end
end
make_request(url, data) click to toggle source

private methods

# File lib/rb_conclas/api.rb, line 49
def make_request(url, data)
  headers = {"Content-Type" => "application/json",
              "Authorization" => @token}
  @req = Core::Requester.post(url, data, headers, @timeout, @use_ssl)

  if check_request

    response = Core::ConclasResponse.new(
      @req.body, 
      @req.code, 
      @req.header.to_hash.inspect)

    if response.request_successful?
      populate_properties
    end
  end
end
populate_properties() click to toggle source
# File lib/rb_conclas/api.rb, line 77
def populate_properties
  @status_code = @req.code
  @headers = @req.header.to_hash.inspect
  @result = JSON.parse(@req.body, :quirks_mode => true)
end