Class: Dict::CLI::Runner
- Inherits:
-
Object
- Object
- Dict::CLI::Runner
- Defined in:
- lib/dict/cli/runner.rb
Overview
Class, which provides using application in Command Line Interface.
Constant Summary collapse
- MSG =
"Przykład użycia: dict SŁOWO [OPCJE]\n `dict --help, aby uzyskać więcej informacji.\n"
- 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"
Instance Method Summary collapse
-
#clean_translation(results) ⇒ Object
Returns array without duplicates.
- #expected_argument_description(option) ⇒ Object
- #get_translations(opts, word) ⇒ Object
- #parameters_valid? ⇒ Boolean
- #parse_parameters ⇒ Object
-
#print_all_dictionaries_translations(results) ⇒ Object
Prints translations from all dictionaries.
-
#print_array(arr) ⇒ Object
Prints array elements one by one vertically.
-
#print_translations(results) ⇒ Object
Prints translations for given Hash or Array.
- #run ⇒ Object
Instance Method Details
#clean_translation(results) ⇒ Object
Returns array without duplicates
58 59 60 61 62 63 64 65 66 |
# File 'lib/dict/cli/runner.rb', line 58 def clean_translation(results) clean = [] results.each do |_, translations_hash| translations_hash.each do |_, translations| clean.concat(translations) end end clean.uniq end |
#expected_argument_description(option) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/dict/cli/runner.rb', line 43 def expected_argument_description(option) case option when "dict" Dict.available_dictionaries.join(', ') when "time" "liczba sekund" else "?" end end |
#get_translations(opts, word) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/dict/cli/runner.rb', line 33 def get_translations(opts, word) Timeout::timeout(opts[:time].to_i || 300) do if opts.dict? Dict.get_single_dictionary_translations(word, opts[:dict]) else Dict.get_all_dictionaries_translations(word) end end end |
#parameters_valid? ⇒ Boolean
13 14 15 |
# File 'lib/dict/cli/runner.rb', line 13 def parameters_valid? not ARGV.empty? end |
#parse_parameters ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/dict/cli/runner.rb', line 17 def parse_parameters opts = Slop.parse! do <<-END Przykład użycia: dict SŁOWO [OPCJE] Wyszukaj SŁOWO w dict, open-source'owym agregatorze słowników. END on '-h', :help, 'Wyświetl pomoc' on '-t', :time=, 'Ustaw limit czasu żądania w sekundach. Domyślnie: 300', :as => :int on '-d', :dict=, "Wybierz słownik. Dostępne są : #{Dict.available_dictionaries.join(', ')}" on '-v', :version, "Informacje o gemie, autorach, licencji" on '-c', :clean, "Nie wyświetlaj przykładów użycia" end end |
#print_all_dictionaries_translations(results) ⇒ Object
Prints translations from all dictionaries
69 70 71 72 73 74 |
# File 'lib/dict/cli/runner.rb', line 69 def print_all_dictionaries_translations(results) results.each do |dictionary, translations_hash| puts "Nazwa słownika - #{dictionary.upcase}" print_translations(translations_hash) end end |
#print_array(arr) ⇒ Object
Prints array elements one by one vertically
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/dict/cli/runner.rb', line 92 def print_array(arr) translations_string = case arr.size when 1 then "tłumaczenie" when 2..4 then "tłumaczenia" else "tłumaczeń" end puts "Znaleziono: #{arr.size} #{translations_string}" arr.each { |el| puts "- #{el}" } end |
#print_translations(results) ⇒ Object
Prints translations for given Hash or Array
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/dict/cli/runner.rb', line 77 def print_translations(results) if results.empty? puts 'Przepraszamy, ale w wybranym słowniku nie znaleziono tłumaczenia.' else if results.instance_of?(Hash) results.each do |_, translations| print_array(translations) end elsif results.instance_of?(Array) print_array(results) end end end |
#run ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/dict/cli/runner.rb', line 103 def run begin opts = parse_parameters rescue Slop::MissingArgumentError => e incomplete_option = /(.*?) expects an argument/.match(e.to_s)[1] description = expected_argument_description(incomplete_option) abort("Brakujący argument. Spodziewano: #{description}") end abort(opts.to_s) if opts.help? abort(VERSION) if opts.version? abort(MSG) if not parameters_valid? if opts.time? and (opts[:time].to_i) == 0 abort("Nieprawidłowa wartość czasu.") end translations = get_translations(opts, ARGV[0]) if opts.clean? && !opts.dict? print_translations(clean_translation(translations)) else if opts.dict? print_translations(translations) else print_all_dictionaries_translations(translations) end end rescue Timeout::Error puts "Upłynął limit czasu żądania." end |