Class: Carrot2

Inherits:
Object
  • Object
show all
Defined in:
lib/carrot2.rb,
lib/carrot2/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

HEADERS =
{
  "Content-Type" => "application/json",
  "Accept" => "application/json"
}
VERSION =
"0.3.0"

Instance Method Summary collapse

Constructor Details

#initialize(url: nil, open_timeout: 3, read_timeout: nil) ⇒ Carrot2

Returns a new instance of Carrot2.



16
17
18
19
20
21
22
23
# 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

Instance Method Details

#cluster(documents, language: nil, algorithm: nil, parameters: nil, template: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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

#listObject



25
26
27
# File 'lib/carrot2.rb', line 25

def list
  get("service/list")
end