class Carrot2
Constants
- HEADERS
- VERSION
Public Class Methods
new(url: nil, open_timeout: 3, read_timeout: nil)
click to toggle source
# File lib/carrot2.rb, line 16 def initialize(url: nil, open_timeout: 3, read_timeout: nil) url ||= ENV["CARROT2_URL"] || "http://localhost:8080" @uri = URI.parse(url) @http = Net::HTTP.new(@uri.host, @uri.port) @http.use_ssl = true if @uri.scheme == "https" @http.open_timeout = open_timeout if open_timeout @http.read_timeout = read_timeout if read_timeout end
Public Instance Methods
cluster(documents, language: nil, algorithm: nil, parameters: nil, template: nil)
click to toggle source
# File lib/carrot2.rb, line 29 def cluster(documents, language: nil, algorithm: nil, parameters: nil, template: nil) # no defaults if template unless template language ||= "English" algorithm ||= "Lingo" parameters ||= {} end # data data = { documents: documents.map { |v| v.is_a?(String) ? {field: v} : v } } data[:language] = language if language data[:algorithm] = algorithm if algorithm data[:parameters] = parameters if parameters # path path = "service/cluster" path = "#{path}?#{URI.encode_www_form(template: template)}" if template post(path, data) end
list()
click to toggle source
# File lib/carrot2.rb, line 25 def list get("service/list") end
Private Instance Methods
get(path)
click to toggle source
# File lib/carrot2.rb, line 54 def get(path) handle_response do @http.get("#{@uri.request_uri.chomp("/")}/#{path}", HEADERS) end end
handle_response() { || ... }
click to toggle source
# File lib/carrot2.rb, line 66 def handle_response begin response = yield rescue Errno::ECONNREFUSED => e raise Carrot2::Error, e.message end unless response.kind_of?(Net::HTTPSuccess) body = JSON.parse(response.body) rescue {} message = body["message"] || "Bad response: #{response.code}" raise Carrot2::Error, message end JSON.parse(response.body) end
post(path, data)
click to toggle source
# File lib/carrot2.rb, line 60 def post(path, data) handle_response do @http.post("#{@uri.request_uri.chomp("/")}/#{path}", data.to_json, HEADERS) end end