dict 0.1.9 → 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.
- data/lib/dict.rb +8 -6
- data/lib/dictionary.rb +11 -4
- data/lib/dictpl.rb +3 -1
- data/lib/module_main.rb +19 -25
- data/lib/wiktionary.rb +4 -2
- metadata +2 -2
data/lib/dict.rb
CHANGED
@@ -8,16 +8,16 @@ module Dict
|
|
8
8
|
class << self
|
9
9
|
def get_all_dictionaries_translations(word)
|
10
10
|
dictionaries = Hash.new
|
11
|
-
|
11
|
+
|
12
12
|
available_services.each do |service|
|
13
13
|
dictionaries[service] = get_single_dictionary_translations(word, service)
|
14
14
|
end
|
15
15
|
dictionaries
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
def print_all_dictionaries_translations(word)
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
def get_single_dictionary_translations(word, service)
|
22
22
|
case service
|
23
23
|
when 'wiktionary'
|
@@ -26,8 +26,10 @@ module Dict
|
|
26
26
|
Dictpl.new(word, DICT_URL).translate
|
27
27
|
else Dictionary.message
|
28
28
|
end
|
29
|
+
rescue Dictionary::ConnectError
|
30
|
+
"Couldn't connect to the service."
|
29
31
|
end
|
30
|
-
|
32
|
+
|
31
33
|
def print_single_dictionary_translations(word, service)
|
32
34
|
obj = get_single_dictionary_translations(word, service)
|
33
35
|
hash = obj.translate
|
@@ -35,11 +37,11 @@ module Dict
|
|
35
37
|
puts "#{k} - #{v}"
|
36
38
|
end
|
37
39
|
end
|
38
|
-
|
40
|
+
|
39
41
|
def to_json(hash)
|
40
42
|
hash.to_json
|
41
43
|
end
|
42
|
-
|
44
|
+
|
43
45
|
def available_services
|
44
46
|
['wiktionary', 'dictpl']
|
45
47
|
end
|
data/lib/dictionary.rb
CHANGED
@@ -10,9 +10,9 @@ class Dictionary
|
|
10
10
|
@examples = []
|
11
11
|
@uri = URI(URI.escape(url + word.downcase.tr(' ', '_')))
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
def make_hash_results(arr)
|
15
|
-
hash = arr.each_slice(2).inject({}) do |h, (key, value)|
|
15
|
+
hash = arr.each_slice(2).inject({}) do |h, (key, value)|
|
16
16
|
if h.has_key?(key)
|
17
17
|
h[key].push(value) ; h
|
18
18
|
else
|
@@ -22,12 +22,19 @@ class Dictionary
|
|
22
22
|
@translations, @examples = hash.keys, hash.values
|
23
23
|
hash
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
def check_arguments(word)
|
27
27
|
raise ArgumentError.new("No given word") if word.empty?
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
def self.message
|
31
31
|
'There\'s no such dictionary in database.'
|
32
32
|
end
|
33
|
+
|
34
|
+
class ConnectError < Exception
|
35
|
+
attr_reader :original
|
36
|
+
def initialize(original = $!)
|
37
|
+
@original = original
|
38
|
+
end
|
39
|
+
end
|
33
40
|
end
|
data/lib/dictpl.rb
CHANGED
@@ -8,10 +8,12 @@ DICT_URL = 'http://dict.pl/dict?word='
|
|
8
8
|
class Dictpl < Dictionary
|
9
9
|
# Method returns hash with translations as keys and examples of using words as values
|
10
10
|
def translate
|
11
|
-
context_words = []
|
11
|
+
context_words = []
|
12
12
|
Nokogiri::HTML(open(@uri)).xpath('//td[@class="resWordCol"]/a').each do |node|
|
13
13
|
context_words << node.text
|
14
14
|
end
|
15
15
|
make_hash_results(context_words)
|
16
|
+
rescue OpenURI::HTTPError
|
17
|
+
raise Dictionary::ConnectError
|
16
18
|
end
|
17
19
|
end
|
data/lib/module_main.rb
CHANGED
@@ -5,56 +5,50 @@ require 'slop'
|
|
5
5
|
require 'timeout'
|
6
6
|
|
7
7
|
module Main
|
8
|
-
def self.
|
9
|
-
ARGV.empty?
|
8
|
+
def self.parameters_valid?
|
9
|
+
not ARGV.empty?
|
10
10
|
end
|
11
11
|
|
12
12
|
def self.parse_parameters
|
13
|
-
|
13
|
+
available_dictionaries = Dict.available_services.join(', ')
|
14
|
+
|
15
|
+
opts = Slop.parse! do
|
14
16
|
banner <<-END
|
15
17
|
Usage: dict WORD [OPTIONS]
|
16
18
|
Search WORD in dict, an open source dictionary aggregator.
|
17
19
|
END
|
18
|
-
|
19
|
-
on '-h', :help, '
|
20
|
-
on '-t', :time=, 'timeout in seconds
|
21
|
-
on '-d', :dict=,
|
20
|
+
|
21
|
+
on '-h', :help, 'Display this help message'
|
22
|
+
on '-t', :time=, 'Set timeout in seconds. Default: 300', :as => :int
|
23
|
+
on '-d', :dict=, "Select desired dictionary. Available options: #{available_dictionaries}"
|
22
24
|
end
|
23
25
|
opts
|
24
26
|
end
|
25
|
-
|
27
|
+
|
26
28
|
def self.get_translations(opts, word)
|
27
29
|
Timeout::timeout(opts[:time].to_i || 300) do
|
28
|
-
if opts.dict?
|
30
|
+
if opts.dict?
|
29
31
|
Dict.get_single_dictionary_translations(word, opts[:dict])
|
30
32
|
else
|
31
33
|
Dict.get_all_dictionaries_translations(word)
|
32
34
|
end
|
33
35
|
end
|
34
|
-
rescue
|
36
|
+
rescue Timeout::Error
|
35
37
|
"Timeout for the query."
|
36
38
|
end
|
37
|
-
|
39
|
+
|
38
40
|
def self.main(opts)
|
39
41
|
begin
|
40
42
|
opts = parse_parameters
|
41
43
|
rescue Slop::MissingArgumentError
|
42
|
-
|
43
|
-
exit
|
44
|
+
abort("Missing argument")
|
44
45
|
end
|
45
46
|
|
46
|
-
if opts.help?
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
else
|
51
|
-
if check_parameters? == true
|
52
|
-
puts "Please enter a word. (-h for help)"
|
53
|
-
exit
|
54
|
-
end
|
55
|
-
puts get_translations(opts, ARGV[0])
|
56
|
-
end
|
47
|
+
abort(opts.to_s) if opts.help?
|
48
|
+
parameters_valid? or abort("Please enter a word. (-h for help)")
|
49
|
+
|
50
|
+
puts get_translations(opts, ARGV[0])
|
57
51
|
end
|
58
|
-
|
52
|
+
|
59
53
|
end
|
60
54
|
|
data/lib/wiktionary.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dict
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -12,7 +12,7 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date: 2012-07-
|
15
|
+
date: 2012-07-11 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: slop
|