free_dictionary_api 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/free_dictionary_api/version.rb +1 -1
- data/lib/free_dictionary_api.rb +27 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a4ef615f534b5015fa049832d066a3c82161ae639bda5882ee22f379a27f57b
|
4
|
+
data.tar.gz: 989337e46754e50fed5756faa8aceee551cf8900d0f02862ec9afe000a327b31
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b10b80dd8609d2cad4ab254864720e6ba0d3085878fb2ffce4209353229cc4dfcd5269a4327c4c6b3caa446039593b6b6170179dfa67749d0315d84bdda2f387
|
7
|
+
data.tar.gz: 99cdcdc5be5b236ea2e14647e1244aef188d058a266556ed7bf87f93a625b374b5a200941f0cea45cc6352935fce15f21f133960da2236a14dea49dbf40a3874
|
data/lib/free_dictionary_api.rb
CHANGED
@@ -11,6 +11,14 @@ module FreeDictionaryApi
|
|
11
11
|
BASE_URL = 'https://api.dictionaryapi.dev/api/v2/entries/en'.freeze
|
12
12
|
PARSER = URI::Parser.new
|
13
13
|
|
14
|
+
def self.lookup word
|
15
|
+
self.new.lookup word
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.lookup! word
|
19
|
+
self.new.lookup! word
|
20
|
+
end
|
21
|
+
|
14
22
|
def lookup(word)
|
15
23
|
encoded_word = PARSER.escape(word)
|
16
24
|
uri = URI("#{BASE_URL}/#{encoded_word}")
|
@@ -24,6 +32,19 @@ module FreeDictionaryApi
|
|
24
32
|
end
|
25
33
|
end
|
26
34
|
|
35
|
+
def lookup!(word)
|
36
|
+
encoded_word = PARSER.escape(word)
|
37
|
+
uri = URI("#{BASE_URL}/#{encoded_word}")
|
38
|
+
response = Net::HTTP.get_response(uri)
|
39
|
+
|
40
|
+
case response
|
41
|
+
when Net::HTTPSuccess
|
42
|
+
parse_response(response.body)
|
43
|
+
else
|
44
|
+
handle_error(response, true)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
27
48
|
private
|
28
49
|
|
29
50
|
def parse_response(body)
|
@@ -31,10 +52,14 @@ module FreeDictionaryApi
|
|
31
52
|
data.map { |word_data| Word.new(word_data) }
|
32
53
|
end
|
33
54
|
|
34
|
-
def handle_error(response)
|
55
|
+
def handle_error(response, raise_error = false)
|
35
56
|
case response.code
|
36
57
|
when '404'
|
37
|
-
|
58
|
+
if raise_error
|
59
|
+
raise Error, "Word not found"
|
60
|
+
else
|
61
|
+
return nil
|
62
|
+
end
|
38
63
|
else
|
39
64
|
raise Error, "API request failed with status #{response.code}"
|
40
65
|
end
|