dict 0.3.4 → 0.3.5
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/.gitignore +1 -1
- data/.rspec +2 -2
- data/.travis.yml +14 -14
- data/Gemfile +4 -4
- data/Gemfile.lock +40 -40
- data/README.md +34 -34
- data/Rakefile +8 -8
- data/bin/dict +7 -7
- data/dict.gemspec +28 -28
- data/lib/dict.rb +5 -5
- data/lib/dict/cli/runner.rb +137 -137
- data/lib/dict/dict.rb +38 -40
- data/lib/dict/dictionary.rb +45 -45
- data/lib/dict/glosbe.rb +68 -68
- data/lib/dict/result.rb +38 -38
- data/lib/dict/version.rb +3 -3
- data/lib/dict/wiktionary.rb +91 -91
- data/spec/dict/lib_dict_cli_runner_spec.rb +120 -120
- data/spec/dict/lib_dict_spec.rb +39 -39
- data/spec/dict/lib_glosbe_spec.rb +48 -48
- data/spec/dict/lib_wiktionary_spec.rb +68 -68
- data/spec/dict/spec_helper.rb +17 -17
- data/spec/dict/vcr_cassettes/glosbe_translations_asdfff_cassette.yml +1518 -1518
- data/spec/dict/vcr_cassettes/glosbe_translations_atomic_cassette.yml +2794 -2794
- data/spec/dict/vcr_cassettes/glosbe_translations_usage_cassette.yml +2530 -2530
- data/spec/dict/vcr_cassettes/glosbe_translations_woda_cassette.yml +1949 -1949
- data/spec/dict/vcr_cassettes/slowik_runner_cassette.yml +4178 -4178
- data/spec/dict/vcr_cassettes/translations_dragon_cassette.yml +8659 -8659
- data/spec/dict/vcr_cassettes/translations_slownik_cassette.yml +4177 -4177
- data/spec/dict/vcr_cassettes/wiktionary_no_usage_examples.yml +8634 -8634
- data/spec/dict/vcr_cassettes/wiktionary_translate_result_uppercase.yml +6120 -6120
- data/spec/dict/vcr_cassettes/wiktionary_translations_field_cassette.yml +8600 -8600
- data/spec/dict/vcr_cassettes/wiktionary_translations_samochod_cassette.yml +6140 -6140
- data/spec/dict/vcr_cassettes/wiktionary_usage_examples_kot.yml +6444 -6444
- data/spec/dict/vcr_setup.rb +19 -19
- metadata +20 -2
data/.gitignore
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
*~
|
1
|
+
*~
|
2
2
|
*.gem
|
data/.rspec
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
--color
|
2
|
-
--format progress
|
1
|
+
--color
|
2
|
+
--format progress
|
data/.travis.yml
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
language: ruby
|
2
|
-
rvm:
|
3
|
-
- 1.9.3
|
4
|
-
- 1.9.2
|
5
|
-
notifications:
|
6
|
-
email:
|
7
|
-
- gozdek.aleksander@gmail.com
|
8
|
-
- grk@ragnarson.com
|
9
|
-
- jan.borwin@gmail.com
|
10
|
-
- kosmadunikowski@gmail.com
|
11
|
-
- mtczerwinski@gmail.com
|
12
|
-
- michalk@ragnarson.com
|
13
|
-
- m.podlecki@op.pl
|
14
|
-
- rafal.osko@gmail.com
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 1.9.3
|
4
|
+
- 1.9.2
|
5
|
+
notifications:
|
6
|
+
email:
|
7
|
+
- gozdek.aleksander@gmail.com
|
8
|
+
- grk@ragnarson.com
|
9
|
+
- jan.borwin@gmail.com
|
10
|
+
- kosmadunikowski@gmail.com
|
11
|
+
- mtczerwinski@gmail.com
|
12
|
+
- michalk@ragnarson.com
|
13
|
+
- m.podlecki@op.pl
|
14
|
+
- rafal.osko@gmail.com
|
data/Gemfile
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
source "http://rubygems.org"
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in dict.gemspec
|
4
|
-
gemspec
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in dict.gemspec
|
4
|
+
gemspec
|
data/Gemfile.lock
CHANGED
@@ -1,40 +1,40 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
dict (0.3.4)
|
5
|
-
nokogiri (~> 1.5.5)
|
6
|
-
slop (~> 3.3.2)
|
7
|
-
|
8
|
-
GEM
|
9
|
-
remote: http://rubygems.org/
|
10
|
-
specs:
|
11
|
-
activesupport (3.2.6)
|
12
|
-
i18n (~> 0.6)
|
13
|
-
multi_json (~> 1.0)
|
14
|
-
diff-lcs (1.1.3)
|
15
|
-
fakeweb (1.3.0)
|
16
|
-
i18n (0.6.0)
|
17
|
-
multi_json (1.3.6)
|
18
|
-
nokogiri (1.5.5)
|
19
|
-
rake (0.9.2.2)
|
20
|
-
rspec (2.11.0)
|
21
|
-
rspec-core (~> 2.11.0)
|
22
|
-
rspec-expectations (~> 2.11.0)
|
23
|
-
rspec-mocks (~> 2.11.0)
|
24
|
-
rspec-core (2.11.0)
|
25
|
-
rspec-expectations (2.11.1)
|
26
|
-
diff-lcs (~> 1.1.3)
|
27
|
-
rspec-mocks (2.11.1)
|
28
|
-
slop (3.3.2)
|
29
|
-
vcr (2.2.3)
|
30
|
-
|
31
|
-
PLATFORMS
|
32
|
-
ruby
|
33
|
-
|
34
|
-
DEPENDENCIES
|
35
|
-
activesupport
|
36
|
-
dict!
|
37
|
-
fakeweb
|
38
|
-
rake
|
39
|
-
rspec (~> 2.11)
|
40
|
-
vcr
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
dict (0.3.4)
|
5
|
+
nokogiri (~> 1.5.5)
|
6
|
+
slop (~> 3.3.2)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: http://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activesupport (3.2.6)
|
12
|
+
i18n (~> 0.6)
|
13
|
+
multi_json (~> 1.0)
|
14
|
+
diff-lcs (1.1.3)
|
15
|
+
fakeweb (1.3.0)
|
16
|
+
i18n (0.6.0)
|
17
|
+
multi_json (1.3.6)
|
18
|
+
nokogiri (1.5.5)
|
19
|
+
rake (0.9.2.2)
|
20
|
+
rspec (2.11.0)
|
21
|
+
rspec-core (~> 2.11.0)
|
22
|
+
rspec-expectations (~> 2.11.0)
|
23
|
+
rspec-mocks (~> 2.11.0)
|
24
|
+
rspec-core (2.11.0)
|
25
|
+
rspec-expectations (2.11.1)
|
26
|
+
diff-lcs (~> 1.1.3)
|
27
|
+
rspec-mocks (2.11.1)
|
28
|
+
slop (3.3.2)
|
29
|
+
vcr (2.2.3)
|
30
|
+
|
31
|
+
PLATFORMS
|
32
|
+
ruby
|
33
|
+
|
34
|
+
DEPENDENCIES
|
35
|
+
activesupport
|
36
|
+
dict!
|
37
|
+
fakeweb
|
38
|
+
rake
|
39
|
+
rspec (~> 2.11)
|
40
|
+
vcr
|
data/README.md
CHANGED
@@ -1,35 +1,35 @@
|
|
1
|
-
# dict [](http://travis-ci.org/Ragnarson/dict-gem)
|
2
|
-
CLI and backend for dict - an open source dictionary aggregator.
|
3
|
-
|
4
|
-
## Usage ##
|
5
|
-
To search for `WORD`:
|
6
|
-
|
7
|
-
dict WORD [OPTIONS]
|
8
|
-
|
9
|
-
### Available options ###
|
10
|
-
|
11
|
-
- `-h` or `--help` - displays help message
|
12
|
-
- `-t` or `--time` - used to set timeout in seconds. Default value: 300
|
13
|
-
- `-d` or `--dict` - used to select desired dictionary
|
14
|
-
- `-v` or `--version` - used to show version, authors, license
|
15
|
-
- `-c` or `--clean` - used to delete examples from translation
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
## People
|
20
|
-
|
21
|
-
### Trainees
|
22
|
-
- [Jan Borwin](https://github.com/janiuuuu)
|
23
|
-
- [Mateusz Czerwiński](https://github.com/mrfustrr)
|
24
|
-
- [Kosma Dunikowski](https://github.com/kdunee)
|
25
|
-
- [Aleksander Gozdek](https://github.com/alexgozdek)
|
26
|
-
- [Rafał Ośko](https://github.com/rekosko)
|
27
|
-
- [Michał Podlecki](https://github.com/michalpodlecki)
|
28
|
-
|
29
|
-
### Mentors
|
30
|
-
- [Grzegorz Kołodziejczyk](https://github.com/grk)
|
31
|
-
- [Michał Kwiatkowski](https://github.com/mkwiatkowski)
|
32
|
-
|
33
|
-
## Made during internship at [Ragnarson](http://ragnarson.com/)
|
34
|
-
|
1
|
+
# dict [](http://travis-ci.org/Ragnarson/dict-gem)
|
2
|
+
CLI and backend for dict - an open source dictionary aggregator.
|
3
|
+
|
4
|
+
## Usage ##
|
5
|
+
To search for `WORD`:
|
6
|
+
|
7
|
+
dict WORD [OPTIONS]
|
8
|
+
|
9
|
+
### Available options ###
|
10
|
+
|
11
|
+
- `-h` or `--help` - displays help message
|
12
|
+
- `-t` or `--time` - used to set timeout in seconds. Default value: 300
|
13
|
+
- `-d` or `--dict` - used to select desired dictionary
|
14
|
+
- `-v` or `--version` - used to show version, authors, license
|
15
|
+
- `-c` or `--clean` - used to delete examples from translation
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
## People
|
20
|
+
|
21
|
+
### Trainees
|
22
|
+
- [Jan Borwin](https://github.com/janiuuuu)
|
23
|
+
- [Mateusz Czerwiński](https://github.com/mrfustrr)
|
24
|
+
- [Kosma Dunikowski](https://github.com/kdunee)
|
25
|
+
- [Aleksander Gozdek](https://github.com/alexgozdek)
|
26
|
+
- [Rafał Ośko](https://github.com/rekosko)
|
27
|
+
- [Michał Podlecki](https://github.com/michalpodlecki)
|
28
|
+
|
29
|
+
### Mentors
|
30
|
+
- [Grzegorz Kołodziejczyk](https://github.com/grk)
|
31
|
+
- [Michał Kwiatkowski](https://github.com/mkwiatkowski)
|
32
|
+
|
33
|
+
## Made during internship at [Ragnarson](http://ragnarson.com/)
|
34
|
+
|
35
35
|
## Hosted by [Shelly Cloud](https://shellycloud.com/)
|
data/Rakefile
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
require "bundler/gem_tasks"
|
2
|
-
require "rspec/core/rake_task"
|
3
|
-
|
4
|
-
task :default => :spec
|
5
|
-
desc "Run specs"
|
6
|
-
RSpec::Core::RakeTask.new do |t|
|
7
|
-
t.rspec_opts = %w(-fp --color)
|
8
|
-
end
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
|
4
|
+
task :default => :spec
|
5
|
+
desc "Run specs"
|
6
|
+
RSpec::Core::RakeTask.new do |t|
|
7
|
+
t.rspec_opts = %w(-fp --color)
|
8
|
+
end
|
data/bin/dict
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
#!/usr/bin/ruby
|
2
|
-
# -*- coding: utf-8 -*-
|
3
|
-
|
4
|
-
require 'dict/cli/runner'
|
5
|
-
|
6
|
-
runner = Dict::CLI::Runner.new
|
7
|
-
runner.run
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
|
4
|
+
require 'dict/cli/runner'
|
5
|
+
|
6
|
+
runner = Dict::CLI::Runner.new
|
7
|
+
runner.run
|
data/dict.gemspec
CHANGED
@@ -1,28 +1,28 @@
|
|
1
|
-
$:.push File.expand_path("../lib", __FILE__)
|
2
|
-
require "dict/version"
|
3
|
-
|
4
|
-
Gem::Specification.new do |s|
|
5
|
-
s.add_dependency 'slop', '~> 3.3.2'
|
6
|
-
s.add_dependency 'nokogiri', '~>1.5.5'
|
7
|
-
s.add_development_dependency "rspec", "~> 2.11"
|
8
|
-
s.add_development_dependency "rake"
|
9
|
-
s.add_development_dependency "vcr"
|
10
|
-
s.add_development_dependency "activesupport"
|
11
|
-
s.add_development_dependency "fakeweb"
|
12
|
-
|
13
|
-
s.name = %q{dict}
|
14
|
-
s.version = Dict::VERSION
|
15
|
-
s.authors = ['Aleksander Gozdek', 'Mateusz Czerwinski', 'Michał Podlecki', 'Rafał Ośko', 'Kosma Dunikowski', 'Jan Borwin']
|
16
|
-
s.email = ['mtczerwinski@gmail.com']
|
17
|
-
s.date = Time.now.strftime('%Y-%m-%d')
|
18
|
-
s.summary = %q{Gem stworzony dla aplikacji słownikowej}
|
19
|
-
s.description = <<-END
|
20
|
-
Dict to open-source'owy agregator słowników.
|
21
|
-
END
|
22
|
-
s.homepage = 'https://github.com/Ragnarson/dict-gem'
|
23
|
-
|
24
|
-
s.files = `git ls-files`.split("\n")
|
25
|
-
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
26
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
27
|
-
s.require_paths = ["lib"]
|
28
|
-
end
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require "dict/version"
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.add_dependency 'slop', '~> 3.3.2'
|
6
|
+
s.add_dependency 'nokogiri', '~>1.5.5'
|
7
|
+
s.add_development_dependency "rspec", "~> 2.11"
|
8
|
+
s.add_development_dependency "rake"
|
9
|
+
s.add_development_dependency "vcr"
|
10
|
+
s.add_development_dependency "activesupport"
|
11
|
+
s.add_development_dependency "fakeweb"
|
12
|
+
|
13
|
+
s.name = %q{dict}
|
14
|
+
s.version = Dict::VERSION
|
15
|
+
s.authors = ['Aleksander Gozdek', 'Mateusz Czerwinski', 'Michał Podlecki', 'Rafał Ośko', 'Kosma Dunikowski', 'Jan Borwin']
|
16
|
+
s.email = ['mtczerwinski@gmail.com']
|
17
|
+
s.date = Time.now.strftime('%Y-%m-%d')
|
18
|
+
s.summary = %q{Gem stworzony dla aplikacji słownikowej}
|
19
|
+
s.description = <<-END
|
20
|
+
Dict to open-source'owy agregator słowników.
|
21
|
+
END
|
22
|
+
s.homepage = 'https://github.com/Ragnarson/dict-gem'
|
23
|
+
|
24
|
+
s.files = `git ls-files`.split("\n")
|
25
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
26
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
27
|
+
s.require_paths = ["lib"]
|
28
|
+
end
|
data/lib/dict.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
require 'dict/dict'
|
2
|
-
|
3
|
-
module Dict
|
4
|
-
autoload :VERSION, "dict/version"
|
5
|
-
end
|
1
|
+
require 'dict/dict'
|
2
|
+
|
3
|
+
module Dict
|
4
|
+
autoload :VERSION, "dict/version"
|
5
|
+
end
|
data/lib/dict/cli/runner.rb
CHANGED
@@ -1,137 +1,137 @@
|
|
1
|
-
# -*- coding: utf-8 -*-
|
2
|
-
|
3
|
-
# Class, which provides using application in Command Line Interface.
|
4
|
-
|
5
|
-
require 'dict/dict'
|
6
|
-
require 'dict/version'
|
7
|
-
require 'slop'
|
8
|
-
require 'timeout'
|
9
|
-
|
10
|
-
module Dict
|
11
|
-
module CLI
|
12
|
-
class Runner
|
13
|
-
|
14
|
-
def parameters_valid?
|
15
|
-
not ARGV.empty?
|
16
|
-
end
|
17
|
-
|
18
|
-
def parse_parameters
|
19
|
-
opts = Slop.parse! do
|
20
|
-
banner <<-END
|
21
|
-
Przykład użycia: dict SŁOWO [OPCJE]
|
22
|
-
Wyszukaj SŁOWO w dict, open-source'owym agregatorze słowników.
|
23
|
-
END
|
24
|
-
|
25
|
-
on '-h', :help=, 'Wyświetl pomoc', :argument => :optional
|
26
|
-
on '-t', :time=, 'Ustaw limit czasu żądania w sekundach. Domyślnie: 300', :as => :int
|
27
|
-
on '-d', :dict=, "Wybierz słownik. Dostępne są : #{Dict.available_dictionaries.join(', ')}"
|
28
|
-
on '-v', :version=, "Informacje o gemie, autorach, licencji", :argument => :optional
|
29
|
-
on '-c', :clean=, "Nie wyświetlaj przykładów użycia", :argument => :optional
|
30
|
-
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def get_translations(opts, word)
|
35
|
-
Timeout::timeout(opts[:time].to_i || 300) do
|
36
|
-
if opts.dict?
|
37
|
-
Dict.get_single_dictionary_translations(word, opts[:dict])
|
38
|
-
else
|
39
|
-
Dict.get_all_dictionaries_translations(word)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
rescue Timeout::Error
|
43
|
-
"Upłynął limit czasu żądania."
|
44
|
-
end
|
45
|
-
|
46
|
-
def expected_argument_description(option)
|
47
|
-
case option
|
48
|
-
when "dict"
|
49
|
-
Dict.available_dictionaries.join(', ')
|
50
|
-
when "time"
|
51
|
-
"liczba sekund"
|
52
|
-
else
|
53
|
-
"?"
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
MSG = "Przykład użycia: dict SŁOWO [OPCJE]\n `dict --help, aby uzyskać więcej informacji.\n"
|
58
|
-
VERSION = "dict wersja #{Dict::VERSION}\nWyszukaj SŁOWO w dict, open-source'owym agregatorze słowników. \nCopyright (C) 2012 by\nZespół:\n Jan Borwin\n Mateusz Czerwiński\n Kosma Dunikowski\n Aleksander Gozdek\n Rafał Ośko\n Michał Podlecki\nMentorzy:\n Grzegorz Kołodziejski\n Michał Kwiatkowski\nLicencja: MIT\nStworzono na praktykach w : http://ragnarson.com/\nHosting: Shelly Cloud :\t http://shellycloud.com/\nStrona domowa:\t\t http://github.com/Ragnarson/dict-gem/\nSłowniki:\t\t http://wiktionary.org/\n\t\t\t http://glosbe.com/\n"
|
59
|
-
|
60
|
-
# Returns array without duplicates
|
61
|
-
def clean_translation(results)
|
62
|
-
clean = []
|
63
|
-
results.each do |_, translations_hash|
|
64
|
-
translations_hash.each do |_, translations|
|
65
|
-
clean.concat(translations)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
clean.uniq
|
69
|
-
end
|
70
|
-
|
71
|
-
# Prints translations from all dictionaries
|
72
|
-
def print_all_dictionaries_translations(results)
|
73
|
-
results.each do |dictionary, translations_hash|
|
74
|
-
puts "Nazwa słownika - #{dictionary.upcase}"
|
75
|
-
print_translations(translations_hash)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
# Prints translations for given Hash or Array
|
80
|
-
def print_translations(results)
|
81
|
-
if results.empty?
|
82
|
-
puts 'Przepraszamy, ale w wybranym słowniku nie znaleziono tłumaczenia.'
|
83
|
-
else
|
84
|
-
if results.instance_of?(Hash)
|
85
|
-
results.each do |_, translations|
|
86
|
-
print_array(translations)
|
87
|
-
end
|
88
|
-
elsif results.instance_of?(Array)
|
89
|
-
print_array(results)
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
# Prints array elements one by one vertically
|
95
|
-
def print_array(arr)
|
96
|
-
puts "Znaleziono: #{arr.size} tłumaczeń"
|
97
|
-
arr.each { |el| puts "- #{el}" }
|
98
|
-
end
|
99
|
-
|
100
|
-
def run
|
101
|
-
begin
|
102
|
-
opts = parse_parameters
|
103
|
-
rescue Slop::MissingArgumentError => e
|
104
|
-
incomplete_option = /(.*?) expects an argument/.match(e.to_s)[1]
|
105
|
-
description = expected_argument_description(incomplete_option)
|
106
|
-
abort("Brakujący argument. Spodziewano: #{description}")
|
107
|
-
end
|
108
|
-
|
109
|
-
if opts.help?
|
110
|
-
abort(opts.to_s)
|
111
|
-
elsif opts.version?
|
112
|
-
abort(VERSION)
|
113
|
-
elsif opts.time?
|
114
|
-
if (opts[:time].to_i) == 0
|
115
|
-
abort("Nieprawidłowa wartość czasu.")
|
116
|
-
else
|
117
|
-
print_all_dictionaries_translations(get_translations(opts, ARGV[0]))
|
118
|
-
end
|
119
|
-
else
|
120
|
-
if !parameters_valid?
|
121
|
-
abort(MSG)
|
122
|
-
else
|
123
|
-
if opts.clean? && !opts.dict?
|
124
|
-
print_translations(clean_translation(get_translations(opts, ARGV[0])))
|
125
|
-
else
|
126
|
-
if opts.dict?
|
127
|
-
print_translations(get_translations(opts, ARGV[0]))
|
128
|
-
else
|
129
|
-
print_all_dictionaries_translations(get_translations(opts, ARGV[0]))
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|
133
|
-
end
|
134
|
-
end
|
135
|
-
end
|
136
|
-
end
|
137
|
-
end
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
# Class, which provides using application in Command Line Interface.
|
4
|
+
|
5
|
+
require 'dict/dict'
|
6
|
+
require 'dict/version'
|
7
|
+
require 'slop'
|
8
|
+
require 'timeout'
|
9
|
+
|
10
|
+
module Dict
|
11
|
+
module CLI
|
12
|
+
class Runner
|
13
|
+
|
14
|
+
def parameters_valid?
|
15
|
+
not ARGV.empty?
|
16
|
+
end
|
17
|
+
|
18
|
+
def parse_parameters
|
19
|
+
opts = Slop.parse! do
|
20
|
+
banner <<-END
|
21
|
+
Przykład użycia: dict SŁOWO [OPCJE]
|
22
|
+
Wyszukaj SŁOWO w dict, open-source'owym agregatorze słowników.
|
23
|
+
END
|
24
|
+
|
25
|
+
on '-h', :help=, 'Wyświetl pomoc', :argument => :optional
|
26
|
+
on '-t', :time=, 'Ustaw limit czasu żądania w sekundach. Domyślnie: 300', :as => :int
|
27
|
+
on '-d', :dict=, "Wybierz słownik. Dostępne są : #{Dict.available_dictionaries.join(', ')}"
|
28
|
+
on '-v', :version=, "Informacje o gemie, autorach, licencji", :argument => :optional
|
29
|
+
on '-c', :clean=, "Nie wyświetlaj przykładów użycia", :argument => :optional
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def get_translations(opts, word)
|
35
|
+
Timeout::timeout(opts[:time].to_i || 300) do
|
36
|
+
if opts.dict?
|
37
|
+
Dict.get_single_dictionary_translations(word, opts[:dict])
|
38
|
+
else
|
39
|
+
Dict.get_all_dictionaries_translations(word)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
rescue Timeout::Error
|
43
|
+
"Upłynął limit czasu żądania."
|
44
|
+
end
|
45
|
+
|
46
|
+
def expected_argument_description(option)
|
47
|
+
case option
|
48
|
+
when "dict"
|
49
|
+
Dict.available_dictionaries.join(', ')
|
50
|
+
when "time"
|
51
|
+
"liczba sekund"
|
52
|
+
else
|
53
|
+
"?"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
MSG = "Przykład użycia: dict SŁOWO [OPCJE]\n `dict --help, aby uzyskać więcej informacji.\n"
|
58
|
+
VERSION = "dict wersja #{Dict::VERSION}\nWyszukaj SŁOWO w dict, open-source'owym agregatorze słowników. \nCopyright (C) 2012 by\nZespół:\n Jan Borwin\n Mateusz Czerwiński\n Kosma Dunikowski\n Aleksander Gozdek\n Rafał Ośko\n Michał Podlecki\nMentorzy:\n Grzegorz Kołodziejski\n Michał Kwiatkowski\nLicencja: MIT\nStworzono na praktykach w : http://ragnarson.com/\nHosting: Shelly Cloud :\t http://shellycloud.com/\nStrona domowa:\t\t http://github.com/Ragnarson/dict-gem/\nSłowniki:\t\t http://wiktionary.org/\n\t\t\t http://glosbe.com/\n"
|
59
|
+
|
60
|
+
# Returns array without duplicates
|
61
|
+
def clean_translation(results)
|
62
|
+
clean = []
|
63
|
+
results.each do |_, translations_hash|
|
64
|
+
translations_hash.each do |_, translations|
|
65
|
+
clean.concat(translations)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
clean.uniq
|
69
|
+
end
|
70
|
+
|
71
|
+
# Prints translations from all dictionaries
|
72
|
+
def print_all_dictionaries_translations(results)
|
73
|
+
results.each do |dictionary, translations_hash|
|
74
|
+
puts "Nazwa słownika - #{dictionary.upcase}"
|
75
|
+
print_translations(translations_hash)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# Prints translations for given Hash or Array
|
80
|
+
def print_translations(results)
|
81
|
+
if results.empty?
|
82
|
+
puts 'Przepraszamy, ale w wybranym słowniku nie znaleziono tłumaczenia.'
|
83
|
+
else
|
84
|
+
if results.instance_of?(Hash)
|
85
|
+
results.each do |_, translations|
|
86
|
+
print_array(translations)
|
87
|
+
end
|
88
|
+
elsif results.instance_of?(Array)
|
89
|
+
print_array(results)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# Prints array elements one by one vertically
|
95
|
+
def print_array(arr)
|
96
|
+
puts "Znaleziono: #{arr.size} tłumaczeń"
|
97
|
+
arr.each { |el| puts "- #{el}" }
|
98
|
+
end
|
99
|
+
|
100
|
+
def run
|
101
|
+
begin
|
102
|
+
opts = parse_parameters
|
103
|
+
rescue Slop::MissingArgumentError => e
|
104
|
+
incomplete_option = /(.*?) expects an argument/.match(e.to_s)[1]
|
105
|
+
description = expected_argument_description(incomplete_option)
|
106
|
+
abort("Brakujący argument. Spodziewano: #{description}")
|
107
|
+
end
|
108
|
+
|
109
|
+
if opts.help?
|
110
|
+
abort(opts.to_s)
|
111
|
+
elsif opts.version?
|
112
|
+
abort(VERSION)
|
113
|
+
elsif opts.time?
|
114
|
+
if (opts[:time].to_i) == 0
|
115
|
+
abort("Nieprawidłowa wartość czasu.")
|
116
|
+
else
|
117
|
+
print_all_dictionaries_translations(get_translations(opts, ARGV[0]))
|
118
|
+
end
|
119
|
+
else
|
120
|
+
if !parameters_valid?
|
121
|
+
abort(MSG)
|
122
|
+
else
|
123
|
+
if opts.clean? && !opts.dict?
|
124
|
+
print_translations(clean_translation(get_translations(opts, ARGV[0])))
|
125
|
+
else
|
126
|
+
if opts.dict?
|
127
|
+
print_translations(get_translations(opts, ARGV[0]))
|
128
|
+
else
|
129
|
+
print_all_dictionaries_translations(get_translations(opts, ARGV[0]))
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|