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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b05db641a064b02de12be55a7213ebd7a8c33032
4
- data.tar.gz: bd51dda0fee5787e964959ae8fc5b43204553407
3
+ metadata.gz: b95042bf3ffa9ca78eab2ce3260555933c485ae1
4
+ data.tar.gz: 4134ce0676ac430ea22bb6566d0f1c7a2abbac13
5
5
  SHA512:
6
- metadata.gz: d0ecbd51c7867bec5cdd08e8af68db16a63b8c2c41c63d1cfd2cf91d37db38883208aea672e59833179e04dbbb312add2606e41320f1eb8a6e0af95ad0327c9c
7
- data.tar.gz: 126b5dd142c1958043178b7d1c8b68010f67c6b56a6e183cd16e541946f3cc614119fc525836065a766580dae4f820b9e95646959c5add7d0af355f5197421a0
6
+ metadata.gz: 475ea89fd7ed720b6d106b66b9e6fd1a7ed1ddcec94336cf932879147a897b3669f06f81cea4eece68cc7b011aaff706b63416410f680493f94ed4b799621a8c
7
+ data.tar.gz: 2f5a5cfd780e960189aa6a588def16cda593490990ef4e4bbd3e41e1cbdd7204d4d56eb8e084f612900867b349edbbd88ebffd8cfbdc90b983a8cc10ee625029
data/.rubocop.yml CHANGED
@@ -1,3 +1,7 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+ Style/FrozenStringLiteralComment:
4
+ Enabled: False
1
5
  Metrics/LineLength:
2
6
  Max: 85
3
7
  Metrics/MethodLength:
data/.travis.yml CHANGED
@@ -1,6 +1,7 @@
1
- sudo: false
2
1
  language: ruby
2
+ cache: bundler
3
3
  rvm:
4
4
  - 2.3.1
5
- cache: bundler
6
-
5
+ install: bundle install
6
+ script: script/cibuild
7
+ sudo: false
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
- Install
9
- -------
10
-
7
+ ### Installing
11
8
  ```bash
12
9
  $ gem install nebrija
13
10
  ```
14
11
 
15
- API
16
- ---
12
+ ### Usage
17
13
  ```ruby
18
14
  require 'nebrija'
19
- res = Rae.new.search('word/id')
15
+ res = Rae.search('word/id') # e.g. Rae.search('amigo')
16
+
20
17
  puts res # prints the results
21
18
  ```
22
19
 
23
- `HTTPRae` returns a hash where the key `status` can be `error`|`success`.
20
+ `Rae#search` returns a hash where the key `status` can be [`error`|`success`].
24
21
 
25
- If the request is succesful, the key `type` indicates whether the result type is a single word or multiple with `single`|`multiple`.
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 an array.
24
+ The response data can be found in the `response` key which holds a hash with all the information.
28
25
 
29
- The response data is formatted as shown in the [nebrija/cli](https://github.com/javierhonduco/nebrija/blob/master/lib/nebrija/cli.rb) file:
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 searching
32
- ---------
28
+ ### CLI
33
29
  ```bash
34
30
  $ nebrija <word>
35
31
  ```
36
32
 
37
- Maybe you are lazy and want to add an alias to `rae` or `es` like I do :)
33
+ If you feel it is too cumbersome to type, you can add an alias :)
38
34
 
39
- Friend projects/ project using this gem
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
- TODO
45
- ----
46
- * Improve the API.
47
- * Update clients to use new API.
48
- * Fix multiple responses.
49
- * Better doc.
50
- * Mockless testing.
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
@@ -4,4 +4,4 @@ require 'json'
4
4
  require 'pp'
5
5
  require 'nebrija'
6
6
 
7
- pp Rae.new.search(ARGV.first)
7
+ pp Rae.search(ARGV.first)
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' => '3',
38
- 'TS017111a7_cr' => '1895c885a17201dca76eb401d01fd59f:jlmn:U9YRi5sw:1485055093',
39
- 'TS017111a7_76' => '0',
40
- 'TS017111a7_86' => '0',
41
- 'TS017111a7_md' => '1',
42
- 'TS017111a7_rf' => '0',
43
- 'TS017111a7_ct' => '0',
44
- 'TS017111a7_pd' => '0',
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
@@ -1,7 +1,7 @@
1
1
  module Nebrija
2
2
  MAJOR = 1
3
- MINOR = 0
4
- PATCH = 9
3
+ MINOR = 1
4
+ PATCH = 0
5
5
 
6
6
  VERSION = [MAJOR, MINOR, PATCH].join('.')
7
7
  end
data/script/cibuild ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env bash
2
+ set -ex
3
+
4
+ if [ "$TRAVIS_EVENT_TYPE" = "cron" ]; then
5
+ echo "=> running non mocked test as environment='cron'"
6
+ NO_MOCK=1 bundle exec rake test
7
+ else
8
+ bundle exec rake test
9
+ fi
@@ -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
- stub_request(:post, "#{Rae::SEARCH_URL}?w=#{word}")
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 mock_cli.gsub(/\s+/, ''), out.gsub(/\s+/, '')
16
+ assert_match mock(:cli_output).gsub(/\s+/, ''), out.gsub(/\s+/, '')
14
17
  end
15
18
 
16
19
  def test_error_basic
17
- stub_request(:post, "#{Rae::SEARCH_URL}?w=wadus")
18
- .to_return(status: 200, body: mock('error'))
20
+ word = 'wadus'
21
+ stub(word, :error)
19
22
 
20
- search = Rae.new.search('wadus')
23
+ search = Rae.search('wadus')
21
24
  assert_equal search[:status], 'error'
22
25
  end
23
26
 
24
27
  def test_single_basic
25
- stub_request(:post, "#{Rae::SEARCH_URL}?w=amor")
26
- .to_return(status: 200, body: mock('single'))
28
+ word = 'amor'
29
+ stub(word, :single)
27
30
 
28
- search = Rae.new.search('amor')
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
- stub_request(:post, "#{Rae::SEARCH_URL}?w=banco")
38
- .to_return(status: 200, body: mock('multiple'))
40
+ word = 'banco'
41
+ stub(word, :multiple)
39
42
 
40
- search = Rae.new.search('banco')
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 mock(mock_name)
51
- File.read("#{File.expand_path(File.dirname(__FILE__))}/mocks/#{mock_name}.html")
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 mock_cli
55
- # rubocop:disable LineLength
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.9
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-04 00:00:00.000000000 Z
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
- - test/mocks/error.html
106
- - test/mocks/multiple.html
107
- - test/mocks/noscript.html
108
- - test/mocks/single.html
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