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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ab7e6df2573a8b1882b035027433cdb771ae70aab3f3aa0c0a3102f26ea9240d
4
- data.tar.gz: 6b36c959dcf19f4d0f4e0ffc8ded42b21165e1d6ae2796f1a2e26cfa259d96d0
3
+ metadata.gz: 0a0d26bcf393434845a9ceecb204a24ad819c0a85b9e5a105d90d0a141256271
4
+ data.tar.gz: b8f0776e469c2c4354c8e5fde9e0c04568e2b0b7b14bc1a858ad2ac26399f6b0
5
5
  SHA512:
6
- metadata.gz: 0e7334049af2c40a258435be3e972a59697d2e09af66c3b352f677bdb4adffc54fc3cbb72fb55986ddc81e0f89a07e51dd223b5c3b961cad492b211f6957aeaf
7
- data.tar.gz: 7c8f06326baffb2421e1ed6dc536012ab362a9b135fe3aa9f4dd74aced8d5191caec3236309398a76bf57bf4dd8ce9489daa9bd954dbf662786302e1d8cca9a9
6
+ metadata.gz: 663afc026528794d39bab24773df1236eb1d5495b082087e09d2a43bc6bbce577643a8b0d9dc3ade39a0731747462fa5cc5c830cb18aaee7dd3c759afafe1685
7
+ data.tar.gz: 980a5581622a7d19794c2ef8ed4e1b7fe404e14df9975b7ed5b24b1a105ff20c1ab9966f52b2c922936d7ae962befe5983bc50ece77488be7005012b5195d5fa
@@ -1,3 +1,3 @@
1
1
  module OpenDictionary
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -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
- raise Error, "Failed to fetch definition (HTTP #{response.code})"
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open_dictionary
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben D'Angelo