cambridge_dict 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: c267c98cdc071799bc99ff46db6963e543bf79ba0a96ce1efb391d27fabcd40c
4
- data.tar.gz: 3bfc706d541138132c78030a885a227eb3f66cb1405235b5c87022420f204b2c
3
+ metadata.gz: 294c128a929285511c42b782899cdf08045b3cc3c6034bf89fbabb5ca6cfed0b
4
+ data.tar.gz: ca2757faaaf9658699190234a391c6f69208a30e7f022e8d0f9cb0139f2101fc
5
5
  SHA512:
6
- metadata.gz: 6f31eeb418116539c5d61b9487a0bfc1d98c0bfa310c986bf7a4ac99a213c46e98e4032a7bc45f0b85ae87cb82ff2ea043fbd7979c600ad94d2442dc84fbb3ba
7
- data.tar.gz: defbccf32f337d0ddbefac15a85853e27e0487cf6f4d67f77cde947228bc50fe77bc2d0270d948a26b5fe0dd47b93e891d06d75bf433f0781f0d036d03c3aef8
6
+ metadata.gz: 7f227d3434d68a7988df29fca488fa3952bf6c84a2455246e858888fdde7e22a14959061d8ce1dc346f610a4e00e48c8dd29cd4a5b7094418457bd2373f38eef
7
+ data.tar.gz: b15a564112405d640aab362d816d66e4fa724fcc7ea40f51acf52e0a7e56147913fab901ad16eaf1c42e586b236a59ef8250a13d448f3a6ebe077f95ea025319
data/README.md CHANGED
@@ -11,9 +11,18 @@ gem install cambridge_dict
11
11
  ## Client Usage
12
12
 
13
13
  ```
14
+ # throws error
15
+ client = CambridgeDictionary::Client.lookup! "hello", "en"
16
+
17
+ # returns nil if not found
18
+ client = CambridgeDictionary::Client.lookup "hello", "en"
19
+
20
+ # can also create instance
14
21
  client = CambridgeDictionary::Client.new
15
22
  result = client.lookup("back", language: "en")
16
23
 
24
+ return if result.nil?
25
+
17
26
  =>
18
27
  {:word=>"back",
19
28
  :pos=>["adverb", "noun", "verb", "adjective"],
@@ -1,3 +1,3 @@
1
1
  module CambridgeDict
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -5,12 +5,22 @@ require 'sinatra/cors'
5
5
  require 'cambridge_dict/version'
6
6
 
7
7
  module CambridgeDict
8
+ class Error < StandardError; end
9
+
8
10
  class Client
9
11
  def initialize
10
12
  @base_url = "https://dictionary.cambridge.org"
11
13
  @wiki_base = "https://simple.wiktionary.org/wiki"
12
14
  end
13
15
 
16
+ def self.lookup(word, language: 'en')
17
+ self.new.lookup(word, language: language)
18
+ end
19
+
20
+ def self.lookup!(word, language: 'en')
21
+ self.new.lookup!(word, language: language)
22
+ end
23
+
14
24
  def lookup(word, language: 'en')
15
25
  nation, dict_lang = parse_language(language)
16
26
  url = "#{@base_url}/#{nation}/dictionary/#{dict_lang}/#{word}"
@@ -23,8 +33,12 @@ module CambridgeDict
23
33
  doc = Nokogiri::HTML(response.body)
24
34
  wiki_verbs = fetch_verbs("#{@wiki_base}/#{word}")
25
35
 
36
+ word = extract_word(doc)
37
+
38
+ return nil if word.nil?
39
+
26
40
  {
27
- word: extract_word(doc),
41
+ word: word,
28
42
  pos: extract_pos(doc),
29
43
  verbs: wiki_verbs,
30
44
  pronunciation: extract_pronunciation(doc),
@@ -32,6 +46,16 @@ module CambridgeDict
32
46
  }
33
47
  end
34
48
 
49
+ def lookup!(word, language: 'en')
50
+ result = lookup word, language: language
51
+
52
+ if result.nil?
53
+ raise Error.new "Word not found!"
54
+ end
55
+
56
+ result
57
+ end
58
+
35
59
  private
36
60
 
37
61
  def parse_language(lang)
@@ -69,7 +93,7 @@ module CambridgeDict
69
93
  end
70
94
 
71
95
  def extract_word(doc)
72
- doc.at_css('.hw.dhw')&.text || ''
96
+ doc.at_css('.hw.dhw')&.text
73
97
  end
74
98
 
75
99
  def extract_pos(doc)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cambridge_dict
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