Skip to content

Commit 81072f2

Browse files
committed
chore: small consistency and documentation fixes
- AnalyticsRules and CurationSets now memoize their resource proxies in `[]`, matching the pattern every other collection class already uses (Collections, Documents, Aliases, Keys, Presets, SynonymSets, etc). Calls like `client.curation_sets['x']` now return the same instance across calls, which is the documented contract elsewhere. - Remove an unreachable `|| 3` fallback from `Configuration#num_retries`: the LHS is always an integer because `validate!` rejects empty `nodes`. - Replace `node.send(:[], attr)` with `node[attr]` in `Configuration#node_missing_parameters?` — same behaviour, less indirection. - Document `Typesense::Error#data` in the README so callers know how to read the HTTP status and body from a raised exception.
1 parent f33f224 commit 81072f2

4 files changed

Lines changed: 22 additions & 4 deletions

File tree

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ Here are some examples with inline comments that walk you through how to use the
2929

3030
Tests are also a good place to know how the the library works internally: [spec](spec)
3131

32+
### Error handling
33+
34+
Failed requests raise a subclass of `Typesense::Error`. The exception's `message` is the API error string (`"Object Not Found"`, etc.), and the underlying Faraday response is available on `#data` for callers that need the HTTP status, headers, or raw body:
35+
36+
```ruby
37+
begin
38+
client.collections['unknown'].retrieve
39+
rescue Typesense::Error::ObjectNotFound => e
40+
e.message # => "Not Found"
41+
e.data.status # => 404
42+
e.data.body # => raw response body
43+
end
44+
```
45+
46+
The exception classes that map to specific status codes are: `RequestMalformed` (400), `RequestUnauthorized` (401), `ObjectNotFound` (404), `ObjectAlreadyExists` (409), `ObjectUnprocessable` (422), `ServerError` (5xx), `HTTPStatus0Error` (status 0), `TimeoutError`, and `HTTPError` (anything else).
47+
3248
## Compatibility
3349

3450
| Typesense Server | typesense-ruby |

lib/typesense/analytics_rules.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class AnalyticsRules
66

77
def initialize(api_call)
88
@api_call = api_call
9+
@analytics_rules = {}
910
end
1011

1112
def create(rules)
@@ -17,7 +18,7 @@ def retrieve
1718
end
1819

1920
def [](rule_name)
20-
AnalyticsRule.new(rule_name, @api_call)
21+
@analytics_rules[rule_name] ||= AnalyticsRule.new(rule_name, @api_call)
2122
end
2223
end
2324
end

lib/typesense/configuration.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def initialize(options = {})
1111
@nearest_node = options[:nearest_node]
1212
@connection_timeout_seconds = options[:connection_timeout_seconds] || options[:timeout_seconds] || 10
1313
@healthcheck_interval_seconds = options[:healthcheck_interval_seconds] || 15
14-
@num_retries = options[:num_retries] || (@nodes.length + (@nearest_node.nil? ? 0 : 1)) || 3
14+
@num_retries = options[:num_retries] || (@nodes.length + (@nearest_node.nil? ? 0 : 1))
1515
@retry_interval_seconds = options[:retry_interval_seconds] || 0.1
1616
@api_key = options[:api_key]
1717

@@ -36,7 +36,7 @@ def validate!
3636
private
3737

3838
def node_missing_parameters?(node)
39-
%i[protocol host port].any? { |attr| node.send(:[], attr).nil? }
39+
%i[protocol host port].any? { |attr| node[attr].nil? }
4040
end
4141

4242
def show_deprecation_warnings(options)

lib/typesense/curation_sets.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class CurationSets
66

77
def initialize(api_call)
88
@api_call = api_call
9+
@curation_sets = {}
910
end
1011

1112
def upsert(curation_set_name, curation_set_data)
@@ -17,7 +18,7 @@ def retrieve
1718
end
1819

1920
def [](curation_set_name)
20-
CurationSet.new(curation_set_name, @api_call)
21+
@curation_sets[curation_set_name] ||= CurationSet.new(curation_set_name, @api_call)
2122
end
2223
end
2324
end

0 commit comments

Comments
 (0)