nebrija 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/nebrija +2 -2
- data/lib/nebrija/parser.rb +22 -28
- data/lib/nebrija.rb +8 -35
- data/test/test_rae.rb +42 -0
- metadata +18 -4
- data/test/test_basic.rb +0 -63
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3468bc9fdc5a9179a3afdca07498df03222d5d82
|
4
|
+
data.tar.gz: b1fff36971dd0891331f7ae9ca13b5a557ec552c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4eeccc878c3b06ed6802f0c90f56c8f273b8f6ed189bacd523c5d7b27836d3db803c68430944e56add28e40778ebdfe505d0789fbbe7c6344e2428d2d115a73d
|
7
|
+
data.tar.gz: 32ca4f4a95be26aac62a2538e1e408a6d04dacca7ea5fcb290f77d503e2e0b38d3b0b0264db740b0b0f07471cb31cd2733061522e3c77501b2893de541c667ea
|
data/bin/nebrija
CHANGED
@@ -14,7 +14,7 @@ end
|
|
14
14
|
puts 'Oh, hai!'
|
15
15
|
puts
|
16
16
|
|
17
|
-
response =
|
17
|
+
response = Rae.new.search(arg)
|
18
18
|
|
19
19
|
status = response[:status]
|
20
20
|
|
@@ -36,7 +36,7 @@ else
|
|
36
36
|
data.each_with_index do |entry, i|
|
37
37
|
puts "#{i+1}. #{entry[:word]}"
|
38
38
|
entry[:meanings].each_with_index do |definitions, j|
|
39
|
-
puts "\t#{j+1}. #{definitions[:
|
39
|
+
puts "\t#{j+1}. #{definitions[:definition]}"
|
40
40
|
puts "\t\tMeta: #{definitions[:meta]}" if !definitions[:meta].nil?
|
41
41
|
end
|
42
42
|
end
|
data/lib/nebrija/parser.rb
CHANGED
@@ -1,27 +1,36 @@
|
|
1
1
|
require 'nokogiri'
|
2
2
|
|
3
3
|
class Parser
|
4
|
-
|
4
|
+
|
5
5
|
META_REGEX = /^([a-zA-Z]{1,4}+\.[ ]{1,2})+/
|
6
6
|
|
7
7
|
def initialize(rae_data, word)
|
8
8
|
@doc = Nokogiri::HTML(rae_data
|
9
|
-
.gsub(
|
10
|
-
.gsub(
|
11
|
-
@word = word
|
9
|
+
.gsub(/\n+/, '')
|
10
|
+
.gsub(/\s{2,}+/, ' '))
|
12
11
|
end
|
13
12
|
|
14
13
|
def parse
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
if valid?
|
15
|
+
{
|
16
|
+
:status => 'success',
|
17
|
+
:type => single? ? 'single' : 'multiple',
|
18
|
+
:response => single? ? parse_single : parse_multiple
|
19
|
+
}
|
20
|
+
else
|
21
|
+
{
|
22
|
+
:status => 'error',
|
23
|
+
:message => 'Word/id does not exist. Sorry.'
|
24
|
+
}
|
25
|
+
end
|
18
26
|
end
|
19
27
|
|
28
|
+
private
|
29
|
+
|
20
30
|
def single?
|
21
31
|
@doc.css('body > ul').length.zero?
|
22
32
|
end
|
23
33
|
|
24
|
-
private
|
25
34
|
def parse_single
|
26
35
|
single_data = []
|
27
36
|
state = :entry # TODO. Improve FSM syntax.
|
@@ -32,7 +41,7 @@ class Parser
|
|
32
41
|
word = entry.css('span').inner_text
|
33
42
|
word = '=>' if word == ''
|
34
43
|
single_data << {
|
35
|
-
:word => word.strip.capitalize,
|
44
|
+
:word => word.strip.capitalize,
|
36
45
|
:meanings => [],
|
37
46
|
:etymology => nil
|
38
47
|
}
|
@@ -54,18 +63,17 @@ class Parser
|
|
54
63
|
end
|
55
64
|
state = :entry
|
56
65
|
end
|
66
|
+
|
57
67
|
single_data
|
58
68
|
end
|
59
69
|
|
60
70
|
def parse_multiple
|
61
|
-
|
62
|
-
|
63
|
-
multiple_result << {
|
71
|
+
@doc.css('body > ul > li > a').map do |word|
|
72
|
+
{
|
64
73
|
:word => word.css('span').first.inner_text,
|
65
74
|
:id => word['href'].gsub(/search\?id=/, '')
|
66
75
|
}
|
67
|
-
end
|
68
|
-
multiple_result
|
76
|
+
end
|
69
77
|
end
|
70
78
|
|
71
79
|
def valid?
|
@@ -75,20 +83,6 @@ class Parser
|
|
75
83
|
valid_title && valid_body && delete_pending?
|
76
84
|
end
|
77
85
|
|
78
|
-
def perform
|
79
|
-
response = nil
|
80
|
-
if single?
|
81
|
-
response = parse_single
|
82
|
-
else
|
83
|
-
response = parse_multiple
|
84
|
-
end
|
85
|
-
{
|
86
|
-
:status => 'success',
|
87
|
-
:type => if single? then 'single' else 'multiple' end,
|
88
|
-
:response => response
|
89
|
-
}
|
90
|
-
end
|
91
|
-
|
92
86
|
def delete_pending?
|
93
87
|
tb_deleted = true
|
94
88
|
if !@doc.css('body > div > p').nil? && !@doc.css('body > div > p').first.nil?
|
data/lib/nebrija.rb
CHANGED
@@ -1,55 +1,28 @@
|
|
1
|
-
require 'nebrija/parser'
|
2
1
|
require 'typhoeus'
|
3
2
|
require 'uri'
|
3
|
+
require 'nebrija/parser'
|
4
4
|
|
5
5
|
class Rae
|
6
|
+
SEARCH_URL = 'http://lema.rae.es/drae/srv/search'
|
6
7
|
|
7
8
|
def search(word)
|
8
9
|
Parser.new(query(word), word).parse
|
9
10
|
end
|
10
11
|
|
11
12
|
private
|
12
|
-
def query(word)
|
13
|
-
raise 'NotImplementedError'
|
14
|
-
end
|
15
|
-
end
|
16
13
|
|
17
|
-
|
18
|
-
class FileRae < Rae
|
19
|
-
|
20
|
-
private
|
21
|
-
def query(file)
|
22
|
-
IO.read(file)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
|
27
|
-
class HTTPRae < Rae
|
28
|
-
USER_AGENT = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36'
|
29
|
-
SEARCH_URL = 'http://lema.rae.es/drae/srv/search?'
|
30
|
-
ID_REGEX = /[0-9]/
|
31
|
-
|
32
|
-
private
|
33
14
|
def query(word)
|
34
|
-
|
35
|
-
|
36
|
-
params = 'id='
|
37
|
-
params = 'val=' if val?
|
38
|
-
|
15
|
+
params = (word.encode('utf-8') =~ /\d/) ? 'id=' : 'val='
|
16
|
+
|
39
17
|
response = Typhoeus::Request.post(
|
40
|
-
URI.escape("#{SEARCH_URL}
|
41
|
-
body
|
18
|
+
URI.escape("#{SEARCH_URL}?#{params}#{word}".encode('iso-8859-1')),
|
19
|
+
:body => build_body
|
42
20
|
)
|
43
|
-
|
44
|
-
response.body
|
45
|
-
|
46
|
-
end
|
47
21
|
|
48
|
-
|
49
|
-
(@word.encode('utf-8') =~ ID_REGEX).nil?
|
22
|
+
response.body
|
50
23
|
end
|
51
24
|
|
52
|
-
def
|
25
|
+
def build_body
|
53
26
|
{
|
54
27
|
'TS014dfc77_id' => 3,
|
55
28
|
'TS014dfc77_cr' => '42612abd48551544c72ae36bc40f440a%3Akkmj%3AQG60Q2v4%3A1477350835',
|
data/test/test_rae.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestRae < Minitest::Test
|
4
|
+
|
5
|
+
def test_error_basic
|
6
|
+
stub_request(:post, "#{Rae::SEARCH_URL}?val=wadus").
|
7
|
+
to_return(:status => 200, :body => mock('error'), :headers => {})
|
8
|
+
|
9
|
+
search = Rae.new.search('wadus')
|
10
|
+
assert_equal search[:status], 'error'
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_single_basic
|
14
|
+
stub_request(:post, "#{Rae::SEARCH_URL}?val=amor").
|
15
|
+
to_return(:status => 200, :body => mock('single'), :headers => {})
|
16
|
+
|
17
|
+
search = Rae.new.search('amor')
|
18
|
+
assert_equal 'success', search[:status]
|
19
|
+
assert_equal 'single', search[:type]
|
20
|
+
|
21
|
+
first_word = search[:response].first
|
22
|
+
assert_equal 'Amor.', first_word[:word]
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_multiple_basic
|
26
|
+
stub_request(:post, "#{Rae::SEARCH_URL}?val=banco").
|
27
|
+
to_return(:status => 200, :body => mock('multiple'), :headers => {})
|
28
|
+
|
29
|
+
search = Rae.new.search('banco')
|
30
|
+
assert_equal 'success', search[:status]
|
31
|
+
assert_equal 'multiple', search[:type]
|
32
|
+
assert_equal 2, search[:response].length
|
33
|
+
assert_equal 'bancar', search[:response][0][:word]
|
34
|
+
assert_equal 'banco', search[:response][1][:word]
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def mock(mock_name)
|
40
|
+
File.read("#{File.expand_path(File.dirname(__FILE__))}/mocks/#{mock_name}.html")
|
41
|
+
end
|
42
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nebrija
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- '@javierhonduco'
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - ~>
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '5'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: webmock
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.18.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.18.0
|
69
83
|
description: A gem to access the rae dictionary
|
70
84
|
email: javierhonduco@gmail.com
|
71
85
|
executables:
|
@@ -77,7 +91,7 @@ files:
|
|
77
91
|
- lib/nebrija.rb
|
78
92
|
- lib/nebrija/parser.rb
|
79
93
|
- bin/nebrija
|
80
|
-
- test/
|
94
|
+
- test/test_rae.rb
|
81
95
|
homepage: http://rubygems.org/gems/nebrija
|
82
96
|
licenses:
|
83
97
|
- MIT
|
@@ -103,4 +117,4 @@ signing_key:
|
|
103
117
|
specification_version: 4
|
104
118
|
summary: This gem provides access to the rae webpage
|
105
119
|
test_files:
|
106
|
-
- test/
|
120
|
+
- test/test_rae.rb
|
data/test/test_basic.rb
DELETED
@@ -1,63 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require 'nebrija'
|
3
|
-
|
4
|
-
MOCKS_DIR = "#{File.expand_path(File.dirname(__FILE__))}/mocks"
|
5
|
-
|
6
|
-
class TestMockedParserBasic < Minitest::Test
|
7
|
-
|
8
|
-
def test_error_basic
|
9
|
-
assert_equal FileRae.new.search("#{MOCKS_DIR}/error.html")[:status]
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_single_basic
|
13
|
-
assert !FileRae.new.search("#{MOCKS_DIR}/single.html")[:response][:data].nil?
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_multiple_basic
|
17
|
-
assert FileRae.new.search("#{MOCKS_DIR}/multiple.html")[:reponse].length == 2
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
class TestMockedParserContent < Minitest::Test
|
22
|
-
|
23
|
-
def test_single_basic
|
24
|
-
assert FileRae.new.search("#{MOCKS_DIR}/single.html")[:response].length > 20
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_multiple_basic
|
28
|
-
assert FileRae.new.search("#{MOCKS_DIR}/multiple.html")[:response][0][:word] == 'bancar'
|
29
|
-
assert FileRae.new.search("#{MOCKS_DIR}/multiple.html")[:response][1][:word] == 'banco'
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
|
34
|
-
class TestMockedParserBasic < Minitest::Test
|
35
|
-
|
36
|
-
def test_single_basic_id
|
37
|
-
assert !HTTPRae.new.search('MHpGWYJ6YDXX2bw9Ghwm')[:response].nil?
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_error_basic
|
41
|
-
assert HTTPRae.new.search('jddhfgsd')[:status] == 'error'
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_single_basic
|
45
|
-
assert !HTTPRae.new.search('a')[:response].nil?
|
46
|
-
end
|
47
|
-
|
48
|
-
def test_multiple_basic
|
49
|
-
assert HTTPRae.new.search('banco')[:response].length == 2
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
class TestParserContent < Minitest::Test
|
54
|
-
|
55
|
-
def test_single_basic
|
56
|
-
assert HTTPRae.new.search('a')[:response].length > 4
|
57
|
-
end
|
58
|
-
|
59
|
-
def test_multiple_basic
|
60
|
-
assert HTTPRae.new.search('banco')[:response][0][:word] == 'bancar'
|
61
|
-
assert HTTPRae.new.search('banco')[:response][1][:word] == 'banco'
|
62
|
-
end
|
63
|
-
end
|