cambridge_dict 0.1.0 → 0.2.1
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 +15 -3
- data/config.ru +2 -1
- data/lib/cambridge_dict/server.rb +29 -0
- data/lib/cambridge_dict/version.rb +1 -1
- data/lib/cambridge_dict.rb +28 -29
- metadata +8 -35
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83f7f7932b9333e01c7af904c306f9e0a215cd797c824285b301292924b880ec
|
4
|
+
data.tar.gz: 244ebce0e0e906b85d111fbbdea76b3870e02fea54f16d6c232fc2711db68be6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4071c55b3707412ef8681946aa61c8d16bdd2b98e074bf6a2c1d4ecc52a58b9398b08920a22032469ad324b699a78aa5702e9cb7494c297d0c61fc3013db3e44
|
7
|
+
data.tar.gz: 7e4e7927bc956849ed64f97c0d829a40696f95932adfdc0bc3fc74258728bc7449031182993c3edf5790fc6adf911f7ad75903250b6b9ddc2c6592e58c4fb0e6
|
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"],
|
@@ -58,11 +67,14 @@ result = client.lookup("back", language: "en")
|
|
58
67
|
## Webserver Setup
|
59
68
|
|
60
69
|
```
|
70
|
+
# add sinatra and sinatra-cors to gemfile
|
71
|
+
|
61
72
|
# config.ru
|
62
|
-
require '
|
63
|
-
|
73
|
+
require 'cambridge_dict'
|
74
|
+
require 'cambridge_dict/server'
|
75
|
+
run CambridgeDict::Server
|
64
76
|
|
65
|
-
|
77
|
+
rackup config.ru
|
66
78
|
```
|
67
79
|
|
68
80
|
## Development
|
data/config.ru
CHANGED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'sinatra'
|
2
|
+
require 'sinatra/cors'
|
3
|
+
|
4
|
+
module CambridgeDict
|
5
|
+
class Server < Sinatra::Base
|
6
|
+
register Sinatra::Cors
|
7
|
+
|
8
|
+
set :allow_origin, "*"
|
9
|
+
set :allow_methods, "GET,HEAD,POST"
|
10
|
+
set :allow_headers, "content-type,if-modified-since"
|
11
|
+
set :expose_headers, "location,link"
|
12
|
+
|
13
|
+
client = Client.new
|
14
|
+
|
15
|
+
get '/api/dictionary/:language/:entry' do
|
16
|
+
content_type :json
|
17
|
+
|
18
|
+
result = client.lookup(params[:entry], language: params[:language])
|
19
|
+
|
20
|
+
if result && !result[:word].empty?
|
21
|
+
status 200
|
22
|
+
result.to_json
|
23
|
+
else
|
24
|
+
status 404
|
25
|
+
{error: "word not found"}.to_json
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/cambridge_dict.rb
CHANGED
@@ -1,16 +1,25 @@
|
|
1
1
|
require 'nokogiri'
|
2
2
|
require 'httparty'
|
3
|
-
|
4
|
-
|
5
|
-
require 'cambridge_dict/version'
|
3
|
+
|
4
|
+
require_relative 'cambridge_dict/version'
|
6
5
|
|
7
6
|
module CambridgeDict
|
7
|
+
class Error < StandardError; end
|
8
|
+
|
8
9
|
class Client
|
9
10
|
def initialize
|
10
11
|
@base_url = "https://dictionary.cambridge.org"
|
11
12
|
@wiki_base = "https://simple.wiktionary.org/wiki"
|
12
13
|
end
|
13
14
|
|
15
|
+
def self.lookup(word, language: 'en')
|
16
|
+
self.new.lookup(word, language: language)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.lookup!(word, language: 'en')
|
20
|
+
self.new.lookup!(word, language: language)
|
21
|
+
end
|
22
|
+
|
14
23
|
def lookup(word, language: 'en')
|
15
24
|
nation, dict_lang = parse_language(language)
|
16
25
|
url = "#{@base_url}/#{nation}/dictionary/#{dict_lang}/#{word}"
|
@@ -23,8 +32,12 @@ module CambridgeDict
|
|
23
32
|
doc = Nokogiri::HTML(response.body)
|
24
33
|
wiki_verbs = fetch_verbs("#{@wiki_base}/#{word}")
|
25
34
|
|
35
|
+
word = extract_word(doc)
|
36
|
+
|
37
|
+
return nil if word.nil?
|
38
|
+
|
26
39
|
{
|
27
|
-
word:
|
40
|
+
word: word,
|
28
41
|
pos: extract_pos(doc),
|
29
42
|
verbs: wiki_verbs,
|
30
43
|
pronunciation: extract_pronunciation(doc),
|
@@ -32,6 +45,16 @@ module CambridgeDict
|
|
32
45
|
}
|
33
46
|
end
|
34
47
|
|
48
|
+
def lookup!(word, language: 'en')
|
49
|
+
result = lookup word, language: language
|
50
|
+
|
51
|
+
if result.nil?
|
52
|
+
raise Error.new "Word not found!"
|
53
|
+
end
|
54
|
+
|
55
|
+
result
|
56
|
+
end
|
57
|
+
|
35
58
|
private
|
36
59
|
|
37
60
|
def parse_language(lang)
|
@@ -69,7 +92,7 @@ module CambridgeDict
|
|
69
92
|
end
|
70
93
|
|
71
94
|
def extract_word(doc)
|
72
|
-
doc.at_css('.hw.dhw')&.text
|
95
|
+
doc.at_css('.hw.dhw')&.text
|
73
96
|
end
|
74
97
|
|
75
98
|
def extract_pos(doc)
|
@@ -147,28 +170,4 @@ module CambridgeDict
|
|
147
170
|
end
|
148
171
|
end
|
149
172
|
|
150
|
-
class API < Sinatra::Base
|
151
|
-
register Sinatra::Cors
|
152
|
-
|
153
|
-
set :allow_origin, "*"
|
154
|
-
set :allow_methods, "GET,HEAD,POST"
|
155
|
-
set :allow_headers, "content-type,if-modified-since"
|
156
|
-
set :expose_headers, "location,link"
|
157
|
-
|
158
|
-
client = Client.new
|
159
|
-
|
160
|
-
get '/api/dictionary/:language/:entry' do
|
161
|
-
content_type :json
|
162
|
-
|
163
|
-
result = client.lookup(params[:entry], language: params[:language])
|
164
|
-
|
165
|
-
if result && !result[:word].empty?
|
166
|
-
status 200
|
167
|
-
result.to_json
|
168
|
-
else
|
169
|
-
status 404
|
170
|
-
{error: "word not found"}.to_json
|
171
|
-
end
|
172
|
-
end
|
173
|
-
end
|
174
173
|
end
|
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
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben D'Angelo
|
@@ -14,58 +14,30 @@ dependencies:
|
|
14
14
|
name: nokogiri
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.15'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.15'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: httparty
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0.21'
|
34
|
-
type: :runtime
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0.21'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: sinatra
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
31
|
+
- - ">="
|
46
32
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
33
|
+
version: 0.1.0
|
48
34
|
type: :runtime
|
49
35
|
prerelease: false
|
50
36
|
version_requirements: !ruby/object:Gem::Requirement
|
51
37
|
requirements:
|
52
|
-
- - "
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '3.1'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: sinatra-cors
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - "~>"
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '1.2'
|
62
|
-
type: :runtime
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - "~>"
|
38
|
+
- - ">="
|
67
39
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
40
|
+
version: 0.1.0
|
69
41
|
- !ruby/object:Gem::Dependency
|
70
42
|
name: rspec
|
71
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -119,6 +91,7 @@ files:
|
|
119
91
|
- Rakefile
|
120
92
|
- config.ru
|
121
93
|
- lib/cambridge_dict.rb
|
94
|
+
- lib/cambridge_dict/server.rb
|
122
95
|
- lib/cambridge_dict/version.rb
|
123
96
|
homepage: https://codeberg.org/bendangelo/cambridge_dict
|
124
97
|
licenses:
|