ud 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 11f8394cb33159ae220771206f5c66611840532a
4
+ data.tar.gz: ed5b562d420f7a2e071a9da934c0b8528978674f
5
+ SHA512:
6
+ metadata.gz: 6d3b91414b1ee2f7ba0543ffc684892bc0d064f2e762623a22ab85a792a20883b5b565684ad801c6ddf3f2bf49106e043530dbc9ae63618620962b98dc3091a0
7
+ data.tar.gz: 8cfe02567efc781a86af680b6d2a11bbeb4e6f81440ea543f7aa1523500381d2aad36adb5287c115faa8a07bd108877da4bf79aca5263719f5c4190e98336b7f
data/bin/ud CHANGED
@@ -15,7 +15,7 @@ where [options] are:
15
15
  EOS
16
16
 
17
17
  opt :ratio, 'Filter by upvotes/downvotes ratio', :type => :float, :default => 0.0
18
- opt :count, 'Limit the number of definitions', :type => :int, :default => 10, :short => '-n'
18
+ opt :count, 'Limit the number of definitions', :type => :int, :default => 1, :short => '-n'
19
19
  opt :color, 'Use colorized output', :default => true
20
20
  opt :up, 'Shortcut for \'-r 2\''
21
21
 
data/lib/ud.rb CHANGED
@@ -14,7 +14,7 @@ module UD
14
14
 
15
15
  # The current version of the module
16
16
  def UD.version
17
- '0.1.1'
17
+ '0.1.2'
18
18
  end
19
19
 
20
20
  # Get the search URL to query for a given term.
@@ -62,7 +62,7 @@ module UD
62
62
  # upvotes/downvotes ratio. Other options may be added in the future.
63
63
  def UD.query(term, *opts)
64
64
 
65
- opts = {:count => 10, :ratio => 0.0}.merge(opts[0] || {})
65
+ opts = {:count => 1, :ratio => 0.0}.merge(opts[0] || {})
66
66
 
67
67
  return [] if opts[:count] <= 0
68
68
 
@@ -106,37 +106,7 @@ module UD
106
106
  # Format results for output
107
107
  # [results] this must be an array of results, as returned by +UD.query+.
108
108
  def UD.format_results(results, color=true)
109
- require 'colored' if color
110
-
111
- t = ' ' # 4-spaces tab
112
-
113
- results.map do |r|
114
-
115
- word = r[:word]
116
- upvotes = r[:upvotes]
117
- downvotes = r[:downvotes]
118
-
119
- if (color)
120
- word = word.bold
121
- upvotes = upvotes.to_s.green
122
- downvotes = downvotes.to_s.red
123
- end
124
-
125
- votes = "#{upvotes}/#{downvotes}"
126
- definition = UD::Formatting.fit(r[:definition], 75).map {|l| t+l}.join("\n")
127
- example = UD::Formatting.fit(r[:example], 75).map {|l| t+l}.join("\n")
128
-
129
- s = ''
130
-
131
- s << "* #{word} (#{votes}):\n"
132
- s << "\n"
133
- s << definition
134
- s << "\n\n Example:\n"
135
- s << example
136
- s << "\n\n"
137
-
138
- end.join("\n")
139
-
109
+ UD::Formatting.text(results, color)
140
110
  end
141
111
 
142
112
  end
@@ -7,8 +7,58 @@ module UD
7
7
  # Fit a text in a given width (number of chars). It returns
8
8
  # a list of lines of text.
9
9
  def self.fit(txt, width=79)
10
+ return [] if width < 1
11
+
10
12
  # from http://stackoverflow.com/a/7567210/735926
11
- txt.split("\n").map{|l| l.scan(/(.{1,#{width}})(?:\s|$)/m) }.flatten
13
+ r = /(.{1,#{width}})(?:\s|$)/m
14
+ txt.split("\n").map { |l| l.scan(r) }.flatten
15
+ end
16
+
17
+ # Add a tab at the beginning of a text. If it's a list, add a tab at
18
+ # the beginning of each element.
19
+ # [txt] The text to tab, may be a string or a list of strings
20
+ # [width] The width (number of spaces) of a tab
21
+ def self.tab(txt, width=4)
22
+ width = 0 if width < 0
23
+
24
+ tab = ' ' * width
25
+
26
+ return tab + txt if txt.is_a?(String)
27
+
28
+ txt.map { |l| tab + l }
29
+ end
30
+
31
+ # Format results for text output (e.g. in the terminal)
32
+ # [results] this must be an array of results, as returned by +UD.query+.
33
+ def self.text(results, color=true)
34
+ require 'colored' if color
35
+
36
+ results.map do |r|
37
+
38
+ word = r[:word]
39
+ upvotes = r[:upvotes]
40
+ downvotes = r[:downvotes]
41
+
42
+ if (color)
43
+ word = word.bold
44
+ upvotes = upvotes.to_s.green
45
+ downvotes = downvotes.to_s.red
46
+ end
47
+
48
+ votes = "#{upvotes}/#{downvotes}"
49
+ definition = tab(fit(r[:definition], 75)).join("\n")
50
+ example = tab(fit(r[:example], 75)).join("\n")
51
+
52
+ s = ''
53
+
54
+ s << "* #{word} (#{votes}):\n"
55
+ s << "\n"
56
+ s << definition
57
+ s << "\n\n Example:\n"
58
+ s << example
59
+ s << "\n\n"
60
+
61
+ end.join("\n")
12
62
  end
13
63
 
14
64
  end
@@ -60,14 +60,54 @@ class UD_Formatting_test < Test::Unit::TestCase
60
60
  expected = <<EOS
61
61
  * XYZ (42/78):
62
62
 
63
- xyz
63
+ xyz
64
64
 
65
65
  Example:
66
- zyx
66
+ zyx
67
67
  EOS
68
68
 
69
69
  assert_equal(expected.strip, output)
70
70
 
71
71
  end
72
72
 
73
+ # == UD::Formatting#fit == #
74
+
75
+ def test_fit_0_width
76
+ assert_equal([], UD::Formatting.fit('foo', 0))
77
+ end
78
+
79
+ def test_fit_negative_width
80
+ assert_equal([], UD::Formatting.fit('foo', -1))
81
+ end
82
+
83
+ def test_fit_right_width
84
+ assert_equal(['foo'], UD::Formatting.fit('foo', 3))
85
+ end
86
+
87
+ def test_fit_larger_width
88
+ assert_equal(['foo'], UD::Formatting.fit('foo', 4))
89
+ end
90
+
91
+ def test_fit_smaller_width
92
+ assert_equal(['a', 'b'], UD::Formatting.fit('a b', 2))
93
+ end
94
+
95
+ # == UD::Formatting#tab == #
96
+
97
+ def test_tab_0_width
98
+ assert_equal('foo', UD::Formatting.tab('foo', 0))
99
+ end
100
+
101
+ def test_tab_negative_width
102
+ assert_equal('foo', UD::Formatting.tab('foo', -1))
103
+ end
104
+
105
+ def test_tab_2_width
106
+ assert_equal(' foo', UD::Formatting.tab('foo', 2))
107
+ end
108
+
109
+ def test_tab_array
110
+ assert_equal([' a', ' b'], UD::Formatting.tab(['a', 'b'], 1))
111
+ end
112
+
73
113
  end
@@ -74,7 +74,7 @@ class UD_Formatting_test < Test::Unit::TestCase
74
74
  }
75
75
  ]
76
76
 
77
- assert_equal(expected, UD.query('two'))
77
+ assert_equal(expected, UD.query('two', :count => 2))
78
78
  end
79
79
 
80
80
  def test_query_count
@@ -104,7 +104,7 @@ class UD_Formatting_test < Test::Unit::TestCase
104
104
  }
105
105
  ]
106
106
 
107
- assert_equal(expected, UD.query('two', :ratio => 1.5))
107
+ assert_equal(expected, UD.query('two', :count => 10, :ratio => 1.5))
108
108
  end
109
109
 
110
110
  end
metadata CHANGED
@@ -1,142 +1,125 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ud
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
5
- prerelease:
4
+ version: 0.1.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Baptiste Fontaine
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-06-04 00:00:00.000000000 Z
11
+ date: 2013-10-11 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: nokogiri
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
19
  version: 1.5.9
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: 1.5.9
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: json
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: 1.8.0
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: 1.8.0
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: trollop
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ~>
45
+ - - "~>"
52
46
  - !ruby/object:Gem::Version
53
47
  version: '2.0'
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ~>
52
+ - - "~>"
60
53
  - !ruby/object:Gem::Version
61
54
  version: '2.0'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: colored
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ~>
59
+ - - "~>"
68
60
  - !ruby/object:Gem::Version
69
61
  version: '1.2'
70
62
  type: :runtime
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ~>
66
+ - - "~>"
76
67
  - !ruby/object:Gem::Version
77
68
  version: '1.2'
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: simplecov
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
- - - ! '>='
73
+ - - ">="
84
74
  - !ruby/object:Gem::Version
85
75
  version: '0'
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
- - - ! '>='
80
+ - - ">="
92
81
  - !ruby/object:Gem::Version
93
82
  version: '0'
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: rake
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
- - - ! '>='
87
+ - - ">="
100
88
  - !ruby/object:Gem::Version
101
89
  version: '0'
102
90
  type: :development
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
- - - ! '>='
94
+ - - ">="
108
95
  - !ruby/object:Gem::Version
109
96
  version: '0'
110
97
  - !ruby/object:Gem::Dependency
111
98
  name: test-unit
112
99
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
100
  requirements:
115
- - - ! '>='
101
+ - - ">="
116
102
  - !ruby/object:Gem::Version
117
103
  version: '0'
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
107
  requirements:
123
- - - ! '>='
108
+ - - ">="
124
109
  - !ruby/object:Gem::Version
125
110
  version: '0'
126
111
  - !ruby/object:Gem::Dependency
127
112
  name: fakeweb
128
113
  requirement: !ruby/object:Gem::Requirement
129
- none: false
130
114
  requirements:
131
- - - ! '>='
115
+ - - ">="
132
116
  - !ruby/object:Gem::Version
133
117
  version: '0'
134
118
  type: :development
135
119
  prerelease: false
136
120
  version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
121
  requirements:
139
- - - ! '>='
122
+ - - ">="
140
123
  - !ruby/object:Gem::Version
141
124
  version: '0'
142
125
  description: Get words' definitions from Urban Dictionary on the command-line.
@@ -148,36 +131,35 @@ extra_rdoc_files: []
148
131
  files:
149
132
  - lib/ud.rb
150
133
  - lib/ud/formatting.rb
151
- - tests/tests.rb
152
- - tests/query_tests.rb
153
134
  - tests/formatting_tests.rb
135
+ - tests/query_tests.rb
136
+ - tests/tests.rb
154
137
  - bin/ud
155
138
  homepage: https://github.com/bfontaine/ud
156
139
  licenses:
157
140
  - MIT
141
+ metadata: {}
158
142
  post_install_message:
159
143
  rdoc_options: []
160
144
  require_paths:
161
145
  - lib
162
146
  required_ruby_version: !ruby/object:Gem::Requirement
163
- none: false
164
147
  requirements:
165
- - - ! '>='
148
+ - - ">="
166
149
  - !ruby/object:Gem::Version
167
150
  version: '0'
168
151
  required_rubygems_version: !ruby/object:Gem::Requirement
169
- none: false
170
152
  requirements:
171
- - - ! '>='
153
+ - - ">="
172
154
  - !ruby/object:Gem::Version
173
155
  version: '0'
174
156
  requirements: []
175
157
  rubyforge_project:
176
- rubygems_version: 1.8.25
158
+ rubygems_version: 2.1.6
177
159
  signing_key:
178
- specification_version: 3
160
+ specification_version: 4
179
161
  summary: Urban Dictionary unofficial scrapper
180
162
  test_files:
181
- - tests/tests.rb
182
- - tests/query_tests.rb
183
163
  - tests/formatting_tests.rb
164
+ - tests/query_tests.rb
165
+ - tests/tests.rb