ud 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/ud +3 -0
- data/lib/ud/formatting.rb +22 -13
- data/lib/ud.rb +15 -15
- data/tests/formatting_tests.rb +26 -30
- data/tests/query_tests.rb +1 -1
- data/tests/tests.rb +5 -1
- metadata +39 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d511a1877540f177fd4d65e6f08f6bb292fbd6a
|
4
|
+
data.tar.gz: 31b686725109b06ea9ea5813dcf8a6380d6ecce4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 749cb4bf772d90c32e4a81783d557773fa993bce4208e14ad357da07e222501c068c3c307ab1344c941d6e4eba5aac14dabbd8e1a7cef5084fe140fd39067005
|
7
|
+
data.tar.gz: 9b4796d197fd96eb62299281851005de965cbd11c84b5c8d7d45fd8b29fbfa7fd2f7d3de880449cf263fbba5dbb55fbe3080c27780bb9b50c97dbe00028718da
|
data/bin/ud
CHANGED
data/lib/ud/formatting.rb
CHANGED
@@ -1,12 +1,15 @@
|
|
1
|
-
#! /usr/bin/env ruby
|
2
1
|
# -*- coding: UTF-8 -*-
|
3
2
|
|
4
3
|
module UD
|
4
|
+
|
5
|
+
# Formatting tools for {UD.query}'s output
|
5
6
|
module Formatting
|
6
7
|
class << self
|
7
8
|
|
8
|
-
# Fit a text in a given width (number of chars).
|
9
|
-
#
|
9
|
+
# Fit a text in a given width (number of chars).
|
10
|
+
# @param txt [String]
|
11
|
+
# @param width [Integer] maximum width
|
12
|
+
# @return [Array] list of lines of text
|
10
13
|
def fit(txt, width=79)
|
11
14
|
return [] if width < 1
|
12
15
|
|
@@ -17,8 +20,9 @@ module UD
|
|
17
20
|
|
18
21
|
# Add a tab at the beginning of a text. If it's a list, add a tab at
|
19
22
|
# the beginning of each element.
|
20
|
-
# [
|
21
|
-
# [
|
23
|
+
# @param txt [String] The text to tab, may be a string or a list of strings
|
24
|
+
# @param width [Integer] tab width
|
25
|
+
# @return [String]
|
22
26
|
def tab(txt, width=4)
|
23
27
|
width = 0 if width < 0
|
24
28
|
|
@@ -30,7 +34,10 @@ module UD
|
|
30
34
|
end
|
31
35
|
|
32
36
|
# Format results for text output (e.g. in the terminal)
|
33
|
-
# [
|
37
|
+
# @param results [Array<Hash>] this must be an array of results, as
|
38
|
+
# returned by {UD.query}.
|
39
|
+
# @param color [Boolean] colored output
|
40
|
+
# @return [String]
|
34
41
|
def text(results, color=true)
|
35
42
|
require 'colored' if color
|
36
43
|
|
@@ -50,14 +57,16 @@ module UD
|
|
50
57
|
definition = tab(fit(r[:definition], 75)).join("\n")
|
51
58
|
example = tab(fit(r[:example], 75)).join("\n")
|
52
59
|
|
53
|
-
|
60
|
+
<<-EOD
|
61
|
+
* #{word} (#{votes}):
|
62
|
+
|
63
|
+
#{definition}
|
64
|
+
|
65
|
+
Example:
|
66
|
+
#{example}
|
67
|
+
|
54
68
|
|
55
|
-
|
56
|
-
s << "\n"
|
57
|
-
s << definition
|
58
|
-
s << "\n\n Example:\n"
|
59
|
-
s << example
|
60
|
-
s << "\n\n"
|
69
|
+
EOD
|
61
70
|
|
62
71
|
end.join("\n")
|
63
72
|
end
|
data/lib/ud.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
#! /usr/bin/env ruby
|
2
1
|
# -*- coding: UTF-8 -*-
|
3
2
|
|
4
3
|
require 'uri'
|
@@ -12,30 +11,28 @@ require File.dirname(__FILE__) + '/ud/formatting'
|
|
12
11
|
module UD
|
13
12
|
class << self
|
14
13
|
|
14
|
+
# @return [String] the current gem's version
|
15
15
|
def version
|
16
|
-
'0.2.
|
16
|
+
'0.2.1'
|
17
17
|
end
|
18
18
|
|
19
19
|
# Get the search URL to query for a given term.
|
20
|
-
# [
|
20
|
+
# @param term [String] the term to search for. It must be a string, spaces
|
21
|
+
# are allowed.
|
22
|
+
# @return [String]
|
21
23
|
def search_url(term='')
|
22
24
|
param = URI.encode_www_form('term' => term)
|
23
25
|
"http://api.urbandictionary.com/v0/define?#{param}"
|
24
26
|
end
|
25
27
|
|
26
|
-
# Get the text of an element. This is an helper for internal usage.
|
27
|
-
def text(el)
|
28
|
-
el.text.strip.gsub(/\r/, "\n")
|
29
|
-
rescue
|
30
|
-
''
|
31
|
-
end
|
32
|
-
|
33
28
|
# Query the website and return a list of definitions for the provided term.
|
34
29
|
# This list may be empty if there's no result.
|
35
|
-
# [
|
36
|
-
# [
|
37
|
-
# maximum number of results to return,
|
38
|
-
# upvotes/downvotes ratio. Other
|
30
|
+
# @param term [String] the term to search for
|
31
|
+
# @param opts [Hash] options. This is used by the command-line tool.
|
32
|
+
# +:count+ is the maximum number of results to return,
|
33
|
+
# +:ratio+ is the minimum upvotes/downvotes ratio. Other
|
34
|
+
# options may be added in the future.
|
35
|
+
# @return [Array<Hash>]
|
39
36
|
def query(term, *opts)
|
40
37
|
|
41
38
|
opts = {:count => 1, :ratio => 0.0}.merge(opts[0] || {})
|
@@ -61,7 +58,10 @@ module UD
|
|
61
58
|
end
|
62
59
|
|
63
60
|
# Format results for output
|
64
|
-
# [
|
61
|
+
# @param results [Array] this must be an array of results, as returned by
|
62
|
+
# +UD.query+.
|
63
|
+
# @param color [Boolean] colored output
|
64
|
+
# @return [String]
|
65
65
|
def format_results(results, color=true)
|
66
66
|
UD::Formatting.text(results, color)
|
67
67
|
end
|
data/tests/formatting_tests.rb
CHANGED
@@ -12,42 +12,13 @@ end
|
|
12
12
|
|
13
13
|
class UD_Formatting_test < Test::Unit::TestCase
|
14
14
|
|
15
|
-
# == UD#text == #
|
16
|
-
|
17
|
-
def test_text_empty
|
18
|
-
el = FakeHTMLElement.new
|
19
|
-
assert_equal('', UD.text(el))
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_text_trailing_spaces
|
23
|
-
el = FakeHTMLElement.new('foo ')
|
24
|
-
assert_equal('foo', UD.text(el))
|
25
|
-
|
26
|
-
el = FakeHTMLElement.new(" bar \n")
|
27
|
-
assert_equal('bar', UD.text(el))
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_text_newline
|
31
|
-
el = FakeHTMLElement.new("a\rb")
|
32
|
-
assert_equal("a\nb", UD.text(el))
|
33
|
-
|
34
|
-
el = FakeHTMLElement.new("a\nb")
|
35
|
-
assert_equal("a\nb", UD.text(el))
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_text_invalid_element
|
39
|
-
assert_equal('', UD.text(nil))
|
40
|
-
assert_equal('', UD.text(true))
|
41
|
-
assert_equal('', UD.text('foo'))
|
42
|
-
end
|
43
|
-
|
44
15
|
# == UD#format_results == #
|
45
16
|
|
46
17
|
def test_format_results_empty_list
|
47
18
|
assert_equal('', UD.format_results([]))
|
48
19
|
end
|
49
20
|
|
50
|
-
def
|
21
|
+
def test_format_results_one_element_no_color
|
51
22
|
res = {
|
52
23
|
:word => 'XYZ',
|
53
24
|
:upvotes => 42,
|
@@ -67,7 +38,32 @@ class UD_Formatting_test < Test::Unit::TestCase
|
|
67
38
|
EOS
|
68
39
|
|
69
40
|
assert_equal(expected.strip, output)
|
41
|
+
end
|
70
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)
|
71
67
|
end
|
72
68
|
|
73
69
|
# == UD::Formatting#fit == #
|
data/tests/query_tests.rb
CHANGED
data/tests/tests.rb
CHANGED
@@ -1,12 +1,16 @@
|
|
1
1
|
#! /usr/bin/env ruby
|
2
2
|
# -*- coding: UTF-8 -*-
|
3
3
|
|
4
|
+
require 'coveralls'
|
5
|
+
Coveralls.wear!
|
6
|
+
|
4
7
|
require 'test/unit'
|
5
8
|
require 'simplecov'
|
6
9
|
|
7
10
|
test_dir = File.expand_path( File.dirname(__FILE__) )
|
8
11
|
|
9
|
-
SimpleCov.
|
12
|
+
SimpleCov.formatter = Coveralls::SimpleCov::Formatter
|
13
|
+
SimpleCov.start { add_filter '/tests/' }
|
10
14
|
|
11
15
|
require 'ud'
|
12
16
|
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ud
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Baptiste Fontaine
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-05-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: '2.8'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: '2.8'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: trollop
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -56,58 +56,72 @@ dependencies:
|
|
56
56
|
name: simplecov
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ~>
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
61
|
+
version: '0.7'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ~>
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
68
|
+
version: '0.7'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rake
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ~>
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
75
|
+
version: '10.1'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ~>
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
82
|
+
version: '10.1'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: test-unit
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- -
|
87
|
+
- - ~>
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
89
|
+
version: '2.5'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- -
|
94
|
+
- - ~>
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
96
|
+
version: '2.5'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: fakeweb
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
|
-
- -
|
101
|
+
- - ~>
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: '
|
103
|
+
version: '1.3'
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
|
-
- -
|
108
|
+
- - ~>
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version: '
|
110
|
+
version: '1.3'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: coveralls
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ~>
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0.7'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ~>
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0.7'
|
111
125
|
description: Get words' definitions from Urban Dictionary on the command-line.
|
112
126
|
email: batifon@yahoo.fr
|
113
127
|
executables:
|
@@ -115,12 +129,12 @@ executables:
|
|
115
129
|
extensions: []
|
116
130
|
extra_rdoc_files: []
|
117
131
|
files:
|
132
|
+
- bin/ud
|
118
133
|
- lib/ud.rb
|
119
134
|
- lib/ud/formatting.rb
|
120
135
|
- tests/formatting_tests.rb
|
121
136
|
- tests/query_tests.rb
|
122
137
|
- tests/tests.rb
|
123
|
-
- bin/ud
|
124
138
|
homepage: https://github.com/bfontaine/ud
|
125
139
|
licenses:
|
126
140
|
- MIT
|
@@ -141,7 +155,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
141
155
|
version: '0'
|
142
156
|
requirements: []
|
143
157
|
rubyforge_project:
|
144
|
-
rubygems_version: 2.
|
158
|
+
rubygems_version: 2.2.1
|
145
159
|
signing_key:
|
146
160
|
specification_version: 4
|
147
161
|
summary: Urban Dictionary unofficial scrapper
|
@@ -149,3 +163,4 @@ test_files:
|
|
149
163
|
- tests/formatting_tests.rb
|
150
164
|
- tests/query_tests.rb
|
151
165
|
- tests/tests.rb
|
166
|
+
has_rdoc:
|