dict 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/bin/translate +28 -14
  2. data/lib/dict.rb +14 -9
  3. data/lib/wiktionary.rb +29 -37
  4. metadata +18 -2
data/bin/translate CHANGED
@@ -4,20 +4,34 @@
4
4
  require 'dict'
5
5
  require 'slop'
6
6
 
7
- if ARGV.empty?
8
- puts "Please, enter the word to translate."
9
- exit
7
+ if ARGV.empty? then puts "Please enter the word. (-h for help)" ; exit end
8
+
9
+ opts = Slop.parse do
10
+ banner "Usage: $translate word [Options] (-h for help)"
11
+ on '-h', '--help', 'help'
12
+ on '-t', '--time', 'time in seconds', :as => :int
13
+ on '-d', '--dict', 'dictonaries: all, wiki etc.'
14
+ on '-s', '--status', 'status of API'
15
+ end
16
+
17
+ # problem with situation where first word after
18
+ # translate is --help or -h ...
19
+ unless ARGV[0].nil?
20
+ puts Dict::Translation.get_response(ARGV[0])
21
+ end
22
+
23
+ unless opts[:time].nil?
24
+ puts Dict::Translation.get_response(ARGV[0],opts[:time])
10
25
  end
11
26
 
12
- if ARGV[0] == "status"
13
- print "Status API: "
14
- print Dict::Translation.status
15
- elsif ARGV[1] == "time"
16
- if !ARGV[2].nil?
17
- Dict::Translation.getResponse(ARGV[0], ARGV[2])
18
- else
19
- Dict::Translation.getResponse(ARGV[0])
20
- end
21
- else
22
- Dict::Translation.getResponse(ARGV[0])
27
+ unless opts[:dict].nil?
28
+ # todo: refactor get_response because it has not a dict
29
+ # parameter
23
30
  end
31
+
32
+ puts opts if opts[:help]
33
+ puts Dict::Translation.status unless opts[:status].nil?
34
+
35
+
36
+
37
+
data/lib/dict.rb CHANGED
@@ -7,24 +7,29 @@ require 'timeout'
7
7
 
8
8
  module Dict
9
9
  class Translation
10
- def initialize(word)
10
+ def initialize
11
11
  @word = ARGV[0]
12
12
  end
13
13
 
14
14
  def self.status(adres = "http://dict-app-staging.shellyapp.com/")
15
15
  uri = URI(adres)
16
16
  res = Net::HTTP.get_response(uri)
17
- puts res.message
17
+ res.message
18
18
  end
19
19
 
20
- def self.getResponse(word, time = 3600)
21
- uri = URI.parse(URI.escape("http://dict-app-staging.shellyapp.com/#{word}"))
22
- Timeout::timeout(time.to_i) do
23
- puts Net::HTTP.get(uri)
20
+ def self.get_response(word, time = 3600)
21
+ uri = URI.parse(URI.escape("http://dict-app-staging.shellyapp.com/#{word}"))
22
+ begin
23
+ Timeout::timeout(time.to_i) do
24
+ res = Net::HTTP.get(uri)
25
+ "Success" unless res.empty?
26
+ end
27
+
28
+ rescue => ex
29
+ puts "Timeout for the query."
30
+ {:code => 408, :describe => "Request Timeout"}
31
+ end
24
32
  end
25
- end
26
-
27
-
28
33
  end
29
34
  end
30
35
 
data/lib/wiktionary.rb CHANGED
@@ -1,40 +1,32 @@
1
- require 'net/http' # requires 'uri'
1
+ require 'net/http'
2
+ require 'nokogiri'
2
3
 
3
4
  class Wiktionary
4
- WIKI_URL = "http://en.wiktionary.org/wiki/" # #{word}
5
- def initialize(word)
6
- if word.empty? then puts "No word given."; exit end
7
- @word = word
8
- @uri = URI(URI.escape(WIKI_URL << @word))
9
- end
10
-
11
- def translate
12
- req = Net::HTTP::Get.new(@uri.path)
13
- response, array = nil, []
14
- Net::HTTP.start(@uri.host, @uri.port) do |http|
15
- response = http.request(req).body
16
- regexp = Regexp.new('<li>[^<]*<a href="/wiki/([^"]+)"\s+title="\1">\1<\/a>', Regexp::IGNORECASE)
17
- results = regexp.match(response)
18
-
19
- return array if !results # no translation found
20
-
21
- array.push results[1]
22
- end
23
- end
24
-
25
- def uri_debug
26
- puts "--- DEBUG start ---"
27
- puts "uri:\t\t" << @uri.to_s
28
- puts "scheme:\t\t" << @uri.scheme
29
- puts "host:\t\t" << @uri.host
30
- puts "query:\t\t" << @uri.query if @uri.query
31
- puts "path:\t\t" << @uri.path if @uri.path
32
- puts "fragment:\t" << @uri.fragment if @uri.fragment
33
- puts "--- DEBUG end ---"
34
- end
5
+ WIKI_URL = "http://en.wiktionary.org/wiki/"
6
+ def initialize(word)
7
+ if word.empty? then raise ArgumentError, "No word given." end
8
+ @uri = URI(URI.escape(WIKI_URL + word.downcase))
9
+ end
10
+
11
+ def translate
12
+ req = Net::HTTP::Get.new(@uri.path)
13
+ response, translations, sentences = nil, [], []
14
+ Net::HTTP.start(@uri.host, @uri.port) do |http|
15
+ response = http.request(req).body
16
+
17
+ doc = Nokogiri::HTML(response)
18
+ doc.css('div#mw-content-text[lang=en] ol > li a').each do |link|
19
+ translations.push link.content
20
+ end
21
+
22
+ translations.each do |item|
23
+ sentence = Nokogiri::HTML(Net::HTTP.get(URI(WIKI_URL + item)))
24
+ sentence.css('div#mw-content-text[lang=en] ol > li dl dd i').each do |s|
25
+ sentences.push s.content
26
+ end
27
+ end
28
+
29
+ end
30
+ [translations, sentences]
31
+ end
35
32
  end
36
-
37
- word = ARGV[0]
38
- word ||= ''
39
- translation = Wiktionary.new word
40
- puts translation.translate.to_s
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.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -28,8 +28,24 @@ dependencies:
28
28
  - - ! '>='
29
29
  - !ruby/object:Gem::Version
30
30
  version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: nokogiri
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
31
47
  description: ! " Dict is a client of API which you lets you use multiple dictionaries.\n
32
- \ Dict is a OpenSource Project.\n"
48
+ \ Dict is an open source project.\n"
33
49
  email:
34
50
  - mtczerwinski@gmail.com
35
51
  executables: