dict 0.0.6 → 0.0.7

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.
Files changed (4) hide show
  1. data/bin/translate +11 -25
  2. data/lib/dict.rb +2 -2
  3. data/lib/wiktionary.rb +9 -1
  4. metadata +1 -1
data/bin/translate CHANGED
@@ -8,33 +8,19 @@ if ARGV.empty? then puts "Please enter the word. (-h for help)" ; exit end
8
8
 
9
9
  opts = Slop.parse do
10
10
  banner "Usage: $translate -w word [Options]"
11
- on '-w', '--word', 'after this option is a word to translate'
12
- on '-h', '--help', 'help'
13
- on '-t', '--time', 'time in seconds', :as => :int
14
- on '-d', '--dict', 'dictonaries: all, wiki etc.'
15
- on '-s', '--status', 'status of API'
11
+ on '-w', :word=, 'after this option is a word to translate'
12
+ on '-h', :help=, 'help', :argument => :optional
13
+ on '-t', :time=, 'time in seconds, default: 300 seconds', :as => :int
14
+ on '-d', :dict=, 'dictionaries: all, wiki etc., default is all dictionaries'
15
+ on '-s', :status=, 'status of API', :argument => :optional
16
16
  end
17
17
 
18
- puts opts.to_hash
19
-
20
- =begin
21
- # problem with situation where first word after
22
- # translate is --help or -h ...
23
- unless ARGV[0].nil?
24
- puts Dict::Translation.get_response(ARGV[0])
25
- end
26
-
27
- unless opts[:time].nil?
28
- puts Dict::Translation.get_response(ARGV[0],opts[:time])
29
- end
30
-
31
- unless opts[:dict].nil?
32
- # todo: refactor get_response because it has not a dict
33
- # parameter
34
- end
35
- =end
36
- puts opts if opts[:help]
37
- puts Dict::Translation.status unless opts[:status].nil?
18
+ # translation
19
+ puts Dict::Translation.get_response(opts[:word], opts[:time] || 300, opts[:dict] || "all") if opts.word?
38
20
 
21
+ # help
22
+ puts opts if opts.help?
39
23
 
24
+ # status
25
+ puts Dict::Translation.status if opts.status?
40
26
 
data/lib/dict.rb CHANGED
@@ -17,8 +17,8 @@ module Dict
17
17
  res.message
18
18
  end
19
19
 
20
- def self.get_response(word, time = 3600)
21
- uri = URI.parse(URI.escape("http://dict-app-staging.shellyapp.com/#{word}"))
20
+ def self.get_response(word, time, dict)
21
+ uri = URI.parse(URI.escape("http://dict-app-staging.shellyapp.com/#{dict}/#{word}"))
22
22
  begin
23
23
  Timeout::timeout(time.to_i) do
24
24
  res = Net::HTTP.get(uri)
data/lib/wiktionary.rb CHANGED
@@ -9,6 +9,11 @@ class Wiktionary
9
9
  @uri = URI(URI.escape(WIKI_URL + escaped_word))
10
10
  end
11
11
 
12
+ #
13
+ # Method returns an array of translations and word usage examples
14
+ # For instance to given word 'samochód' it returns:
15
+ # [["car","automobile"],["She drove her car to the mall.", "The conductor linked the cars to the locomotive.", "The 11:10 to London was operated by a 4-car diesel multiple unit"]
16
+ #
12
17
  def translate
13
18
  req = Net::HTTP::Get.new(@uri.path)
14
19
  response, translations, sentences = nil, [], []
@@ -16,6 +21,9 @@ class Wiktionary
16
21
  response = http.request(req).body
17
22
 
18
23
  doc = Nokogiri::HTML(response)
24
+ doc.css('div#mw-content-text h2:first .mw-headline').each do |lang|
25
+ raise "Given word is not polish." if lang.content != 'Polish'
26
+ end
19
27
  doc.css('div#mw-content-text[lang=en] ol > li a').each do |link|
20
28
  translations.push link.content
21
29
  end
@@ -23,7 +31,7 @@ class Wiktionary
23
31
  translations.each do |item|
24
32
  escaped_item = item.tr(' ', '_')
25
33
  sentence = Nokogiri::HTML(Net::HTTP.get(URI(WIKI_URL + escaped_item)))
26
- sentence.css('div#mw-content-text[lang=en] ol > li dl dd i').each do |s|
34
+ sentence.css('div#mw-content-text[lang=en] ol:first > li dl dd i').each do |s|
27
35
  sentences.push s.content
28
36
  end
29
37
  end
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.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: