dict 0.2.1 → 0.2.2

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/Gemfile.lock CHANGED
@@ -8,7 +8,13 @@ PATH
8
8
  GEM
9
9
  remote: http://rubygems.org/
10
10
  specs:
11
+ activesupport (3.2.6)
12
+ i18n (~> 0.6)
13
+ multi_json (~> 1.0)
11
14
  diff-lcs (1.1.3)
15
+ fakeweb (1.3.0)
16
+ i18n (0.6.0)
17
+ multi_json (1.3.6)
12
18
  nokogiri (1.5.5)
13
19
  rake (0.9.2.2)
14
20
  rspec (2.11.0)
@@ -20,11 +26,15 @@ GEM
20
26
  diff-lcs (~> 1.1.3)
21
27
  rspec-mocks (2.11.1)
22
28
  slop (3.3.2)
29
+ vcr (2.2.3)
23
30
 
24
31
  PLATFORMS
25
32
  ruby
26
33
 
27
34
  DEPENDENCIES
35
+ activesupport
28
36
  dict!
37
+ fakeweb
29
38
  rake
30
39
  rspec (~> 2.11)
40
+ vcr
data/bin/dict CHANGED
@@ -3,7 +3,5 @@
3
3
 
4
4
  require 'dict/cli/runner'
5
5
 
6
- opts = Hash.new
7
-
8
6
  runner = Dict::CLI::Runner.new
9
- runner.run(opts)
7
+ runner.run
data/dict.gemspec CHANGED
@@ -6,6 +6,9 @@ Gem::Specification.new do |s|
6
6
  s.add_dependency 'nokogiri', '~>1.5.5'
7
7
  s.add_development_dependency "rspec", "~> 2.11"
8
8
  s.add_development_dependency "rake"
9
+ s.add_development_dependency "vcr"
10
+ s.add_development_dependency "activesupport"
11
+ s.add_development_dependency "fakeweb"
9
12
 
10
13
  s.name = %q{dict}
11
14
  s.version = Dict::VERSION
@@ -12,17 +12,15 @@ module Dict
12
12
  end
13
13
 
14
14
  def parse_parameters
15
- available_dictionaries = Dict.available_services.join(', ')
16
-
17
15
  opts = Slop.parse! do
18
16
  banner <<-END
19
- Usage: dict WORD [OPTIONS]
20
- Search WORD in dict, an open source dictionary aggregator.
17
+ Usage: dict WORD [OPTIONS]
18
+ Search WORD in dict, an open source dictionary aggregator.
21
19
  END
22
20
 
23
21
  on '-h', :help, 'Display this help message'
24
22
  on '-t', :time=, 'Set timeout in seconds. Default: 300', :as => :int
25
- on '-d', :dict=, "Select desired dictionary. Available options: #{available_dictionaries}"
23
+ on '-d', :dict=, "Select desired dictionary. Available options: #{Dict.available_dictionaries.join(', ')}"
26
24
  end
27
25
  opts
28
26
  end
@@ -39,15 +37,28 @@ module Dict
39
37
  "Timeout for the query."
40
38
  end
41
39
 
42
- def run(opts)
40
+ def expected_argument_description(option)
41
+ case option
42
+ when "dict"
43
+ Dict.available_dictionaries.join(', ')
44
+ when "time"
45
+ "number of seconds"
46
+ else
47
+ "?"
48
+ end
49
+ end
50
+
51
+ def run
43
52
  begin
44
53
  opts = parse_parameters
45
- rescue Slop::MissingArgumentError
46
- abort("Missing argument")
54
+ rescue Slop::MissingArgumentError => e
55
+ incomplete_option = /(.*?) expects an argument/.match(e.to_s)[1]
56
+ description = expected_argument_description(incomplete_option)
57
+ abort("Missing argument. Expected: #{description}")
47
58
  end
48
59
 
49
60
  abort(opts.to_s) if opts.help?
50
- parameters_valid? or abort("Please enter a word. (-h for help)")
61
+ abort("Please enter a word. (-h for help)") unless parameters_valid?
51
62
 
52
63
  puts get_translations(opts, ARGV[0])
53
64
  end
data/lib/dict/dict.rb CHANGED
@@ -2,47 +2,50 @@
2
2
 
3
3
  require 'dict/wiktionary'
4
4
  require 'dict/dictpl'
5
- require 'json'
5
+ require "yaml"
6
6
 
7
7
  module Dict
8
8
  class << self
9
+ # returns hash with structure as showed below
10
+ # { 'DICTIONARY_NAME' => { 'TRANSLATION' => ['EXAMPLE', ...], ... }, ... }
9
11
  def get_all_dictionaries_translations(word)
10
12
  dictionaries = Hash.new
11
13
 
12
- available_services.each do |service|
13
- dictionaries[service] = get_single_dictionary_translations(word, service)
14
+ available_dictionaries.each do |dictionary|
15
+ dictionaries[dictionary] = get_single_dictionary_translations(word, dictionary)
14
16
  end
15
17
  dictionaries
16
18
  end
17
19
 
20
+ # prints translations from all dictionaries
18
21
  def print_all_dictionaries_translations(word)
22
+ available_dictionaries.each do |dictionary|
23
+ print_single_dictionary_translations(word, dictionary)
24
+ end
19
25
  end
20
26
 
21
- def get_single_dictionary_translations(word, service)
22
- case service
27
+ # returns hash with structure as showed below
28
+ # { 'TRANSLATION' => ['EXAMPLE', ...], ... }
29
+ def get_single_dictionary_translations(word, dictionary)
30
+ case dictionary
23
31
  when 'wiktionary'
24
- Wiktionary.new(word, WIKI_URL).translate
32
+ Wiktionary.new(word).translate
25
33
  when 'dictpl'
26
- Dictpl.new(word, DICT_URL).translate
34
+ Dictpl.new(word).translate
27
35
  else Dictionary.message
28
36
  end
29
37
  rescue Dictionary::ConnectError
30
- "Couldn't connect to the service."
31
- end
32
-
33
- def print_single_dictionary_translations(word, service)
34
- obj = get_single_dictionary_translations(word, service)
35
- hash = obj.translate
36
- hash.each do |k, v|
37
- puts "#{k} - #{v}"
38
- end
38
+ "Couldn't connect to the dictionary."
39
39
  end
40
40
 
41
- def to_json(hash)
42
- hash.to_json
41
+ # prints translations from single dictionary
42
+ def print_single_dictionary_translations(word, dictionary)
43
+ puts "Word '#{word.upcase}' translations from #{dictionary.upcase} dictionary."
44
+ puts get_single_dictionary_translations(word, dictionary).to_yaml
43
45
  end
44
46
 
45
- def available_services
47
+ # returns array of currently available dictionaries
48
+ def available_dictionaries
46
49
  ['wiktionary', 'dictpl']
47
50
  end
48
51
  end
@@ -5,13 +5,16 @@ require 'open-uri'
5
5
  module Dict
6
6
  class Dictionary
7
7
  attr_accessor :translations, :examples
8
- def initialize(word, url)
8
+
9
+ def initialize(word)
9
10
  check_arguments(word)
10
11
  @translations = []
11
12
  @examples = []
12
- @uri = URI(URI.escape(url + word.downcase.tr(' ', '_')))
13
+ @word = word
13
14
  end
14
15
 
16
+ # returns hash with structure as showed below
17
+ # { 'TRANSLATION' => ['EXAMPLE', ...], ... }
15
18
  def make_hash_results(arr)
16
19
  hash = arr.each_slice(2).inject({}) do |h, (key, value)|
17
20
  if h.has_key?(key)
@@ -23,7 +26,13 @@ module Dict
23
26
  @translations, @examples = hash.keys, hash.values
24
27
  hash
25
28
  end
29
+
30
+ # returns an instance of URI::HTTP class
31
+ def uri(url, word = nil)
32
+ word == nil ? URI(URI.escape(url)) : URI(URI.escape(url + word.downcase.tr(' ', '_')))
33
+ end
26
34
 
35
+ # checks if word was given correctly
27
36
  def check_arguments(word)
28
37
  raise ArgumentError.new("No given word") if word.empty?
29
38
  end
@@ -34,6 +43,7 @@ module Dict
34
43
 
35
44
  class ConnectError < Exception
36
45
  attr_reader :original
46
+
37
47
  def initialize(original = $!)
38
48
  @original = original
39
49
  end
data/lib/dict/dictpl.rb CHANGED
@@ -7,10 +7,11 @@ DICT_URL = 'http://dict.pl/dict?word='
7
7
 
8
8
  module Dict
9
9
  class Dictpl < Dictionary
10
- # Method returns hash with translations as keys and examples of using words as values
10
+ # returns hash with structure as showed below
11
+ # { 'TRANSLATION' => ['EXAMPLE', ...], ... }
11
12
  def translate
12
13
  context_words = []
13
- Nokogiri::HTML(open(@uri)).xpath('//td[@class="resWordCol"]/a').each do |node|
14
+ Nokogiri::HTML(open(uri(DICT_URL, @word))).xpath('//td[@class="resWordCol"]/a').each do |node|
14
15
  context_words << node.text
15
16
  end
16
17
  make_hash_results(context_words)
data/lib/dict/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dict
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -7,12 +7,12 @@ WIKI_URL = 'http://en.wiktionary.org/wiki/'
7
7
 
8
8
  module Dict
9
9
  class Wiktionary < Dictionary
10
- # Method returns hash with translations as keys and examples of using words as values
10
+ # returns hash with structure as showed below
11
+ # { 'TRANSLATION' => ['EXAMPLE', ...], ... }
11
12
  def translate
12
13
  context_words = []
13
- url = 'http://en.wiktionary.org/wiki/'
14
- get_html(@uri).css('p + ol li a').each do |node|
15
- get_html(url + node.text.tr(' ', '_')).css('p + ol > li dl dd').each do |example|
14
+ get_html(uri(WIKI_URL, @word)).css('p + ol li a').each do |node|
15
+ get_html(uri(WIKI_URL, node.text)).css('p + ol > li dl dd').each do |example|
16
16
  context_words << node.text << example.text
17
17
  end
18
18
  end
@@ -0,0 +1,92 @@
1
+ # -*- coding: utf-8 -*
2
+ require 'dict/cli/runner'
3
+
4
+ describe "parameters_valid?" do
5
+ it "should return false if ARGV is empty" do
6
+ stub_const("ARGV", [])
7
+ runner = Dict::CLI::Runner.new
8
+ runner.parameters_valid?.should == false
9
+ end
10
+
11
+ it "should return true if ARGV is not empty" do
12
+ stub_const("ARGV", ["słowik", "-t", "36", "-d"])
13
+ runner = Dict::CLI::Runner.new
14
+ runner.parameters_valid?.should == true
15
+ end
16
+ end
17
+
18
+ describe "parse_parameters" do
19
+ it "should return Hash for parameters słowik -t 36" do
20
+ stub_const("ARGV", ["słowik", "-t", "36"])
21
+ runner = Dict::CLI::Runner.new
22
+ opts = runner.parse_parameters
23
+ {:help=>nil, :time=>"36", :dict=>nil}.should == opts.to_hash
24
+ end
25
+
26
+ it "should return Hash for parameters słowik" do
27
+ stub_const("ARGV", ["słowik"])
28
+ runner = Dict::CLI::Runner.new
29
+ opts = runner.parse_parameters
30
+ {:help=>nil, :time=>nil, :dict=>nil}.should == opts.to_hash
31
+ end
32
+ end
33
+
34
+
35
+ describe "get_translations" do
36
+ it "should return results from wiktionary and dictpl for word 'słowik'" do
37
+ stub_const("ARGV", ["słowik"])
38
+ runner = Dict::CLI::Runner.new
39
+ opts = runner.parse_parameters
40
+ runner.get_translations(opts, "słowik").should == {"wiktionary"=>{}, "dictpl"=>{"słowik"=>["nightingale"], "słowik białobrewy; Luscinia indicus; Tarsiger indicus (gatunek ptaka)"=>["white-browed bush-robin"], "słowik białosterny; Luscinia pectoralis (gatunek ptaka)"=>["Himalayan rubythroat", "white-tailed rubythroat"], "słowik chiński; pekińczyk żółty; Leiothrix lutea"=>["Pekin robin", "red-billed leiothrix"], "słowik chiński; pekińczyk żółty; pekińczyk koralodzioby; Leiothrix lutea"=>["Peking robin"], "słowik czarnogardły; Luscinia obscura"=>["black-throated blue robin"], "słowik himalajski; Luscinia brunnea (gatunek ptaka)"=>["Indian blue chat", "Indian blue robin"], "słowik modry; Luscinia cyane"=>["Siberian blue robin"], "słowik obrożny; Luscinia johnstoniae; Tarsiger johnstoniae (gatunek ptaka)"=>["collared bush-robin"]}}
41
+ end
42
+
43
+ it "should return results from selected dictionary for word 'słowik'" do
44
+ stub_const("ARGV", ["słowik", "-d", "dictpl"])
45
+ runner = Dict::CLI::Runner.new
46
+ opts = runner.parse_parameters
47
+ runner.get_translations(opts, "słowik").should == {"słowik"=>["nightingale"], "słowik białobrewy; Luscinia indicus; Tarsiger indicus (gatunek ptaka)"=>["white-browed bush-robin"], "słowik białosterny; Luscinia pectoralis (gatunek ptaka)"=>["Himalayan rubythroat", "white-tailed rubythroat"], "słowik chiński; pekińczyk żółty; Leiothrix lutea"=>["Pekin robin", "red-billed leiothrix"], "słowik chiński; pekińczyk żółty; pekińczyk koralodzioby; Leiothrix lutea"=>["Peking robin"], "słowik czarnogardły; Luscinia obscura"=>["black-throated blue robin"], "słowik himalajski; Luscinia brunnea (gatunek ptaka)"=>["Indian blue chat", "Indian blue robin"], "słowik modry; Luscinia cyane"=>["Siberian blue robin"], "słowik obrożny; Luscinia johnstoniae; Tarsiger johnstoniae (gatunek ptaka)"=>["collared bush-robin"]}
48
+ end
49
+
50
+ it "should return timeout message for word słowik and -t 5" do
51
+ stub_const("ARGV", ["słowik","-t","5"])
52
+ runner = Dict::CLI::Runner.new
53
+ opts = runner.parse_parameters
54
+ Dict.should_receive(:get_all_dictionaries_translations).
55
+ and_return { sleep 20 }
56
+ runner.get_translations(opts, "słowik").should == "Timeout for the query."
57
+ end
58
+ end
59
+
60
+ describe "CLI::Runner" do
61
+ HELP_MSG = "Usage: dict WORD [OPTIONS]\nSearch WORD in dict, an open source dictionary aggregator.\n\n -h, --help Display this help message\n -t, --time Set timeout in seconds. Default: 300\n -d, --dict Select desired dictionary. Available options: wiktionary, dictpl"
62
+ DICT_MSG = "Missing argument. Expected: wiktionary, dictpl"
63
+ TIME_MSG = "Missing argument. Expected: number of seconds"
64
+
65
+ it "should call abort when program is called with -h" do
66
+ stub_const("ARGV",["-h"])
67
+ opts = Slop.new
68
+ runner = Dict::CLI::Runner.new
69
+ runner.should_receive(:abort).with(HELP_MSG).and_raise(SystemExit)
70
+ expect {
71
+ runner.run
72
+ }.to raise_error(SystemExit)
73
+ end
74
+
75
+ it "should try to display meaningful information when -d option arguments are missing" do
76
+ stub_const("ARGV",["-d"])
77
+ runner = Dict::CLI::Runner.new
78
+ runner.should_receive(:abort).with(DICT_MSG).and_raise(SystemExit)
79
+ expect {
80
+ runner.run
81
+ }.to raise_error(SystemExit)
82
+ end
83
+
84
+ it "should try to display meaningful information when -t option arguments are missing" do
85
+ stub_const("ARGV",["-t"])
86
+ runner = Dict::CLI::Runner.new
87
+ runner.should_receive(:abort).with(TIME_MSG).and_raise(SystemExit)
88
+ expect {
89
+ runner.run
90
+ }.to raise_error(SystemExit)
91
+ end
92
+ end
@@ -14,8 +14,14 @@ describe Dict do
14
14
  Dict.get_all_dictionaries_translations('samochód').should be_a(Hash)
15
15
  end
16
16
 
17
- it "should return array of available services" do
18
- Dict.available_services.should be_a(Array)
17
+ it "should return array of available services which is not empty" do
18
+ arr = Dict.available_dictionaries
19
+ arr.should be_a(Array)
20
+ arr.size.should_not == 0
19
21
  end
20
22
 
23
+ it "should return array of available services which contains wiktionary and dictpl" do
24
+ Dict.available_dictionaries.should == ['wiktionary', 'dictpl']
25
+ end
26
+
21
27
  end
@@ -1,5 +1,6 @@
1
1
  # -*- encoding: utf-8 -*
2
2
 
3
+ require_relative './vcr_setup'
3
4
  require 'dict/dictpl'
4
5
 
5
6
  describe Dict::Dictpl do
@@ -7,27 +8,48 @@ describe Dict::Dictpl do
7
8
  it "should raise no given word exception" do
8
9
  expect { Dict::Dictpl.new }.to raise_error ArgumentError
9
10
  end
10
-
11
- it "should return hash for given word: 'samochód'" do
12
- result = Dict::Dictpl.new('samochód', DICT_URL).translate
13
- result.should be_a(Hash)
11
+
12
+ it "should return array with translations for 'krowa'" do
13
+ VCR.use_cassette('translations_krowa_cassette') do
14
+ d = Dict::Dictpl.new("krowa")
15
+ d.translate
16
+ d.translations.should be_a(Array)
17
+ d.translations.should == ["krowa", "krowa bliska wycielenia", "krowa mleczna", "krowa morska; Hydrodamalis gigas", "krowa morska; krowa morska Stellera; Hydrodamalis gigas (wymarły ssak morski)", "krowa nie zapłodniona", "krowa po pierwszym ocieleniu się", "krowa wysokomleczna", "cielna krowa", "dojna krowa", "mleczna krowa", "wata cukrowa", "mąka cukrowa", "święta krowa", "trzcina cukrowa", "lukier; polewa lukrowa", "święta krowa [przen]"]
18
+ end
14
19
  end
15
-
16
- it "should return array with translations" do
17
- d = Dict::Dictpl.new('samochód', DICT_URL)
18
- d.translate
19
- d.translations.should be_a(Array)
20
+
21
+ it "should return array with examples for 'krowa'" do
22
+ VCR.use_cassette('examples_krowa_cassette') do
23
+ d = Dict::Dictpl.new("krowa")
24
+ d.translate
25
+ d.examples.should be_a(Array)
26
+ d.examples.should == [["cow"], ["freshen of cow"], ["milker", "milcher", "milk cow", "dairy cow", "milch cow"], ["Steller's sea cow"], ["Steller's sea cow"], ["barren cow"], ["cow heifer"], ["deep milking cow"], ["springer"], ["milch-cow"], ["milk cow"], ["candyfloss", "candy floss", "candy-floss", "cotton candy"], ["false grain"], ["sacred moose"], ["sugarcane"], ["icing"], ["sacred cow"]]
27
+ end
20
28
  end
21
-
22
- it "should return array with examples of translated words" do
23
- d = Dict::Dictpl.new('samochód', DICT_URL)
24
- d.translate
25
- d.examples.should be_a(Array)
29
+
30
+ it "should return array with translations for 'samochód'" do
31
+ VCR.use_cassette('translations_samochod_cassette') do
32
+ d = Dict::Dictpl.new('samochód')
33
+ d.translate
34
+ d.translations.should be_a(Array)
35
+ d.translations.should == ["samochód", "samochód ciężarowy", "samochód ciężarowy; samochód skrzyniowy", "samochód cysterna (rodzaj pojazdu)", "samochód do przewozu wojska [wojsk]", "samochód dostawczy", "samochód dwudrzwiowy", "docierać samochód", "kasować samochód", "pojazd; samochód", "duży i tani samochód", "wypożyczony samochód", "uruchomić samochód", "krążownik szos; duży amerykański samochód", "zapalać samochód (zwłaszcza na pych)", "kabriolet; samochód bez dachu", "uruchomić samochód bez uzycia kluczyków (zwłaszcza, aby go ukraść)", "ciężarówka; samochód ciężarowy"]
36
+ end
37
+ end
38
+
39
+ it "should return array with examples for 'samochód'" do
40
+ VCR.use_cassette('examples_samochod_cassette') do
41
+ d = Dict::Dictpl.new('samochód')
42
+ d.translate
43
+ d.examples.should be_a(Array)
44
+ d.examples.should == [["car", "autocar", "automobile"], ["lorry"], ["truck"], ["gas tanker", "petrol tanker", "gasoline tanker"], ["troop-carrying vehicle"], ["van", "cargo plane"], ["two-door car"], ["break in"], ["write off"], ["automobile"], ["flivver"], ["u-drive"], ["put the car in gear"], ["yank-tank"], ["bump-start", "push-start"], ["no-top"], ["hot-wire"], ["lorry"]]
45
+ end
26
46
  end
27
47
 
28
48
  it "should return a hash from array of paired values" do
29
- d = Dict::Dictpl.new('samochód', DICT_URL)
30
- d.make_hash_results(d.translate).should be_a(Hash)
49
+ VCR.use_cassette('paired_value_samochod_cassette') do
50
+ d = Dict::Dictpl.new('samochód')
51
+ d.make_hash_results(d.translate).should be_a(Hash)
52
+ end
31
53
  end
32
-
54
+
33
55
  end
@@ -9,25 +9,25 @@ describe Dict::Wiktionary do
9
9
  end
10
10
 
11
11
  it "should return an two element array of translations of word samochód containing [\"car\",\"automobile\"]" do
12
- w = Dict::Wiktionary.new('samochód', WIKI_URL)
12
+ w = Dict::Wiktionary.new('samochód')
13
13
  w.translate
14
14
  w.translations.should == ["car", "automobile"]
15
15
  end
16
16
 
17
17
  it "should return array with translations" do
18
- w = Dict::Wiktionary.new('samochód', WIKI_URL)
18
+ w = Dict::Wiktionary.new('samochód')
19
19
  w.translate
20
20
  w.translations.should be_a(Array)
21
21
  end
22
22
 
23
23
  it "should return array with examples of translated words" do
24
- w = Dict::Wiktionary.new('samochód', WIKI_URL)
24
+ w = Dict::Wiktionary.new('samochód')
25
25
  w.translate
26
26
  w.examples.should be_a(Array)
27
27
  end
28
28
 
29
29
  it "should return a hash from array of paired values" do
30
- w = Dict::Wiktionary.new('samochód', WIKI_URL)
30
+ w = Dict::Wiktionary.new('samochód')
31
31
  w.make_hash_results(w.translate).should be_a(Hash)
32
32
  end
33
33