ud 0.3.2 → 0.4.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
  SHA256:
3
- metadata.gz: 6ea932203fe0c903b5218ce5b146d90654404dc2eebfac0c07e88336aaf37e47
4
- data.tar.gz: f62d445b4149d6a1741b33f95760db705f3992462e3ad8d32a6cf19f594b18d0
3
+ metadata.gz: dcf00a234d982b7fb9457e576eb874f95f155ba00c239017a29bfe735dee1b71
4
+ data.tar.gz: 54826580f57b21b1e680efb66e68e8f88918085c3585dd62e690b7c76eff0a32
5
5
  SHA512:
6
- metadata.gz: e3c13f592deea95981d2315ad90cab736e2fa4ff3eecbc0d277d0f764159fa08c5c5691a485ec2105e7679265151640d10d5ce67f5740b6011735c51ef417fee
7
- data.tar.gz: b4eee17c59a33c63ef6c7f0796c9595f9724c213279b55f0e68d2d89e3f92e508c9f827cc7ea13a5fb387ead4ef18612c9dcac6bdb9cc000d8c7a7fe66be3ed0
6
+ metadata.gz: c639b78accaa812407e51c03acf98e47ba7929b6bf6846e8a0ffbfd0a41ca2a2e98ad663b9fb81eb40af5030028aabe687b15bba3c14fd1db893ffeea434c7bf
7
+ data.tar.gz: dce1bd5cc5efa5710eadae507ed4c85849457a9a1ff0bf7280d65fa331ec4d070c7c0e5884c394ac9f895b477c97425e67a724e11347ac2e2a23805d83dac2bd
Binary file
data.tar.gz.sig CHANGED
Binary file
data/lib/ud.rb CHANGED
@@ -2,9 +2,8 @@
2
2
 
3
3
  require "uri"
4
4
  require "json"
5
- require "open-uri"
6
5
 
7
- require File.dirname(__FILE__) + "/ud/formatting"
6
+ require "defcli"
8
7
 
9
8
  # This module provide some methods to scrape definitions from the Urban
10
9
  # Dictionary website.
@@ -12,7 +11,7 @@ module UD
12
11
  class << self
13
12
  # @return [String] the current gem's version
14
13
  def version
15
- "0.3.2"
14
+ "0.4.0"
16
15
  end
17
16
 
18
17
  API_ROOT = "https://api.urbandictionary.com/v0"
@@ -48,13 +47,13 @@ module UD
48
47
  # are allowed.
49
48
  # @return [Nil]
50
49
  def open_url(term)
51
- system open_cmd, search_url(term, :api => false)
50
+ Defcli.open_in_browser search_url(term, :api => false)
52
51
  end
53
52
 
54
53
  # Open a random definition URL in the user's browser
55
54
  # @return [Nil]
56
55
  def open_random
57
- system open_cmd, random_url(:api => false)
56
+ Defcli.open_in_browser random_url(:api => false)
58
57
  end
59
58
 
60
59
  # Query the website and return a list of definitions for the provided term.
@@ -64,16 +63,16 @@ module UD
64
63
  # +:count+ is the maximum number of results to return
65
64
  # @return [Array<Hash>]
66
65
  def query(term, opts = {})
67
- parse_response(OpenURI.open_uri(search_url(term)).read, opts)
66
+ parse_response(Defcli.read_url(search_url(term)), opts)
68
67
  end
69
68
 
70
69
  # Return a random definition
71
70
  # @param opts [Hash] options.
72
71
  def random(opts = {})
73
- parse_response(OpenURI.open_uri(random_url).read, opts)
72
+ parse_response(Defcli.read_url(random_url), opts)
74
73
  end
75
74
 
76
- # Parse a response from the Urban Dictionnary website.
75
+ # Parse a response from the Urban Dictionary website.
77
76
  # @param opts [Hash] options. This is used by the command-line tool.
78
77
  # +:count+ is the maximum number of results to return
79
78
  # @return [Array<Hash>]
@@ -106,20 +105,7 @@ module UD
106
105
  # @param color [Boolean] colored output
107
106
  # @return [String]
108
107
  def format_results(results, color = true)
109
- UD::Formatting.text(results, color)
110
- end
111
-
112
- private
113
-
114
- def open_cmd
115
- case RbConfig::CONFIG["host_os"]
116
- when /darwin/
117
- "open"
118
- when /bsd|linux/
119
- "xdg-open"
120
- when /cygwin|mingw|mswin/
121
- "start"
122
- end
108
+ Defcli.format_results(results, color)
123
109
  end
124
110
  end
125
111
  end
@@ -23,33 +23,10 @@ for t in Dir.glob( File.join( test_dir, '*_tests.rb' ) )
23
23
  end
24
24
 
25
25
  class UDTests < Test::Unit::TestCase
26
-
27
26
  # == UD#version == #
28
-
29
27
  def test_ud_version
30
28
  assert(UD.version =~ /^\d+\.\d+\.\d+/)
31
29
  end
32
-
33
- # == UD#open_cmd (private) == #
34
-
35
- def test_ud_open_cmd
36
- os = RbConfig::CONFIG["host_os"]
37
-
38
- RbConfig::CONFIG["host_os"] = "darwin"
39
- assert_equal "open", UD.send(:open_cmd)
40
-
41
- RbConfig::CONFIG["host_os"] = "linux"
42
- assert_equal "xdg-open", UD.send(:open_cmd)
43
-
44
- RbConfig::CONFIG["host_os"] = "bsd"
45
- assert_equal "xdg-open", UD.send(:open_cmd)
46
-
47
- RbConfig::CONFIG["host_os"] = "cygwin"
48
- assert_equal "start", UD.send(:open_cmd)
49
-
50
- RbConfig::CONFIG["host_os"] = os
51
- end
52
-
53
30
  end
54
31
 
55
32
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ud
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Baptiste Fontaine
@@ -10,9 +10,9 @@ bindir: bin
10
10
  cert_chain:
11
11
  - |
12
12
  -----BEGIN CERTIFICATE-----
13
- MIIEPDCCAqSgAwIBAgIBATANBgkqhkiG9w0BAQsFADAkMSIwIAYDVQQDDBliL0RD
14
- PXB0aXN0ZWZvbnRhaW5lL0RDPWZyMB4XDTE4MTIwODAwMDU0MFoXDTE5MTIwODAw
15
- MDU0MFowJDEiMCAGA1UEAwwZYi9EQz1wdGlzdGVmb250YWluZS9EQz1mcjCCAaIw
13
+ MIID/DCCAmSgAwIBAgIBAjANBgkqhkiG9w0BAQsFADAkMSIwIAYDVQQDDBliL0RD
14
+ PXB0aXN0ZWZvbnRhaW5lL0RDPWZyMB4XDTIwMDMwNDE5MDMyMVoXDTIxMDMwNDE5
15
+ MDMyMVowJDEiMCAGA1UEAwwZYi9EQz1wdGlzdGVmb250YWluZS9EQz1mcjCCAaIw
16
16
  DQYJKoZIhvcNAQEBBQADggGPADCCAYoCggGBAKdjTwN/gnJ3WHxKeusaRN1o902Q
17
17
  BmIRgdtb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8/rLGqpo+g2TYlqJhdwuA10W2k
35
- H0Dpo4eQ8PCwqU7AgGTli19gHcox4Ne0O+6QaiB4eSw=
24
+ So5Ibqdd3pMKJDUQJvGIBhsQIXF4Iogk4txoPwIDAQABozkwNzAJBgNVHRMEAjAA
25
+ MAsGA1UdDwQEAwIEsDAdBgNVHQ4EFgQUsV1eKnEFtVRwyz6/nTuharH3G+UwDQYJ
26
+ KoZIhvcNAQELBQADggGBAJz7tbRe/iyMthV7fdqnhFv0SZo1UVWZSaIjB39W4lUi
27
+ 7X1Smag2Fe7IqARUPLn2e49ObyupAmTi0zHoSG/+HsBUqNPPoVW1Y67dtrMM49o7
28
+ qSN66EogB4nvGdvqJ9eOlioiAX7vczWmQjfSONuk0ihOfaTChD7LwoPptTUu/Q6+
29
+ aT8zz7TH+sxxx+D09Q9+Ywf1RVnUe+i1PTo8kadnHIQdeBPudKrw5GWcIoLz1V16
30
+ A+j/SNEUqY1ui8TBX6axkd8vh6C1YcVAFCcdrxTMFTeB1irvL8jns6SulRHM08UC
31
+ 9NR+YthP16QuQzb5R2wIGX6WT11cnIpo0LzTnY/QSZGgwJkuLalBLzUssMiVnoSX
32
+ rVdmQBLrs38YcJouqHpf0wCGygf5430Bq4nCu0xUaH5umPcwJE9qaF1ZfgSCfZEz
33
+ Qe5fflfwGHtlmT6SWZoQD48xF/ILJ+cLTpF79ardGYnWT6N7VUAI5lKp7OjZYd+q
34
+ wwYlmlBW0Femxy5WoxzECQ==
36
35
  -----END CERTIFICATE-----
37
- date: 2020-03-04 00:00:00.000000000 Z
36
+ date: 2020-04-02 00:00:00.000000000 Z
38
37
  dependencies:
39
38
  - !ruby/object:Gem::Dependency
40
- name: optimist
39
+ name: defcli
41
40
  requirement: !ruby/object:Gem::Requirement
42
41
  requirements:
43
42
  - - "~>"
44
43
  - !ruby/object:Gem::Version
45
- version: '3.0'
44
+ version: '0.0'
46
45
  type: :runtime
47
46
  prerelease: false
48
47
  version_requirements: !ruby/object:Gem::Requirement
49
48
  requirements:
50
49
  - - "~>"
51
50
  - !ruby/object:Gem::Version
52
- version: '3.0'
53
- - !ruby/object:Gem::Dependency
54
- name: colored
55
- requirement: !ruby/object:Gem::Requirement
56
- requirements:
57
- - - "~>"
58
- - !ruby/object:Gem::Version
59
- version: '1.2'
60
- type: :runtime
61
- prerelease: false
62
- version_requirements: !ruby/object:Gem::Requirement
63
- requirements:
64
- - - "~>"
65
- - !ruby/object:Gem::Version
66
- version: '1.2'
51
+ version: '0.0'
67
52
  - !ruby/object:Gem::Dependency
68
53
  name: simplecov
69
54
  requirement: !ruby/object:Gem::Requirement
@@ -143,8 +128,6 @@ extra_rdoc_files: []
143
128
  files:
144
129
  - bin/ud
145
130
  - lib/ud.rb
146
- - lib/ud/formatting.rb
147
- - tests/formatting_tests.rb
148
131
  - tests/query_tests.rb
149
132
  - tests/tests.rb
150
133
  homepage: https://github.com/bfontaine/ud
@@ -173,4 +156,3 @@ summary: Urban Dictionary unofficial scrapper
173
156
  test_files:
174
157
  - tests/tests.rb
175
158
  - tests/query_tests.rb
176
- - tests/formatting_tests.rb
metadata.gz.sig CHANGED
Binary file
@@ -1,72 +0,0 @@
1
- # -*- coding: UTF-8 -*-
2
-
3
- module UD
4
- # Formatting tools for {UD.query}'s output
5
- module Formatting
6
- class << self
7
- # Fit a text in a given width (number of chars).
8
- # @param txt [String]
9
- # @param width [Integer] maximum width
10
- # @return [Array] list of lines of text
11
- def fit(txt, width = 79)
12
- return [] if width < 1
13
-
14
- # from https://stackoverflow.com/a/7567210/735926
15
- r = /(.{1,#{width}})(?:\s|$)/m
16
- txt.split("\n").map { |l| l.scan(r) }.flatten
17
- end
18
-
19
- # Add a tab at the beginning of a text. If it's a list, add a tab at
20
- # the beginning of each element.
21
- # @param txt [String] The text to tab, may be a string or a list of
22
- # strings
23
- # @param width [Integer] tab width
24
- # @return [String]
25
- def tab(txt, width = 4)
26
- return txt if width <= 0
27
-
28
- tab = " " * width
29
-
30
- return tab + txt if txt.is_a? String
31
-
32
- txt.map { |l| tab + l }
33
- end
34
-
35
- # Format results for text output (e.g. in the terminal)
36
- # @param results [Array<Hash>] this must be an array of results, as
37
- # returned by {UD.query}.
38
- # @param color [Boolean] colored output
39
- # @return [String]
40
- def text(results, color = true)
41
- require "colored" if color
42
-
43
- results.map do |r|
44
- word = r[:word]
45
- upvotes = r[:upvotes]
46
- downvotes = r[:downvotes]
47
-
48
- if color
49
- word = word.bold
50
- upvotes = upvotes.to_s.green
51
- downvotes = downvotes.to_s.red
52
- end
53
-
54
- votes = "#{upvotes}/#{downvotes}"
55
- definition = tab(fit(r[:definition], 75)).join "\n"
56
- example = tab(fit(r[:example], 75)).join "\n"
57
-
58
- <<-EOD
59
- * #{word} (#{votes}):
60
-
61
- #{definition}
62
-
63
- Example:
64
- #{example}
65
-
66
-
67
- EOD
68
- end.join "\n"
69
- end
70
- end
71
- end
72
- end
@@ -1,109 +0,0 @@
1
- #! /usr/bin/env ruby
2
- # -*- coding: UTF-8 -*-
3
-
4
- class FakeHTMLElement
5
-
6
- attr_reader :text
7
-
8
- def initialize(txt='')
9
- @text = txt
10
- end
11
- end
12
-
13
- class UD_Formatting_test < Test::Unit::TestCase
14
-
15
- # == UD#format_results == #
16
-
17
- def test_format_results_empty_list
18
- assert_equal('', UD.format_results([]))
19
- end
20
-
21
- def test_format_results_one_element_no_color
22
- res = {
23
- :word => 'XYZ',
24
- :upvotes => 42,
25
- :downvotes => 78,
26
- :definition => 'xyz',
27
- :example => 'zyx'
28
- }
29
-
30
- output = UD.format_results([res], false).strip
31
- expected = <<EOS
32
- * XYZ (42/78):
33
-
34
- xyz
35
-
36
- Example:
37
- zyx
38
- EOS
39
-
40
- assert_equal(expected.strip, output)
41
- end
42
-
43
- def test_format_results_one_element_color
44
- green = "\e[32m"
45
- bold = "\e[1m"
46
- red = "\e[31m"
47
- reset = "\e[0m"
48
- res = {
49
- :word => 'XYZ',
50
- :upvotes => 42,
51
- :downvotes => 78,
52
- :definition => 'xyz',
53
- :example => 'zyx'
54
- }
55
-
56
- output = UD.format_results([res], true).strip
57
- expected = <<EOS
58
- * #{bold}XYZ#{reset} (#{green}42#{reset}/#{red}78#{reset}):
59
-
60
- xyz
61
-
62
- Example:
63
- zyx
64
- EOS
65
-
66
- assert_equal(expected.strip, output)
67
- end
68
-
69
- # == UD::Formatting#fit == #
70
-
71
- def test_fit_0_width
72
- assert_equal([], UD::Formatting.fit('foo', 0))
73
- end
74
-
75
- def test_fit_negative_width
76
- assert_equal([], UD::Formatting.fit('foo', -1))
77
- end
78
-
79
- def test_fit_right_width
80
- assert_equal(['foo'], UD::Formatting.fit('foo', 3))
81
- end
82
-
83
- def test_fit_larger_width
84
- assert_equal(['foo'], UD::Formatting.fit('foo', 4))
85
- end
86
-
87
- def test_fit_smaller_width
88
- assert_equal(['a', 'b'], UD::Formatting.fit('a b', 2))
89
- end
90
-
91
- # == UD::Formatting#tab == #
92
-
93
- def test_tab_0_width
94
- assert_equal('foo', UD::Formatting.tab('foo', 0))
95
- end
96
-
97
- def test_tab_negative_width
98
- assert_equal('foo', UD::Formatting.tab('foo', -1))
99
- end
100
-
101
- def test_tab_2_width
102
- assert_equal(' foo', UD::Formatting.tab('foo', 2))
103
- end
104
-
105
- def test_tab_array
106
- assert_equal([' a', ' b'], UD::Formatting.tab(['a', 'b'], 1))
107
- end
108
-
109
- end