dictuby 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f781c7716139aeec617f877c4ff93ced7f133325
4
+ data.tar.gz: b74509bb17a8e517958f36d8ec0c3f8fe792cc28
5
+ SHA512:
6
+ metadata.gz: b0fc2b5d8046e762d9c9ebc2af58ce0f1f5fac924f5dffd84e7baaf162896ab4e6c179dcfaf22c82b4d6496e7dad6c1572ed2c94d67995b2b2dc637384176c8d
7
+ data.tar.gz: 5dd23373c8aed7f632775cd2c7c7999254ffe3c84c8a4ea0072ec538c1489d017808d247fc021e079a59f5f93a1c4df42799915f3335ff3b53da298597100ab0
data/bin/dictuby ADDED
@@ -0,0 +1,5 @@
1
+ #!/bin/env ruby20
2
+
3
+ require 'dictuby'
4
+
5
+ Dictuby::Runner.execute(ARGV)
@@ -0,0 +1,19 @@
1
+
2
+ module Dictuby
3
+ class ArgsParser
4
+ def self.parse(args)
5
+ options = {}
6
+
7
+ OptionParser.new do |o|
8
+ o.on('-h') { puts o; exit }
9
+ o.on('-l') { |b| options[:list] = b }
10
+ o.on('-g') { |b| options[:get] = b }
11
+ o.on('-s DICTIONARY') { |d| options[:set] = d }
12
+ o.on('QUERY') { }
13
+ o.parse!
14
+ end
15
+
16
+ options
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,36 @@
1
+
2
+ module Dictuby
3
+ class DictubyConfig
4
+ def initialize
5
+ @default_dict = 'en-sk'
6
+ @config_path = File.expand_path('~') + '/.dictuby'
7
+ @settings = nil
8
+
9
+ if File.exists?(@config_path)
10
+ File.open(@config_path, 'r') do |content|
11
+ @settings = YAML.load(content)
12
+ end
13
+ end
14
+
15
+ if !@settings
16
+ @settings = {}
17
+ set('dictionary', @default_dict)
18
+ end
19
+ end
20
+
21
+ def get(key)
22
+ @settings[key]
23
+ end
24
+
25
+ def set(key, value)
26
+ @settings[key] = value
27
+ save
28
+ end
29
+
30
+ def save
31
+ f = File.new(@config_path, 'w')
32
+ f.write(YAML.dump(@settings))
33
+ f.close
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,22 @@
1
+
2
+ module Dictuby
3
+ class OnlineDictionary
4
+ @url = ''
5
+ @dicts = {}
6
+
7
+ def self.lookup(dict, query)
8
+ url = @url % {
9
+ :dict => @dicts[dict],
10
+ :query => query,
11
+ }
12
+
13
+ page = Nokogiri::HTML(open(url))
14
+
15
+ self.process(page, query)
16
+ end
17
+
18
+ def self.dicts
19
+ @dicts
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,48 @@
1
+
2
+ module Dictuby
3
+ class Runner
4
+ def self.execute(args)
5
+ new(args).run
6
+ end
7
+
8
+ def initialize(args)
9
+ @options = ArgsParser.parse(args)
10
+ @config = DictubyConfig.new
11
+ @args = args
12
+
13
+ @sources = [SlovnikAzetSK, SlovnikCZ]
14
+
15
+ @dicts = @sources.inject([]) {|r, p| r << p.dicts.keys}
16
+ @dicts.flatten!.sort!
17
+ end
18
+
19
+ def run
20
+ if @options[:list]
21
+ @dicts.each {|d| puts d}
22
+ elsif @options[:set]
23
+ if @dicts.include?(@options[:set])
24
+ @config.set('dictionary', @options[:set])
25
+ else
26
+ puts 'Invalid dictionary'
27
+ end
28
+ elsif @options[:get]
29
+ puts @config.get('dictionary')
30
+ else
31
+ if @args.size == 1
32
+ dict, query = @config.get('dictionary'), @args[0]
33
+ elsif @args.size == 2
34
+ dict, query = *@args
35
+ end
36
+
37
+ map = @sources.inject({}) do |r,p|
38
+ p.dicts.keys.each {|d| r[d] = p}; r
39
+ end
40
+
41
+ puts query
42
+ map[dict].lookup(dict, query).each do |word|
43
+ puts "- #{word}"
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,35 @@
1
+
2
+ module Dictuby
3
+ class SlovnikAzetSK < OnlineDictionary
4
+ @name = 'slovnik.azet.sk'
5
+ @dicts = {
6
+ 'en-sk' => 'anglicko-slovensky',
7
+ 'sk-en' => 'slovensko-anglicky',
8
+ 'ge-sk' => 'nemecko-slovensky',
9
+ 'sk-ge' => 'slovensko-nemecky',
10
+ 'fr-sk' => 'francuzsko-slovensky',
11
+ 'sk-fr' => 'slovensko-francuzsky',
12
+ 'sp-sk' => 'spanielsko-slovensky',
13
+ 'sk-sp' => 'slovensko-spanielsky',
14
+ 'hu-sk' => 'madarsko-slovensky',
15
+ 'sk-hu' => 'slovensko-madarsky',
16
+ 'it-sk' => 'taliansko-slovensky',
17
+ 'sk-it' => 'slovensko-taliansky',
18
+ 'ru-sk' => 'rusko-slovensky',
19
+ 'sk-ru' => 'slovensko-rusky',
20
+ }
21
+ @url = "http://slovnik.azet.sk/preklad/%{dict}/?q=%{query}"
22
+
23
+ def self.process(page, query)
24
+ left = page.xpath('//table[1]//span')[0].text rescue nil
25
+ return [] unless left == query
26
+ right = page.xpath(
27
+ '//table[1]//tr/td[@class="do"]/span'
28
+ ).inject([]) {
29
+ |r, e| r << e.text
30
+ }
31
+
32
+ right
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,29 @@
1
+
2
+ module Dictuby
3
+ class SlovnikCZ < OnlineDictionary
4
+ @name = 'slovnik.cz'
5
+ @dicts = {
6
+ 'en-cz' => 'encz.en',
7
+ 'cz-en' => 'encz.cz',
8
+ 'ge-cz' => 'gecz.ge',
9
+ 'cz-ge' => 'gecz.cz',
10
+ 'fr-cz' => 'frcz.fr',
11
+ 'cz-fr' => 'frcz.cz',
12
+ 'it-cz' => 'itcz.it',
13
+ 'cz-it' => 'itcz.cz',
14
+ 'sp-cz' => 'spcz.sp',
15
+ 'cz-sp' => 'spcz.cz',
16
+ }
17
+ @url = "http://slovnik.cz/bin/mld.fpl?vcb=%{query}&dictdir=%{dict}&lines=30&js=0"
18
+
19
+ def self.process(page, query)
20
+ pairs = page.xpath('//div[@class="pair"]')
21
+
22
+ pairs.inject([]) do |xright, pair|
23
+ left = pair.xpath('span[@class="l"]')[0].text
24
+ return xright if left != query
25
+ xright << pair.xpath('span[@class="r"]')[0].text
26
+ end
27
+ end
28
+ end
29
+ end
data/lib/dictuby.rb ADDED
@@ -0,0 +1,12 @@
1
+
2
+ require 'optparse'
3
+ require 'yaml'
4
+ require 'nokogiri'
5
+ require 'open-uri'
6
+
7
+ require 'dictuby/runner'
8
+ require 'dictuby/args_parser'
9
+ require 'dictuby/config'
10
+ require 'dictuby/online_dictionary'
11
+ require 'dictuby/sources/slovnik_azet_sk'
12
+ require 'dictuby/sources/slovnik_cz'
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dictuby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Rene Klacan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: command line online dictionary
14
+ email: rene.klacan@gmail.com
15
+ executables:
16
+ - dictuby
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/dictuby.rb
21
+ - lib/dictuby/online_dictionary.rb
22
+ - lib/dictuby/sources/slovnik_azet_sk.rb
23
+ - lib/dictuby/sources/slovnik_cz.rb
24
+ - lib/dictuby/config.rb
25
+ - lib/dictuby/args_parser.rb
26
+ - lib/dictuby/runner.rb
27
+ - bin/dictuby
28
+ homepage: ''
29
+ licenses:
30
+ - MIT
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 2.0.7
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: command line online dictionary
52
+ test_files: []