slg 0.0.1 → 0.0.2

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
  SHA256:
3
- metadata.gz: 138f7e46a189d5f94aec8149144f460938dff2f74e3f86650ac323874be91c3e
4
- data.tar.gz: 7b5b412986a25443036a2f8dabef508d2b59242884542f443837a9389c512bda
3
+ metadata.gz: b05bc7815eaa79ffe25e225b5b8a1908bcfad62e90dbfec6bc81ce1f6ff3f299
4
+ data.tar.gz: d963e579557f017156743a2473bdba08f5be1e1f4993c326bd91c7d799a4885b
5
5
  SHA512:
6
- metadata.gz: b9c3b92bad04d21f66128e0cfdd1cc6001f8a70eb3323c86b2e42dafa95756d7af2ff5e4a109c9ccc70e6876d6d50bd8ca9fd19ad176098bce6152520e35e071
7
- data.tar.gz: 8e04ce6ff7b6086edcfc408587a8f451861c2a0055859da63c92f8c53d177946413f52dd2727e2a5ab2ccbdf7525096f4429f32c51544ebf44d0816a5b61fde2
6
+ metadata.gz: b69fe8ab127a4cbfab3edbe725f70fbcb358fe35ef77d059b1169d3bb35353453a40e28af248e311f993aa230469c1213b039e217600feb5651c0335626f799c
7
+ data.tar.gz: 4bd6ec8d45aa60b94c8bdabed519d15f20a122a6753376dd0d20df2b605c8558fab9387cb39d69f42b5d6054f4404b3b8988a37883a9687fc11a814398e1341d
Binary file
data.tar.gz.sig CHANGED
Binary file
data/lib/slg.rb CHANGED
@@ -1,20 +1,16 @@
1
1
  # -*- coding: UTF-8 -*-
2
2
 
3
3
  require "uri"
4
- require "open-uri"
5
4
  require "nokogiri"
6
5
 
7
- require "ud/formatting"
8
-
9
- # Most of this code is copy/pasted from UD.
10
- # TODO generalize the code in UD and reuse it here.
6
+ require "defcli"
11
7
 
12
8
  # This module provide some methods to scrape definitions from Slengo.it.
13
9
  module Slg
14
10
  class << self
15
11
  # @return [String] the current gem's version
16
12
  def version
17
- "0.0.1"
13
+ "0.0.2"
18
14
  end
19
15
 
20
16
  WWW_ROOT = "https://slengo.it"
@@ -33,7 +29,7 @@ module Slg
33
29
  # are allowed.
34
30
  # @return [Nil]
35
31
  def open_url(term)
36
- system open_cmd, search_url(term)
32
+ Defcli.open_in_browser search_url(term)
37
33
  end
38
34
 
39
35
  # Query the website and return a list of definitions for the provided term.
@@ -44,7 +40,7 @@ module Slg
44
40
  # @return [Array<Hash>]
45
41
  def query(term, opts = {})
46
42
  url = search_url(term)
47
- text = OpenURI.open_uri url
43
+ text = Defcli.read_url url
48
44
 
49
45
  opts = { :count => 1 }.merge(opts || {})
50
46
 
@@ -53,19 +49,19 @@ module Slg
53
49
  doc = Nokogiri::HTML.parse text
54
50
 
55
51
  doc.css("article.definition-card").map do |elt|
56
- examples = elt.css(".word-examples li").map(&:text).map(&:strip)
57
- example = examples.join "\n\n"
52
+ examples = elt.css(".word-examples li").map do |ex|
53
+ ex.text.strip
54
+ end
58
55
 
59
56
  votes = elt.css(".votes-container .v-progress-linear")
60
57
  # Slengo.it uses percentages instead of number of votes
61
- upvotes = votes.attr("aria-valuenow").to_s.to_i
62
- downvotes = 100 - upvotes
58
+ ratio = votes.attr("aria-valuenow").to_s.to_f / 100.0
63
59
 
64
60
  header_p = elt.css("header p").first
65
61
 
66
62
  definition = elt.css("div.word-definition").text
67
63
  # Remove 'see also'
68
- definition.gsub! /\bCfr\..+/m, ""
64
+ definition.gsub!(/\bCfr\..+/m, "")
69
65
  definition.strip!
70
66
 
71
67
  {
@@ -75,9 +71,8 @@ module Slg
75
71
  :word => header_p.text.strip,
76
72
  :permalink => url,
77
73
  :definition => definition,
78
- :example => example,
79
- :upvotes => upvotes,
80
- :downvotes => downvotes,
74
+ :examples => examples,
75
+ :ratio => ratio,
81
76
  # TODO add region as well
82
77
  }
83
78
 
@@ -86,22 +81,11 @@ module Slg
86
81
 
87
82
  # Format results for output
88
83
  # @param results [Array] this must be an array of results, as returned by
89
- # +UD.query+.
84
+ # +Slg.query+.
90
85
  # @param color [Boolean] colored output
91
86
  # @return [String]
92
87
  def format_results(results, color = true)
93
- UD::Formatting.text(results, color)
94
- end
95
-
96
- def open_cmd
97
- case RbConfig::CONFIG["host_os"]
98
- when /darwin/
99
- "open"
100
- when /bsd|linux/
101
- "xdg-open"
102
- when /cygwin|mingw|mswin/
103
- "start"
104
- end
88
+ Defcli.format_results(results, color)
105
89
  end
106
90
  end
107
91
  end
@@ -8,18 +8,16 @@ class Slg_Query_test < Test::Unit::TestCase
8
8
  @foo, @bar = [
9
9
  {
10
10
  :definition=>"Foo foo.",
11
- :downvotes=>0,
12
- :example=> "foo ex 1\n\nfoo ex 2",
11
+ :ratio=>1.0,
12
+ :examples=> ["foo ex 1", "foo ex 2"],
13
13
  :permalink=>"https://slengo.it/define/culo",
14
- :upvotes=>100,
15
14
  :word=>"foo",
16
15
  },
17
16
  {
18
17
  :definition=>"Bar bar.",
19
- :downvotes=>10,
20
- :example=>"bar ex",
18
+ :ratio=>0.9,
19
+ :examples=>["bar ex"],
21
20
  :permalink=>"https://slengo.it/define/culo",
22
- :upvotes=>90,
23
21
  :word=>"foo",
24
22
  }
25
23
  ]
@@ -23,30 +23,9 @@ for t in Dir.glob( File.join( test_dir, '*_tests.rb' ) )
23
23
  end
24
24
 
25
25
  class SlgTests < Test::Unit::TestCase
26
-
27
26
  def test_slg_version
28
27
  assert(Slg.version =~ /^\d+\.\d+\.\d+/)
29
28
  end
30
-
31
- def test_slg_open_cmd
32
- os = RbConfig::CONFIG["host_os"]
33
-
34
- RbConfig::CONFIG["host_os"] = "darwin"
35
- assert_equal "open", Slg.send(:open_cmd)
36
-
37
- RbConfig::CONFIG["host_os"] = "linux"
38
- assert_equal "xdg-open", Slg.send(:open_cmd)
39
-
40
- RbConfig::CONFIG["host_os"] = "bsd"
41
- assert_equal "xdg-open", Slg.send(:open_cmd)
42
-
43
- RbConfig::CONFIG["host_os"] = "cygwin"
44
- assert_equal "start", Slg.send(:open_cmd)
45
-
46
- ensure
47
- RbConfig::CONFIG["host_os"] = os
48
- end
49
-
50
29
  end
51
30
 
52
31
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Baptiste Fontaine
@@ -33,50 +33,22 @@ cert_chain:
33
33
  Qe5fflfwGHtlmT6SWZoQD48xF/ILJ+cLTpF79ardGYnWT6N7VUAI5lKp7OjZYd+q
34
34
  wwYlmlBW0Femxy5WoxzECQ==
35
35
  -----END CERTIFICATE-----
36
- date: 2020-03-23 00:00:00.000000000 Z
36
+ date: 2020-04-02 00:00:00.000000000 Z
37
37
  dependencies:
38
38
  - !ruby/object:Gem::Dependency
39
- name: optimist
39
+ name: defcli
40
40
  requirement: !ruby/object:Gem::Requirement
41
41
  requirements:
42
42
  - - "~>"
43
43
  - !ruby/object:Gem::Version
44
- version: '3.0'
44
+ version: '0.0'
45
45
  type: :runtime
46
46
  prerelease: false
47
47
  version_requirements: !ruby/object:Gem::Requirement
48
48
  requirements:
49
49
  - - "~>"
50
50
  - !ruby/object:Gem::Version
51
- version: '3.0'
52
- - !ruby/object:Gem::Dependency
53
- name: colored
54
- requirement: !ruby/object:Gem::Requirement
55
- requirements:
56
- - - "~>"
57
- - !ruby/object:Gem::Version
58
- version: '1.2'
59
- type: :runtime
60
- prerelease: false
61
- version_requirements: !ruby/object:Gem::Requirement
62
- requirements:
63
- - - "~>"
64
- - !ruby/object:Gem::Version
65
- version: '1.2'
66
- - !ruby/object:Gem::Dependency
67
- name: ud
68
- requirement: !ruby/object:Gem::Requirement
69
- requirements:
70
- - - "~>"
71
- - !ruby/object:Gem::Version
72
- version: '0.3'
73
- type: :runtime
74
- prerelease: false
75
- version_requirements: !ruby/object:Gem::Requirement
76
- requirements:
77
- - - "~>"
78
- - !ruby/object:Gem::Version
79
- version: '0.3'
51
+ version: '0.0'
80
52
  - !ruby/object:Gem::Dependency
81
53
  name: nokogiri
82
54
  requirement: !ruby/object:Gem::Requirement
metadata.gz.sig CHANGED
Binary file