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 +4 -4
- data/README.md +9 -0
- data/lib/cambridge_dict/version.rb +1 -1
- data/lib/cambridge_dict.rb +26 -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: 294c128a929285511c42b782899cdf08045b3cc3c6034bf89fbabb5ca6cfed0b
|
4
|
+
data.tar.gz: ca2757faaaf9658699190234a391c6f69208a30e7f022e8d0f9cb0139f2101fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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"],
|
data/lib/cambridge_dict.rb
CHANGED
@@ -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:
|
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)
|