Skip to content

Commit 07dcd06

Browse files
author
Richard Degenne
committed
Added dictionary entry retrieval
1 parent 958e262 commit 07dcd06

4 files changed

Lines changed: 34 additions & 0 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ client.create_dictionary('my-dictionary', case_insensitive: true)
101101
client.create_dictionary_entries('my-dictionary', [{id: 'my-entry', text: 'Text to be matched'}])
102102
```
103103

104+
#### Getting entries from a dictionary
105+
106+
```
107+
client.get_dictionary_entries('my-dictionary')
108+
109+
# Using pagination
110+
client.get_dictionary_entries('my-dictionary', limit: 20, offset: 0)
111+
```
104112

105113
#### Deleting a dictionary entry
106114

lib/textrazor/client.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ def create_dictionary(id, **options)
4242
Request.create_dictionary(api_key, dictionary)
4343
end
4444

45+
def get_dictionary_entries(dictionary_id, limit: 0, offset: 0)
46+
response = ApiResponse.new(
47+
Request.get_dictionary_entries(api_key, dictionary_id, limit: limit, offset: offset)
48+
)
49+
return [] unless response.raw_response.key?(:entries)
50+
response.raw_response[:entries].map { |hash| DictionaryEntry.create_from_hash(hash) }
51+
end
52+
4553
def delete_dictionary(dictionary_id)
4654
Request.delete_dictionary(api_key, dictionary_id)
4755
end

lib/textrazor/request.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ def self.create_dictionary(api_key, dictionary, **options)
3535
dictionary
3636
end
3737

38+
def self.get_dictionary_entries(api_key, dictionary_id, limit:, offset:)
39+
::RestClient.get(
40+
url("entities/#{dictionary_id}/_all?limit=#{limit}&offset=#{offset}"),
41+
build_headers(api_key)
42+
)
43+
end
44+
3845
def self.delete_dictionary(api_key, dictionary_id)
3946
::RestClient.delete(
4047
url("entities/#{dictionary_id}"),

spec/lib/textrazor/request_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ module TextRazor
9696
end
9797
end
9898

99+
context ".get_dictionary_entries" do
100+
101+
it "should make correct calls" do
102+
expect(::RestClient).to receive(:get).
103+
with("https://api.textrazor.com/entities/id/_all?limit=10&offset=20",
104+
accept_encoding: 'gzip', x_textrazor_key: 'api_key')
105+
106+
Request.get_dictionary_entries(api_key, 'id', limit: 10, offset: 20)
107+
end
108+
end
109+
99110
context ".delete_dictionary" do
100111

101112
it "should make correct calls" do

0 commit comments

Comments
 (0)