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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 32c267cbbe88cdf72ef360bef124776f2374c5cdb812948f534792b31081cc2a
4
- data.tar.gz: 57f87b53a62f638ff1d96996a5962609ac315f2bb7c11d57195c4239cdcb6710
3
+ metadata.gz: 3a4ef615f534b5015fa049832d066a3c82161ae639bda5882ee22f379a27f57b
4
+ data.tar.gz: 989337e46754e50fed5756faa8aceee551cf8900d0f02862ec9afe000a327b31
5
5
  SHA512:
6
- metadata.gz: bdcd6366d5b330dee4511ce2fd79a66c521edf503f0eb27cf1fff0e01d18114e59c24ffedba1dc87a861b3140116a58e5f425eccd820fd9ba95280e54053bd31
7
- data.tar.gz: 06f95c0aec6f9adecc609fa1d23edc5baec1436010082db2b20f9c43f0c11bd45578344fac4e6e72068484a5b3c04d9221f58784f83743a99db9a2d6b96e9370
6
+ metadata.gz: b10b80dd8609d2cad4ab254864720e6ba0d3085878fb2ffce4209353229cc4dfcd5269a4327c4c6b3caa446039593b6b6170179dfa67749d0315d84bdda2f387
7
+ data.tar.gz: 99cdcdc5be5b236ea2e14647e1244aef188d058a266556ed7bf87f93a625b374b5a200941f0cea45cc6352935fce15f21f133960da2236a14dea49dbf40a3874
@@ -1,3 +1,3 @@
1
1
  module FreeDictionaryApi
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -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
- raise Error, "Word not found"
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: free_dictionary_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben D'Angelo