class ConClas::Api
module to abstract request
Public Class Methods
new(token, timeout=60, use_ssl=false)
click to toggle source
# File lib/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 @req = nil end
Public Instance Methods
check_request()
click to toggle source
# File lib/conclas/api.rb, line 61 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_classify(contents)
click to toggle source
main methods
# File lib/conclas/api.rb, line 101 def direct_classify(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) JSON.parse(@req.body, :quirks_mode => true) end
headers()
click to toggle source
# File lib/conclas/api.rb, line 39 def headers @headers end
indirect_classify(contents, callback)
click to toggle source
# File lib/conclas/api.rb, line 113 def indirect_classify(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) JSON.parse(@req.body, :quirks_mode => true) end
status_code()
click to toggle source
properties
# File lib/conclas/api.rb, line 35 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/conclas/api.rb, line 76 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/conclas/api.rb, line 44 def make_request(url, data) headers = {"Content-Type" => "application/json", "Authorization" => @token} @req = Core::Requester.post(url, data, headers, @timeout, @use_ssl) response = Core::ConClasResponse.new( @req.body, @req.code, @req.header.to_hash.inspect) if response.request_successful? if check_request populate_properties end end end
populate_properties()
click to toggle source
# File lib/conclas/api.rb, line 71 def populate_properties @status_code = @req.code @headers = @req.header.to_hash.inspect end