nebrija 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/nebrija +37 -2
- data/lib/nebrija/parser.rb +4 -5
- data/test/test_basic.rb +5 -5
- metadata +1 -1
data/bin/nebrija
CHANGED
@@ -3,7 +3,42 @@
|
|
3
3
|
require 'json'
|
4
4
|
require 'nebrija'
|
5
5
|
|
6
|
+
|
7
|
+
arg = ARGV.first
|
8
|
+
|
9
|
+
if arg.nil?
|
10
|
+
puts 'Type smthg!'
|
11
|
+
return
|
12
|
+
end
|
13
|
+
|
6
14
|
puts 'Oh, hai!'
|
7
|
-
puts 'In future versions this will print something more human readable.'
|
8
15
|
puts
|
9
|
-
|
16
|
+
|
17
|
+
response = HTTPRae.new.search(arg)
|
18
|
+
|
19
|
+
status = response[:status]
|
20
|
+
|
21
|
+
if status == 'error'
|
22
|
+
puts 'Word does not exist. :(.'
|
23
|
+
else
|
24
|
+
type = response[:type]
|
25
|
+
data = response[:response]
|
26
|
+
|
27
|
+
if type == 'multiple'
|
28
|
+
puts 'We found multiple words matching your search criteria:'
|
29
|
+
puts 'Search by id to find their meaning.'
|
30
|
+
puts
|
31
|
+
data.each_with_index do |entry, i|
|
32
|
+
puts "#{i+1}. #{entry[:word]} ~> #{entry[:id]}"
|
33
|
+
end
|
34
|
+
else
|
35
|
+
|
36
|
+
data.each_with_index do |entry, i|
|
37
|
+
puts "#{i+1}. #{entry[:word]}"
|
38
|
+
entry[:meanings].each_with_index do |definitions, j|
|
39
|
+
puts "\t#{j+1}. #{definitions[:word]}"
|
40
|
+
puts "\t\tMeta: #{definitions[:meta]}" if !definitions[:meta].nil?
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/nebrija/parser.rb
CHANGED
@@ -24,8 +24,7 @@ class Parser
|
|
24
24
|
|
25
25
|
private
|
26
26
|
def parse_single
|
27
|
-
|
28
|
-
result = {:id => @doc.css('body > div > a').first['name'].to_i, :data => data}
|
27
|
+
single_data = []
|
29
28
|
state = :entry # TODO. Improve FSM syntax.
|
30
29
|
index = -1 # HACK(javierhonduco)
|
31
30
|
|
@@ -33,7 +32,7 @@ class Parser
|
|
33
32
|
if entry['class'] == 'p' and state == :entry
|
34
33
|
word = entry.css('span').inner_text
|
35
34
|
word = '=>' if word == ''
|
36
|
-
|
35
|
+
single_data << {
|
37
36
|
:word => word.gsub(/~/, @word).strip.capitalize,
|
38
37
|
:meanings => []
|
39
38
|
}
|
@@ -43,7 +42,7 @@ class Parser
|
|
43
42
|
next if text[0] == '(' # Del latín, Nil.
|
44
43
|
unparsed_meta = text.scan META_REGEX
|
45
44
|
text = text.gsub(META_REGEX, '')
|
46
|
-
|
45
|
+
single_data[index][:meanings] << {
|
47
46
|
:word => text,
|
48
47
|
:meta => (unparsed_meta.join.strip if unparsed_meta.join.strip != ''),
|
49
48
|
} if !text.nil? and text != ''
|
@@ -51,7 +50,7 @@ class Parser
|
|
51
50
|
end
|
52
51
|
state = :entry
|
53
52
|
end
|
54
|
-
|
53
|
+
single_data
|
55
54
|
end
|
56
55
|
|
57
56
|
def parse_multiple
|
data/test/test_basic.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
require 'nebrija'
|
3
3
|
|
4
|
-
MOCKS_DIR = "#{
|
4
|
+
MOCKS_DIR = "#{File.expand_path(File.dirname(__FILE__))}/mocks"
|
5
5
|
|
6
6
|
class TestMockedParserBasic < Test::Unit::TestCase
|
7
7
|
|
@@ -21,7 +21,7 @@ end
|
|
21
21
|
class TestMockedParserContent < Test::Unit::TestCase
|
22
22
|
|
23
23
|
def test_single_basic
|
24
|
-
assert FileRae.new.search("#{MOCKS_DIR}/single.html")[:response]
|
24
|
+
assert FileRae.new.search("#{MOCKS_DIR}/single.html")[:response].length > 20
|
25
25
|
end
|
26
26
|
|
27
27
|
def test_multiple_basic
|
@@ -34,7 +34,7 @@ end
|
|
34
34
|
class TestMockedParserBasic < Test::Unit::TestCase
|
35
35
|
|
36
36
|
def test_single_basic_id
|
37
|
-
assert_not_nil HTTPRae.new.search('MHpGWYJ6YDXX2bw9Ghwm')[:response]
|
37
|
+
assert_not_nil HTTPRae.new.search('MHpGWYJ6YDXX2bw9Ghwm')[:response]
|
38
38
|
end
|
39
39
|
|
40
40
|
def test_error_basic
|
@@ -42,7 +42,7 @@ class TestMockedParserBasic < Test::Unit::TestCase
|
|
42
42
|
end
|
43
43
|
|
44
44
|
def test_single_basic
|
45
|
-
assert_not_nil HTTPRae.new.search('a')[:response]
|
45
|
+
assert_not_nil HTTPRae.new.search('a')[:response]
|
46
46
|
end
|
47
47
|
|
48
48
|
def test_multiple_basic
|
@@ -53,7 +53,7 @@ end
|
|
53
53
|
class TestParserContent < Test::Unit::TestCase
|
54
54
|
|
55
55
|
def test_single_basic
|
56
|
-
assert HTTPRae.new.search('a')[:response]
|
56
|
+
assert HTTPRae.new.search('a')[:response].length > 4
|
57
57
|
end
|
58
58
|
|
59
59
|
def test_multiple_basic
|