open_dictionary 0.1.1 → 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/open_dictionary/version.rb +1 -1
- data/lib/open_dictionary.rb +21 -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: 0a0d26bcf393434845a9ceecb204a24ad819c0a85b9e5a105d90d0a141256271
|
4
|
+
data.tar.gz: b8f0776e469c2c4354c8e5fde9e0c04568e2b0b7b14bc1a858ad2ac26399f6b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 663afc026528794d39bab24773df1236eb1d5495b082087e09d2a43bc6bbce577643a8b0d9dc3ade39a0731747462fa5cc5c830cb18aaee7dd3c759afafe1685
|
7
|
+
data.tar.gz: 980a5581622a7d19794c2ef8ed4e1b7fe404e14df9975b7ed5b24b1a105ff20c1ab9966f52b2c922936d7ae962befe5983bc50ece77488be7005012b5195d5fa
|
data/lib/open_dictionary.rb
CHANGED
@@ -16,6 +16,10 @@ module OpenDictionary
|
|
16
16
|
new.lookup(word)
|
17
17
|
end
|
18
18
|
|
19
|
+
def self.lookup!(word)
|
20
|
+
new.lookup!(word)
|
21
|
+
end
|
22
|
+
|
19
23
|
def lookup(word)
|
20
24
|
response = fetch_definition(word)
|
21
25
|
parse_response(response, word)
|
@@ -25,21 +29,36 @@ module OpenDictionary
|
|
25
29
|
raise Error, "JSON parsing error: #{e.message}"
|
26
30
|
end
|
27
31
|
|
32
|
+
def lookup!(word)
|
33
|
+
response = fetch_definition(word, true)
|
34
|
+
parse_response(response, word)
|
35
|
+
rescue HTTParty::Error => e
|
36
|
+
raise Error, "HTTP Error: #{e.message}"
|
37
|
+
rescue JSON::ParserError => e
|
38
|
+
raise Error, "JSON parsing error: #{e.message}"
|
39
|
+
end
|
40
|
+
|
28
41
|
private
|
29
42
|
|
30
|
-
def fetch_definition(word)
|
43
|
+
def fetch_definition(word, raise_error = false)
|
31
44
|
path = word.downcase.chars.join('/')
|
32
45
|
url = "#{BASE_URL}/#{path}/_.json"
|
33
46
|
response = HTTParty.get(url)
|
34
47
|
|
35
48
|
unless response.success?
|
36
|
-
|
49
|
+
if raise_error
|
50
|
+
raise Error, "Failed to fetch definition (HTTP #{response.code})"
|
51
|
+
else
|
52
|
+
return nil
|
53
|
+
end
|
37
54
|
end
|
38
55
|
|
39
56
|
response.body
|
40
57
|
end
|
41
58
|
|
42
59
|
def parse_response(response_body, original_word)
|
60
|
+
return nil if response_body.nil?
|
61
|
+
|
43
62
|
data = JSON.parse(response_body)
|
44
63
|
Word.new(data)
|
45
64
|
end
|