mw-dictionary 1.0.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 +7 -0
- data/lib/cache/cache.rb +34 -0
- data/lib/client/client.rb +24 -0
- data/lib/mw-dictionary.rb +43 -0
- data/lib/parser/parser.rb +53 -0
- metadata +47 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: c6759b2a64c18f914419a9e02dc1e1d4cc65b5e2902ad2fb898a9dbce6e11b4f
|
|
4
|
+
data.tar.gz: b39b8dbc3f8eac0b301619b5b4358ad22faf7f6fbe722767245f820adf6e19a0
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 80a1e855c7062090e57cc8f297252ba20200c948cfe73dfc20393906311f19bbcfe985f6920c04feb3c61a995dd51f213f5b697e58f567b4d0d9fae5bc0d2afb
|
|
7
|
+
data.tar.gz: 3f45cf16f248ef8083014bdd7e26a95845bf1d8d77844c6f5e24c1500b1b64975c980e63bca7e5505adf61cac9cb9b15be5025442f13269756910ec3041e584e
|
data/lib/cache/cache.rb
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
class Cache
|
|
2
|
+
def initialize
|
|
3
|
+
@cache = {}
|
|
4
|
+
@file = File.open('cache.dat', 'a+')
|
|
5
|
+
read_cache
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def add(word, thes, result)
|
|
9
|
+
word = word+"_thes" if thes
|
|
10
|
+
@cache[word.to_sym] = result
|
|
11
|
+
|
|
12
|
+
result = result.gsub(/\n+/,'@')
|
|
13
|
+
@file.write "#{word}|| #{result}"
|
|
14
|
+
@file.puts
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def search(word, thes)
|
|
18
|
+
word += '_thes' if thes
|
|
19
|
+
@cache[word.to_sym]
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def read_cache
|
|
23
|
+
arr = IO.readlines("cache.dat")
|
|
24
|
+
if arr
|
|
25
|
+
arr.each do |line|
|
|
26
|
+
key, value = line.split('||')
|
|
27
|
+
str = ''
|
|
28
|
+
value = value.split('@')
|
|
29
|
+
@cache[key.to_sym] = value
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
class Client
|
|
2
|
+
attr_accessor :api_key
|
|
3
|
+
|
|
4
|
+
def initialize(dict, thes)
|
|
5
|
+
@api_key = {
|
|
6
|
+
dict: dict,
|
|
7
|
+
thes: thes
|
|
8
|
+
}
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def request(url, thes)
|
|
12
|
+
api_key = @api_key[thes ? :thes : :dict]
|
|
13
|
+
if !api_key
|
|
14
|
+
puts "Please provide the API Key...\n"
|
|
15
|
+
exit(1)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
uri = URI("#{url}?key=#{api_key}")
|
|
19
|
+
response = Net::HTTP.get_response(uri)
|
|
20
|
+
json = JSON.parse(response.body)
|
|
21
|
+
[response, json]
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
require 'net/http'
|
|
2
|
+
require 'json'
|
|
3
|
+
|
|
4
|
+
require 'client/client.rb'
|
|
5
|
+
require 'parser/parser.rb'
|
|
6
|
+
require 'cache/cache.rb'
|
|
7
|
+
|
|
8
|
+
module MWDictionaryGem
|
|
9
|
+
class Dictionary
|
|
10
|
+
def initialize(dict_api_key, thes_api_key = nil)
|
|
11
|
+
@cache = Cache.new
|
|
12
|
+
@client = Client.new(dict_api_key, thes_api_key)
|
|
13
|
+
@url = "https://www.dictionaryapi.com/api/v3/references"
|
|
14
|
+
super()
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def search(word)
|
|
18
|
+
thes = nil
|
|
19
|
+
word, thes = word.split()
|
|
20
|
+
if thes != nil && thes != '-t'
|
|
21
|
+
puts "Invalid format (try '-t')."
|
|
22
|
+
exit(1)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
puts "[ #{word} ]"
|
|
26
|
+
res = @cache.search(word, thes)
|
|
27
|
+
if res
|
|
28
|
+
puts res
|
|
29
|
+
return
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
@thesaurus = !!thes
|
|
33
|
+
@word = word
|
|
34
|
+
url = ((@thesaurus) ? "#{@url}/thesaurus/json/" : "#{@url}/collegiate/json/") + @word
|
|
35
|
+
response, json = @client.request(url, @thesaurus)
|
|
36
|
+
display(json)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def display(json)
|
|
40
|
+
@cache.add(@word, @thesaurus, @thesaurus ? parse_thesaurus(json) : parse_dict(json))
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
def parse_thesaurus(json)
|
|
2
|
+
result = ''
|
|
3
|
+
|
|
4
|
+
if json == nil or json[0] == nil
|
|
5
|
+
result = "The word you've entered isn't in the dictionary."
|
|
6
|
+
else
|
|
7
|
+
if json[0].class == Hash
|
|
8
|
+
json[0]['meta']['syns'][0].each do |syn|
|
|
9
|
+
result += "- #{syn}\n"
|
|
10
|
+
end
|
|
11
|
+
elsif json[0].class == String
|
|
12
|
+
result = suggestion(json)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
result += "\n-------------"
|
|
16
|
+
puts result
|
|
17
|
+
|
|
18
|
+
result
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def parse_dict(json)
|
|
22
|
+
result = ''
|
|
23
|
+
if json == nil or json[0] == nil
|
|
24
|
+
result = "The word you've entered isn't in the dictionary."
|
|
25
|
+
else
|
|
26
|
+
if json[0].class == Hash
|
|
27
|
+
i = 0
|
|
28
|
+
json[0]['shortdef'].each do |meaning|
|
|
29
|
+
result += "#{('a'.ord+i).chr}) #{meaning}\n"
|
|
30
|
+
i += 1
|
|
31
|
+
end
|
|
32
|
+
elsif json[0].class == String
|
|
33
|
+
result = suggestion(json)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
result += "\n-------------"
|
|
37
|
+
puts result
|
|
38
|
+
|
|
39
|
+
result
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def suggestion(json)
|
|
43
|
+
result = "Suggestion:\n"
|
|
44
|
+
i = 0
|
|
45
|
+
json.each do |word|
|
|
46
|
+
result += "%-15s " % word
|
|
47
|
+
i += 1
|
|
48
|
+
if (i+1)%6==0
|
|
49
|
+
result += "\n"
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
result
|
|
53
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mw-dictionary
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Jione Eu
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2020-05-23 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description:
|
|
14
|
+
email:
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/cache/cache.rb
|
|
20
|
+
- lib/client/client.rb
|
|
21
|
+
- lib/mw-dictionary.rb
|
|
22
|
+
- lib/parser/parser.rb
|
|
23
|
+
homepage: https://rubygems.org/gems/mw-dictionary
|
|
24
|
+
licenses:
|
|
25
|
+
- MIT
|
|
26
|
+
metadata:
|
|
27
|
+
documentation_uri: https://github.com/jioneeu/mw-dictionary
|
|
28
|
+
post_install_message:
|
|
29
|
+
rdoc_options: []
|
|
30
|
+
require_paths:
|
|
31
|
+
- lib
|
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
33
|
+
requirements:
|
|
34
|
+
- - ">="
|
|
35
|
+
- !ruby/object:Gem::Version
|
|
36
|
+
version: '0'
|
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - ">="
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '0'
|
|
42
|
+
requirements: []
|
|
43
|
+
rubygems_version: 3.1.2
|
|
44
|
+
signing_key:
|
|
45
|
+
specification_version: 4
|
|
46
|
+
summary: Merriam-Webster Collegiate Dictionary and Thesaurus API Wrapper
|
|
47
|
+
test_files: []
|