nebrija 1.0.9 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -0
- data/.travis.yml +4 -3
- data/README.md +36 -24
- data/bin/debug +1 -1
- data/lib/nebrija/rae.rb +32 -15
- data/lib/nebrija/version.rb +2 -2
- data/script/cibuild +9 -0
- data/test/mocks/cli_output +1 -0
- data/test/mocks/{error.html → error} +0 -0
- data/test/mocks/{multiple.html → multiple} +0 -0
- data/test/mocks/{noscript.html → noscript} +0 -0
- data/test/mocks/{single.html → single} +0 -0
- data/test/test_rae.rb +20 -17
- metadata +8 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b95042bf3ffa9ca78eab2ce3260555933c485ae1
|
4
|
+
data.tar.gz: 4134ce0676ac430ea22bb6566d0f1c7a2abbac13
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 475ea89fd7ed720b6d106b66b9e6fd1a7ed1ddcec94336cf932879147a897b3669f06f81cea4eece68cc7b011aaff706b63416410f680493f94ed4b799621a8c
|
7
|
+
data.tar.gz: 2f5a5cfd780e960189aa6a588def16cda593490990ef4e4bbd3e41e1cbdd7204d4d56eb8e084f612900867b349edbbd88ebffd8cfbdc90b983a8cc10ee625029
|
data/.rubocop.yml
CHANGED
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,51 +1,63 @@
|
|
1
|
-
Nebrija: A rae parser
|
2
|
-
=====================
|
1
|
+
## Nebrija: A rae parser
|
3
2
|
|
4
3
|
[![Build Status](https://travis-ci.org/javierhonduco/nebrija.svg?branch=master)](https://travis-ci.org/javierhonduco/nebrija)
|
5
4
|
[![Code Climate](https://img.shields.io/codeclimate/github/javierhonduco/nebrija.svg)](https://codeclimate.com/github/javierhonduco/nebrija)
|
6
5
|
|
7
6
|
|
8
|
-
|
9
|
-
-------
|
10
|
-
|
7
|
+
### Installing
|
11
8
|
```bash
|
12
9
|
$ gem install nebrija
|
13
10
|
```
|
14
11
|
|
15
|
-
|
16
|
-
---
|
12
|
+
### Usage
|
17
13
|
```ruby
|
18
14
|
require 'nebrija'
|
19
|
-
res = Rae.
|
15
|
+
res = Rae.search('word/id') # e.g. Rae.search('amigo')
|
16
|
+
|
20
17
|
puts res # prints the results
|
21
18
|
```
|
22
19
|
|
23
|
-
`
|
20
|
+
`Rae#search` returns a hash where the key `status` can be [`error`|`success`].
|
24
21
|
|
25
|
-
If the request is
|
22
|
+
If the request is successful, the key `type` indicates whether the result type is a single word or multiple with [`single`|`multiple`].
|
26
23
|
|
27
|
-
The response data can be found in the `response` key which holds
|
24
|
+
The response data can be found in the `response` key which holds a hash with all the information.
|
28
25
|
|
29
|
-
|
26
|
+
An example on how to iterate over the info can be seen in the [CLI code](https://github.com/javierhonduco/nebrija/blob/master/lib/nebrija/cli.rb) file:
|
30
27
|
|
31
|
-
CLI
|
32
|
-
---------
|
28
|
+
### CLI
|
33
29
|
```bash
|
34
30
|
$ nebrija <word>
|
35
31
|
```
|
36
32
|
|
37
|
-
|
33
|
+
If you feel it is too cumbersome to type, you can add an alias :)
|
38
34
|
|
39
|
-
|
40
|
-
---------------------------------------
|
35
|
+
### Projects using this gem
|
41
36
|
* [Rae downloader](https://github.com/raul/rae-downloader) by [@raul](https://github.com/raul)
|
42
37
|
* [Dulcinea: a nebrija frontend](https://github.com/javierhonduco/dulcinea)
|
43
38
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
39
|
+
### Contributing
|
40
|
+
Feel free to submit any issue or PR 😃.
|
41
|
+
|
42
|
+
In order to test your changes:
|
43
|
+
```bash
|
44
|
+
# you can run the tests
|
45
|
+
$ bundle exec rake [test]
|
46
|
+
# run the CLI
|
47
|
+
$ bundle exec nebrija word-to-be-searched
|
48
|
+
# run the "debug" CLI, which pretty prints the resulting hash
|
49
|
+
$ bundle exec bin/debug word-to-be-searched
|
50
|
+
```
|
51
|
+
|
52
|
+
Additionally, you can get some debug traces in stderr via the `NEBRIJA_DEBUG` environment variable.
|
53
|
+
|
54
|
+
When set, it will output the requested word, the generated URL, and the HTTP status code that it got from the server.
|
55
|
+
If you wish to see the returned body's output as well, you can set it to `2`.
|
56
|
+
|
57
|
+
The tests are [mocked](https://en.wikipedia.org/wiki/Mock_object) so we run our tests using some already downloaded answers from rae.es. However, in some cases, one would like to bypass the mocks and use the real webpages instead. You can achieve that by setting the `NO_MOCK` env var when running your tests.
|
58
|
+
|
59
|
+
It was added as part as the continuous integrations testing with Travis. They introduced an amazing feature, 💛💚💙💖[scheduled builds](https://blog.travis-ci.com/2016-12-06-the-crons-are-here). Given that rae.es changes the way its [auth method](https://github.com/javierhonduco/nebrija/blob/086f1cc0341cad538b9c72406fe76bbb6d5d4394/lib/nebrija/rae.rb#L38-L49) works from time to time, the tests without being mocked are run in a daily basis so we know if something is broken.
|
60
|
+
### TODO
|
51
61
|
* Make the parser more readable.
|
62
|
+
* Improve the API.
|
63
|
+
* Think on multiple responses and how to address them.
|
data/bin/debug
CHANGED
data/lib/nebrija/rae.rb
CHANGED
@@ -8,40 +8,57 @@ class Rae
|
|
8
8
|
OPEN_TIMEOUT = 2
|
9
9
|
READ_TIMEOUT = 3
|
10
10
|
|
11
|
+
def self.search(word)
|
12
|
+
new.search word
|
13
|
+
end
|
14
|
+
|
15
|
+
# old method, mantained for compatibility
|
11
16
|
def search(word)
|
12
17
|
Parser.new(query(word)).parse
|
13
18
|
end
|
14
19
|
|
15
|
-
private
|
16
|
-
|
17
20
|
def query(word)
|
18
21
|
uri = URI.parse "#{SEARCH_URL}?w=#{CGI.escape(word)}".encode('iso-8859-1')
|
19
22
|
|
23
|
+
request = Net::HTTP::Post.new(uri)
|
24
|
+
request['User-Agent'] = USER_AGENT
|
25
|
+
request.form_data = form_data
|
26
|
+
|
20
27
|
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
21
28
|
http.open_timeout = OPEN_TIMEOUT
|
22
29
|
http.read_timeout = READ_TIMEOUT
|
23
30
|
|
24
|
-
request = Net::HTTP::Post.new(uri)
|
25
|
-
request['User-Agent'] = USER_AGENT
|
26
|
-
request.form_data = form_data
|
27
|
-
|
28
31
|
response = http.request request
|
32
|
+
debug(word, uri, response.code, response.body) if ENV['NEBRIJA_DEBUG']
|
29
33
|
|
30
34
|
response.body
|
31
35
|
end
|
32
36
|
end
|
33
37
|
|
34
|
-
private
|
35
38
|
def form_data
|
36
39
|
{
|
37
|
-
'TS017111a7_id'
|
38
|
-
'TS017111a7_cr'
|
39
|
-
'TS017111a7_76'
|
40
|
-
'TS017111a7_86'
|
41
|
-
'TS017111a7_md'
|
42
|
-
'TS017111a7_rf'
|
43
|
-
'TS017111a7_ct'
|
44
|
-
'TS017111a7_pd'
|
40
|
+
'TS017111a7_id': '3',
|
41
|
+
'TS017111a7_cr': '1895c885a17201dca76eb401d01fd59f:jlmn:U9YRi5sw:1485055093',
|
42
|
+
'TS017111a7_76': '0',
|
43
|
+
'TS017111a7_86': '0',
|
44
|
+
'TS017111a7_md': '1',
|
45
|
+
'TS017111a7_rf': '0',
|
46
|
+
'TS017111a7_ct': '0',
|
47
|
+
'TS017111a7_pd': '0'
|
45
48
|
}
|
46
49
|
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def debug(word, url, status_code, body)
|
54
|
+
STDERR.puts <<-DOC
|
55
|
+
[debug] #{'=' * 40}
|
56
|
+
|
57
|
+
word: #{word}
|
58
|
+
url: #{url}
|
59
|
+
status_code: #{status_code}
|
60
|
+
DOC
|
61
|
+
STDERR.puts "body: `#{body}`" if ENV['NEBRIJA_DEBUG'].to_i == 2
|
62
|
+
STDERR.puts
|
63
|
+
end
|
47
64
|
end
|
data/lib/nebrija/version.rb
CHANGED
data/script/cibuild
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Sentimiento hacia otra persona que naturalmente nos atrae y que, procurando reciprocidad en el deseo de unión, nos completa, alegra y da energía para convivir, comunicarnos y crear
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
data/test/test_rae.rb
CHANGED
@@ -1,31 +1,34 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class TestRae < Minitest::Test
|
4
|
+
def setup
|
5
|
+
WebMock.disable! if ENV['NO_MOCK']
|
6
|
+
end
|
7
|
+
|
4
8
|
def test_cli_basic
|
5
9
|
word = 'amor'
|
6
|
-
|
7
|
-
.to_return(status: 200, body: mock('single'))
|
10
|
+
stub(word, :single)
|
8
11
|
|
9
12
|
out, = capture_io do
|
10
13
|
Nebrija.cli(word)
|
11
14
|
end
|
12
15
|
|
13
|
-
assert_match
|
16
|
+
assert_match mock(:cli_output).gsub(/\s+/, ''), out.gsub(/\s+/, '')
|
14
17
|
end
|
15
18
|
|
16
19
|
def test_error_basic
|
17
|
-
|
18
|
-
|
20
|
+
word = 'wadus'
|
21
|
+
stub(word, :error)
|
19
22
|
|
20
|
-
search = Rae.
|
23
|
+
search = Rae.search('wadus')
|
21
24
|
assert_equal search[:status], 'error'
|
22
25
|
end
|
23
26
|
|
24
27
|
def test_single_basic
|
25
|
-
|
26
|
-
|
28
|
+
word = 'amor'
|
29
|
+
stub(word, :single)
|
27
30
|
|
28
|
-
search = Rae.
|
31
|
+
search = Rae.search(word)
|
29
32
|
assert_equal 'success', search[:status]
|
30
33
|
assert_equal 'single', search[:type]
|
31
34
|
|
@@ -34,10 +37,10 @@ class TestRae < Minitest::Test
|
|
34
37
|
end
|
35
38
|
|
36
39
|
def test_multiple_basic
|
37
|
-
|
38
|
-
|
40
|
+
word = 'banco'
|
41
|
+
stub(word, :multiple)
|
39
42
|
|
40
|
-
search = Rae.
|
43
|
+
search = Rae.search(word)
|
41
44
|
assert_equal 'success', search[:status]
|
42
45
|
assert_equal 'multiple', search[:type]
|
43
46
|
assert_equal 4, search[:response].length
|
@@ -47,12 +50,12 @@ class TestRae < Minitest::Test
|
|
47
50
|
|
48
51
|
private
|
49
52
|
|
50
|
-
def
|
51
|
-
|
53
|
+
def stub(word, mock_name)
|
54
|
+
stub_request(:post, "#{Rae::SEARCH_URL}?w=#{word}")
|
55
|
+
.to_return(status: 200, body: mock(mock_name))
|
52
56
|
end
|
53
57
|
|
54
|
-
def
|
55
|
-
#
|
56
|
-
'Sentimiento hacia otra persona que naturalmente nos atrae y que, procurando reciprocidad en el deseo de unión, nos completa, alegra y da energía para convivir, comunicarnos y crear'
|
58
|
+
def mock(mock_name)
|
59
|
+
File.read("#{File.expand_path(File.dirname(__FILE__))}/mocks/#{mock_name}")
|
57
60
|
end
|
58
61
|
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: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- javierhonduco
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-03-
|
11
|
+
date: 2017-03-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -102,10 +102,12 @@ files:
|
|
102
102
|
- lib/nebrija/rae.rb
|
103
103
|
- lib/nebrija/version.rb
|
104
104
|
- nebrija.gemspec
|
105
|
-
-
|
106
|
-
- test/mocks/
|
107
|
-
- test/mocks/
|
108
|
-
- test/mocks/
|
105
|
+
- script/cibuild
|
106
|
+
- test/mocks/cli_output
|
107
|
+
- test/mocks/error
|
108
|
+
- test/mocks/multiple
|
109
|
+
- test/mocks/noscript
|
110
|
+
- test/mocks/single
|
109
111
|
- test/test_helper.rb
|
110
112
|
- test/test_rae.rb
|
111
113
|
homepage: http://rubygems.org/gems/nebrija
|