carrot2 0.3.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f7c82346a7d8fedbd0b875e1a755d5a16a1a2a28c680e7f71c1b7bebe9603ef8
4
- data.tar.gz: b6493deebcce2d05039ba1c6546e4725f4fbc52d0d35f15583d1d49e6e9d086a
3
+ metadata.gz: e7d773ee688b36ada2bbf8e9a0995b7fc0e4de31a8f9efdcf5eb2fa339618bc2
4
+ data.tar.gz: 2cb79abc0d6d171a5dbc4c3b43fdfdb8584e26f662c2d73290b3397610d45a5b
5
5
  SHA512:
6
- metadata.gz: 8587ddd05ff3d8c2d491b25c7a10bea228b92a198d18bdd788c03f44c40a89bdffa39cd7ac65b2ec59828c043cbceb4c2a127f2df961c83733440952e9201c23
7
- data.tar.gz: f91d543e01915d25586b874970996f50c54a25ac4fd39d5a0a0bfb1a3e1aec84c992b60edea4acfc6073ffef5edef969bbce0174d7f32845bdf6d420a3b66596
6
+ metadata.gz: ac3db2a5e50d70846d4b67f515ec2e5d3f8d9671080ec92da482d27a5479bfe86b19e1554e32350f984f7f452fdf8eb2de09ef54a0d22729a1c45a9cd1382536
7
+ data.tar.gz: f781ba26e50e70b53d8fcbdbcc1641112397471cf17eee88ca5e5306cb23fcb6385246aca48c28ca76583d40df6a855673a4eecc62ead65f0e95816a765308ab
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## 0.5.0 (2024-10-22)
2
+
3
+ - Dropped support for Ruby < 3.1
4
+
5
+ ## 0.4.0 (2022-08-28)
6
+
7
+ - Dropped support for Ruby < 2.7
8
+
1
9
  ## 0.3.0 (2020-07-29)
2
10
 
3
11
  - Added support for Carrot2 4
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012-2020 Andrew Kane
1
+ Copyright (c) 2012-2023 Andrew Kane
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # Carrot2
1
+ # Carrot2 Ruby
2
2
 
3
3
  Ruby client for [Carrot2](https://github.com/carrot2/carrot2) - the open-source document clustering server
4
4
 
5
- [![Build Status](https://travis-ci.org/ankane/carrot2.svg?branch=master)](https://travis-ci.org/ankane/carrot2)
5
+ [![Build Status](https://github.com/ankane/carrot2-ruby/actions/workflows/build.yml/badge.svg)](https://github.com/ankane/carrot2-ruby/actions)
6
6
 
7
7
  ## Installation
8
8
 
@@ -16,10 +16,10 @@ brew services start carrot2
16
16
  Then add this line to your application’s Gemfile:
17
17
 
18
18
  ```ruby
19
- gem 'carrot2'
19
+ gem "carrot2"
20
20
  ```
21
21
 
22
- The latest version works with Carrot2 4. For Carrot2 3, use version 0.2.1 and [this readme](https://github.com/ankane/carrot2/blob/v0.2.1/README.md).
22
+ The latest version works with Carrot2 4. For Carrot2 3, use version 0.2.1 and [this readme](https://github.com/ankane/carrot2-ruby/blob/v0.2.1/README.md).
23
23
 
24
24
  ## How to Use
25
25
 
@@ -33,7 +33,7 @@ documents = [
33
33
  "This is completely unrelated to the other documents."
34
34
  ]
35
35
 
36
- carrot2 = Carrot2.new
36
+ carrot2 = Carrot2::Client.new
37
37
  carrot2.cluster(documents)
38
38
  ```
39
39
 
@@ -103,13 +103,13 @@ carrot2.cluster(documents, template: "lingo")
103
103
  To specify the Carrot2 server, set `ENV["CARROT2_URL"]` or use:
104
104
 
105
105
  ```ruby
106
- Carrot2.new(url: "http://localhost:8080")
106
+ Carrot2::Client.new(url: "http://localhost:8080")
107
107
  ```
108
108
 
109
109
  Set timeouts
110
110
 
111
111
  ```ruby
112
- Carrot2.new(open_timeout: 3, read_timeout: 5)
112
+ Carrot2::Client.new(open_timeout: 3, read_timeout: 5)
113
113
  ```
114
114
 
115
115
  ## Resources
@@ -118,22 +118,22 @@ Carrot2.new(open_timeout: 3, read_timeout: 5)
118
118
 
119
119
  ## History
120
120
 
121
- View the [changelog](https://github.com/ankane/carrot2/blob/master/CHANGELOG.md)
121
+ View the [changelog](https://github.com/ankane/carrot2-ruby/blob/master/CHANGELOG.md)
122
122
 
123
123
  ## Contributing
124
124
 
125
125
  Everyone is encouraged to help improve this project. Here are a few ways you can help:
126
126
 
127
- - [Report bugs](https://github.com/ankane/carrot2/issues)
128
- - Fix bugs and [submit pull requests](https://github.com/ankane/carrot2/pulls)
127
+ - [Report bugs](https://github.com/ankane/carrot2-ruby/issues)
128
+ - Fix bugs and [submit pull requests](https://github.com/ankane/carrot2-ruby/pulls)
129
129
  - Write, clarify, or fix documentation
130
130
  - Suggest or add new features
131
131
 
132
132
  To get started with development:
133
133
 
134
134
  ```sh
135
- git clone https://github.com/ankane/carrot2.git
136
- cd carrot2
135
+ git clone https://github.com/ankane/carrot2-ruby.git
136
+ cd carrot2-ruby
137
137
  bundle install
138
138
  bundle exec rake test
139
139
  ```
@@ -0,0 +1,74 @@
1
+ module Carrot2
2
+ class Client
3
+ HEADERS = {
4
+ "Content-Type" => "application/json",
5
+ "Accept" => "application/json"
6
+ }
7
+
8
+ def initialize(url: nil, open_timeout: 3, read_timeout: nil)
9
+ url ||= ENV["CARROT2_URL"] || "http://localhost:8080"
10
+ @uri = URI.parse(url)
11
+ @http = Net::HTTP.new(@uri.host, @uri.port)
12
+ @http.use_ssl = true if @uri.scheme == "https"
13
+ @http.open_timeout = open_timeout if open_timeout
14
+ @http.read_timeout = read_timeout if read_timeout
15
+ end
16
+
17
+ def list
18
+ get("service/list")
19
+ end
20
+
21
+ def cluster(documents, language: nil, algorithm: nil, parameters: nil, template: nil)
22
+ # no defaults if template
23
+ unless template
24
+ language ||= "English"
25
+ algorithm ||= "Lingo"
26
+ parameters ||= {}
27
+ end
28
+
29
+ # data
30
+ data = {
31
+ documents: documents.map { |v| v.is_a?(String) ? {field: v} : v }
32
+ }
33
+ data[:language] = language if language
34
+ data[:algorithm] = algorithm if algorithm
35
+ data[:parameters] = parameters if parameters
36
+
37
+ # path
38
+ path = "service/cluster"
39
+ path = "#{path}?#{URI.encode_www_form(template: template)}" if template
40
+
41
+ post(path, data)
42
+ end
43
+
44
+ private
45
+
46
+ def get(path)
47
+ handle_response do
48
+ @http.get("#{@uri.request_uri.chomp("/")}/#{path}", HEADERS)
49
+ end
50
+ end
51
+
52
+ def post(path, data)
53
+ handle_response do
54
+ @http.post("#{@uri.request_uri.chomp("/")}/#{path}", data.to_json, HEADERS)
55
+ end
56
+ end
57
+
58
+ def handle_response
59
+ begin
60
+ response = yield
61
+ rescue Errno::ECONNREFUSED => e
62
+ raise Carrot2::Error, e.message
63
+ end
64
+
65
+ unless response.kind_of?(Net::HTTPSuccess)
66
+ body = JSON.parse(response.body) rescue {}
67
+ message = body["message"] || "Bad response: #{response.code}"
68
+ raise Carrot2::Error, message
69
+ end
70
+
71
+ JSON.parse(response.body)
72
+ end
73
+ end
74
+ end
@@ -1,3 +1,3 @@
1
- class Carrot2
2
- VERSION = "0.3.0"
1
+ module Carrot2
2
+ VERSION = "0.5.0"
3
3
  end
data/lib/carrot2.rb CHANGED
@@ -1,81 +1,15 @@
1
- # dependencies
1
+ # stdlib
2
2
  require "json"
3
3
  require "net/http"
4
4
 
5
5
  # modules
6
- require "carrot2/version"
6
+ require_relative "carrot2/client"
7
+ require_relative "carrot2/version"
7
8
 
8
- class Carrot2
9
+ module Carrot2
9
10
  class Error < StandardError; end
10
11
 
11
- HEADERS = {
12
- "Content-Type" => "application/json",
13
- "Accept" => "application/json"
14
- }
15
-
16
- def initialize(url: nil, open_timeout: 3, read_timeout: nil)
17
- url ||= ENV["CARROT2_URL"] || "http://localhost:8080"
18
- @uri = URI.parse(url)
19
- @http = Net::HTTP.new(@uri.host, @uri.port)
20
- @http.use_ssl = true if @uri.scheme == "https"
21
- @http.open_timeout = open_timeout if open_timeout
22
- @http.read_timeout = read_timeout if read_timeout
23
- end
24
-
25
- def list
26
- get("service/list")
27
- end
28
-
29
- def cluster(documents, language: nil, algorithm: nil, parameters: nil, template: nil)
30
- # no defaults if template
31
- unless template
32
- language ||= "English"
33
- algorithm ||= "Lingo"
34
- parameters ||= {}
35
- end
36
-
37
- # data
38
- data = {
39
- documents: documents.map { |v| v.is_a?(String) ? {field: v} : v }
40
- }
41
- data[:language] = language if language
42
- data[:algorithm] = algorithm if algorithm
43
- data[:parameters] = parameters if parameters
44
-
45
- # path
46
- path = "service/cluster"
47
- path = "#{path}?#{URI.encode_www_form(template: template)}" if template
48
-
49
- post(path, data)
50
- end
51
-
52
- private
53
-
54
- def get(path)
55
- handle_response do
56
- @http.get("#{@uri.request_uri.chomp("/")}/#{path}", HEADERS)
57
- end
58
- end
59
-
60
- def post(path, data)
61
- handle_response do
62
- @http.post("#{@uri.request_uri.chomp("/")}/#{path}", data.to_json, HEADERS)
63
- end
64
- end
65
-
66
- def handle_response
67
- begin
68
- response = yield
69
- rescue Errno::ECONNREFUSED => e
70
- raise Carrot2::Error, e.message
71
- end
72
-
73
- unless response.kind_of?(Net::HTTPSuccess)
74
- body = JSON.parse(response.body) rescue {}
75
- message = body["message"] || "Bad response: #{response.code}"
76
- raise Carrot2::Error, message
77
- end
78
-
79
- JSON.parse(response.body)
12
+ def self.new(**options)
13
+ Client.new(**options)
80
14
  end
81
15
  end
metadata CHANGED
@@ -1,59 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrot2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-30 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: minitest
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- description:
56
- email: andrew@chartkick.com
11
+ date: 2024-10-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: andrew@ankane.org
57
15
  executables: []
58
16
  extensions: []
59
17
  extra_rdoc_files: []
@@ -62,12 +20,13 @@ files:
62
20
  - LICENSE.txt
63
21
  - README.md
64
22
  - lib/carrot2.rb
23
+ - lib/carrot2/client.rb
65
24
  - lib/carrot2/version.rb
66
- homepage: https://github.com/ankane/carrot2
25
+ homepage: https://github.com/ankane/carrot2-ruby
67
26
  licenses:
68
27
  - MIT
69
28
  metadata: {}
70
- post_install_message:
29
+ post_install_message:
71
30
  rdoc_options: []
72
31
  require_paths:
73
32
  - lib
@@ -75,15 +34,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
75
34
  requirements:
76
35
  - - ">="
77
36
  - !ruby/object:Gem::Version
78
- version: '2.4'
37
+ version: '3.1'
79
38
  required_rubygems_version: !ruby/object:Gem::Requirement
80
39
  requirements:
81
40
  - - ">="
82
41
  - !ruby/object:Gem::Version
83
42
  version: '0'
84
43
  requirements: []
85
- rubygems_version: 3.1.2
86
- signing_key:
44
+ rubygems_version: 3.5.16
45
+ signing_key:
87
46
  specification_version: 4
88
47
  summary: Ruby client for Carrot2
89
48
  test_files: []