web-utils 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +0 -1
- data/README.md +47 -31
- data/lib/web_utils.rb +12 -2
- data/test/test_web_utils.rb +37 -3
- data/web-utils.gemspec +12 -2
- metadata +31 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe05079e7ef8fd8cb746476a6c91cfa6ca4a76db
|
4
|
+
data.tar.gz: 4adc890b7732c60962b739ad908eef2c8b3ce4ff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ada4be5ff78d2747e405a7f71a1649d3bae9708a3c84b1d0cbd16b8e28ceda8e77b46fda60d929b3df9cb900b083ff4d69206a9eee5046f3dfaf48242ca9b553
|
7
|
+
data.tar.gz: 8a14f5837d0298616675d3504fec6ada2993a8264da9167e419b5e206ace34c48b1c3065d4031e67eadecd875b60db80e89f33d8c2d17fe1475935de14016866
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -6,8 +6,8 @@ methods that are quite common to have in a web project.
|
|
6
6
|
It is organized like `Rack::Utils` and actually extends it.
|
7
7
|
|
8
8
|
Some of the methods are similar to methods you would have
|
9
|
-
in `Active::Support` but without monkey patching.
|
10
|
-
is only a coincidence. The purpose is not to
|
9
|
+
in `Active::Support` but without monkey patching. Nevertheless it
|
10
|
+
is only a coincidence. The purpose is not to create an alternative.
|
11
11
|
|
12
12
|
Here is how you would use it with `Sinatra`:
|
13
13
|
|
@@ -27,7 +27,7 @@ class Main < Sinatra::Base
|
|
27
27
|
end
|
28
28
|
```
|
29
29
|
|
30
|
-
Some methods are also useful on the model side.
|
30
|
+
Some methods are also useful on the model side of your app.
|
31
31
|
|
32
32
|
Here is a list of the available methods and what they do:
|
33
33
|
|
@@ -39,31 +39,34 @@ Just tells you if the string is blank or not.
|
|
39
39
|
`pluralize(string)`
|
40
40
|
-------------------
|
41
41
|
|
42
|
-
|
42
|
+
Find a plural version for words. It is meant to deal with most common cases
|
43
|
+
and therefore is not exhaustive.
|
43
44
|
|
44
45
|
`singularize(string)`
|
45
46
|
-------------------
|
46
47
|
|
47
|
-
|
48
|
+
Basically the opposite of `pluralize`.
|
48
49
|
|
49
50
|
`dasherize_class_name(string)`
|
50
51
|
------------------------------
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
|
53
|
+
Turns class names into a dasherized version (e.g. `"web-utils"`).
|
54
|
+
Module separator is a double dash.
|
55
|
+
So `"BlogArticle::Comment"` becomes `"blog-article--comment"`.
|
56
|
+
Also for simplicity, it does not try to be clever with accronyms.
|
57
|
+
So `"Net::FTP"` returns `"net--f-t-p"`.
|
55
58
|
This is useful for urls or creating CSS class names or IDs.
|
56
59
|
|
57
60
|
`undasherize_class_name(string)`
|
58
61
|
--------------------------------
|
59
62
|
|
60
|
-
Basically the opposite
|
63
|
+
Basically the opposite of `dasherize_class_name`.
|
61
64
|
|
62
65
|
`resolve_class_name(string, context=Kernel)`
|
63
66
|
--------------------------------------------
|
64
67
|
|
65
68
|
It takes the class name as a string and returns the class.
|
66
|
-
You can pass a class name with modules as well (e.g. "Net::FTP").
|
69
|
+
You can pass a class name with modules as well (e.g. `"Net::FTP"`).
|
67
70
|
This is actually the main reason why there is a `context`
|
68
71
|
argument, because it uses recursion to do this.
|
69
72
|
But `context` is still useful otherwise.
|
@@ -81,7 +84,7 @@ Useful for resolving a class from a URL param.
|
|
81
84
|
It is mainly used for guessing the class name of a
|
82
85
|
children class with a plural name.
|
83
86
|
So `guess_related_class_name(BlogArticle, :comments)`
|
84
|
-
will return `
|
87
|
+
will return `"BlogArticle::Comment"`.
|
85
88
|
|
86
89
|
`get_value(value, target=Kernel)`
|
87
90
|
----------------------------------
|
@@ -120,7 +123,7 @@ default value. And this value is also returned by the method.
|
|
120
123
|
--------------------------------------
|
121
124
|
|
122
125
|
Same as `ensure_key!` except that it does not change the original
|
123
|
-
hash. It returns a new one.
|
126
|
+
hash. It returns a new one (following the bang convention).
|
124
127
|
|
125
128
|
`slugify(string, force_lowercase=true)`
|
126
129
|
-----------------
|
@@ -128,14 +131,15 @@ hash. It returns a new one.
|
|
128
131
|
This makes the strings ready to be used as a slug in a URL.
|
129
132
|
It removes the accents, replaces a lot of separators with
|
130
133
|
dashes and escapes it. By default it forces the output to
|
131
|
-
be lowercase, but if you pass `false` as
|
134
|
+
be lowercase, but if you pass `false` as the second argument,
|
132
135
|
it will not change the case of letters.
|
133
136
|
|
134
137
|
`label_for_field(string_or_symbol)`
|
135
138
|
-----------------------------------
|
136
139
|
|
137
140
|
Returns a human readable version of a field name.
|
138
|
-
|
141
|
+
So `:label_for_field` returns `"Label for field"`.
|
142
|
+
It says `field`, but it could be used with any kind of symbol.
|
139
143
|
|
140
144
|
`each_stub(nested_object) {|object,key_or_index,value| ... }`
|
141
145
|
-------------------------------------------------------------
|
@@ -154,7 +158,7 @@ simple things like `true`, `false`, integers and floats.
|
|
154
158
|
And an empty string is always `nil`.
|
155
159
|
|
156
160
|
The second argument is the list of things you want to typecast.
|
157
|
-
By default there is everything, but you only want to typecast
|
161
|
+
By default there is everything, but if you only want to typecast
|
158
162
|
integers and floats, you can pass `[:int, :float]`.
|
159
163
|
|
160
164
|
`generate_random_id(size)`
|
@@ -176,17 +180,18 @@ Just in case you want self-closing tags.
|
|
176
180
|
-----------------------
|
177
181
|
|
178
182
|
This just makes sure that a link is complete. Very often
|
179
|
-
people tend to enter a URL like `www.google.com` which is a
|
180
|
-
controversial `href` for some browsers
|
181
|
-
|
183
|
+
people tend to enter a URL like `"www.google.com"` which is a
|
184
|
+
controversial `href` for some browsers. This method would change it for
|
185
|
+
`"//www.google.com"`. Already seemingly complete links are not
|
182
186
|
affected by the method.
|
183
187
|
|
184
188
|
`external_link?(string)`
|
185
189
|
------------------------
|
186
190
|
|
187
191
|
This tells you if a link is pointing to the current site or
|
188
|
-
an external one
|
189
|
-
|
192
|
+
an external one (based on the presence of a domain name or not).
|
193
|
+
This is useful when you want to create a link
|
194
|
+
tag and want to decide if target is `"_blank"` or `"_self"`.
|
190
195
|
|
191
196
|
`automatic_html(string, br="<br>")`
|
192
197
|
-----------------------------------
|
@@ -199,16 +204,27 @@ is quite useful, should it be only for turning an email into a link.
|
|
199
204
|
--------------------------------------------
|
200
205
|
|
201
206
|
It truncates a string like what you have in blog summaries.
|
202
|
-
It automatically removes tags and line breaks. The
|
207
|
+
It automatically removes tags and line breaks. The size is
|
203
208
|
320 by default. When the original string was longer, it puts
|
204
209
|
an ellipsis at the end which can be replaced by whatever you put
|
205
|
-
as a 3rd argument. e.g. `
|
210
|
+
as a 3rd argument. e.g. `"...and more"`.
|
211
|
+
|
212
|
+
`regex_for_query(query, exhaustive=true)`
|
213
|
+
-----------------------------------------
|
214
|
+
|
215
|
+
It takes a query string (potentially from a search field) and
|
216
|
+
builds a regex for matching a string which contains all the words
|
217
|
+
of the query, whatever order they appear in.
|
218
|
+
|
219
|
+
If you pass `false` as the second argument, the regex will match
|
220
|
+
if the text contains at least one word of the query instead of all.
|
221
|
+
|
206
222
|
|
207
223
|
`display_price(int)`
|
208
224
|
--------------------
|
209
225
|
|
210
226
|
It changes a price in cents/pence into a formated string
|
211
|
-
like `49,425.40` when you pass `4942540`.
|
227
|
+
like `"49,425.40"` when you pass `4942540`.
|
212
228
|
|
213
229
|
`parse_price(string)`
|
214
230
|
---------------------
|
@@ -221,29 +237,29 @@ order to return a price in cents/pence.
|
|
221
237
|
|
222
238
|
It takes the path to a file and add the brand/prefix and a dash
|
223
239
|
before the file name (really the file name, not the path).
|
224
|
-
By default, the brand/prefix is `WebUtils`.
|
240
|
+
By default, the brand/prefix is `"WebUtils"`.
|
225
241
|
|
226
242
|
`filename_variation(path, variation, ext)`
|
227
243
|
------------------------------------------
|
228
244
|
|
229
|
-
For example you have a file
|
230
|
-
to create its `thumbnail` in `png`, you can create the
|
245
|
+
For example you have a file `"/path/to/image.jpg"` and you want
|
246
|
+
to create its `thumbnail` in `png`, you can create the thumbnail
|
231
247
|
path with `filename_variation(path, :thumbnail, :png)` and it
|
232
|
-
will return
|
248
|
+
will return `"/path/to/image.thumbnail.png"`.
|
233
249
|
|
234
250
|
`initial_request?(request)`
|
235
251
|
---------------------------
|
236
252
|
|
237
253
|
You basically pass the `Request` object to the method and it
|
238
254
|
looks at the referrer and returns true if it was not on the same
|
239
|
-
domain. Essentially tells you if the visitor just arrived.
|
255
|
+
domain. Essentially tells you if the visitor just arrived on your website.
|
240
256
|
|
241
|
-
`
|
257
|
+
`being_crawled?(request)`
|
242
258
|
--------------------------
|
243
259
|
|
244
|
-
While this method is useful
|
260
|
+
While this method is useful, it only checks for the presence of
|
245
261
|
these words `/bot|crawl|slurp|spider/i` to determine if the user
|
246
|
-
agent is a crawler or not. So it is pretty
|
262
|
+
agent is a crawler or not. So it is pretty basic. If you have a
|
247
263
|
better way, please make a pull request.
|
248
264
|
|
249
265
|
`h(text)`
|
data/lib/web_utils.rb
CHANGED
@@ -5,6 +5,8 @@ require 'uri'
|
|
5
5
|
|
6
6
|
module WebUtils
|
7
7
|
|
8
|
+
VERSION = '0.0.4'
|
9
|
+
|
8
10
|
# Most methods are supposed to be as simple as possible
|
9
11
|
# and just cover most cases.
|
10
12
|
# I would rather override specific cases rather than making
|
@@ -221,6 +223,14 @@ module WebUtils
|
|
221
223
|
end
|
222
224
|
module_function :truncate
|
223
225
|
|
226
|
+
def regex_for_query query, exhaustive=true
|
227
|
+
atoms = query.split(/[^a-zA-Z0-9\&]+/)
|
228
|
+
atom_patterns = atoms.map{|a| "(?=.*\\b#{a})" }
|
229
|
+
sep = exhaustive ? '' : '|'
|
230
|
+
regex = /#{atom_patterns.join(sep)}/i
|
231
|
+
end
|
232
|
+
module_function :regex_for_query
|
233
|
+
|
224
234
|
def display_price int
|
225
235
|
unless int.is_a?(Integer)
|
226
236
|
raise(TypeError, 'The price needs to be the price in cents/pence as an integer')
|
@@ -260,10 +270,10 @@ module WebUtils
|
|
260
270
|
module_function :initial_request?
|
261
271
|
|
262
272
|
BOT_REGEX = /bot|crawl|slurp|spider/i
|
263
|
-
def
|
273
|
+
def being_crawled? request
|
264
274
|
request.user_agent =~ BOT_REGEX
|
265
275
|
end
|
266
|
-
module_function :
|
276
|
+
module_function :being_crawled?
|
267
277
|
|
268
278
|
def h text
|
269
279
|
escape_html text
|
data/test/test_web_utils.rb
CHANGED
@@ -434,6 +434,40 @@ describe WebUtils do
|
|
434
434
|
end
|
435
435
|
end
|
436
436
|
|
437
|
+
describe '#regex_for_query' do
|
438
|
+
it 'Builds a Regex for matching all words in any order' do
|
439
|
+
[
|
440
|
+
['hello', 'hello'],
|
441
|
+
['hello', 'say hello to me'],
|
442
|
+
['hello world', 'hello world'],
|
443
|
+
['hello world', 'the world says hello'],
|
444
|
+
['hello/world', 'the world says hello'],
|
445
|
+
].each do |i|
|
446
|
+
assert_match utils.regex_for_query(i[0]), i[1]
|
447
|
+
end
|
448
|
+
[
|
449
|
+
['hello', 'say aloha to me'],
|
450
|
+
['hello world', 'say hello to me'],
|
451
|
+
['hello', ''],
|
452
|
+
].each do |i|
|
453
|
+
refute_match utils.regex_for_query(i[0]), i[1]
|
454
|
+
end
|
455
|
+
end
|
456
|
+
it 'Builds a Regex for matching at least one word if exhautive is false' do
|
457
|
+
[
|
458
|
+
['hello world', 'say hello to me'],
|
459
|
+
['hello aloha say', 'say hello to me'],
|
460
|
+
].each do |i|
|
461
|
+
assert_match utils.regex_for_query(i[0], false), i[1]
|
462
|
+
end
|
463
|
+
[
|
464
|
+
['hello world', 'say aloha to me'],
|
465
|
+
].each do |i|
|
466
|
+
refute_match utils.regex_for_query(i[0], false), i[1]
|
467
|
+
end
|
468
|
+
end
|
469
|
+
end
|
470
|
+
|
437
471
|
describe '#display_price' do
|
438
472
|
it 'Turns a price number in cents/pence into a displayable one' do
|
439
473
|
assert_equal '45.95', utils.display_price(4595)
|
@@ -522,7 +556,7 @@ describe WebUtils do
|
|
522
556
|
end
|
523
557
|
end
|
524
558
|
|
525
|
-
describe '#
|
559
|
+
describe '#being_crawled?' do
|
526
560
|
let(:req) {
|
527
561
|
Rack::Request.new(
|
528
562
|
Rack::MockRequest.env_for(
|
@@ -534,13 +568,13 @@ describe WebUtils do
|
|
534
568
|
describe 'When user agent matches' do
|
535
569
|
let(:user_agent) {'Mega Bot from hell Version 6.6.6'}
|
536
570
|
it 'Returns true' do
|
537
|
-
assert utils.
|
571
|
+
assert utils.being_crawled?(req)
|
538
572
|
end
|
539
573
|
end
|
540
574
|
describe 'When user does not match' do
|
541
575
|
let(:user_agent) {'Firefox'}
|
542
576
|
it 'Returns false' do
|
543
|
-
refute utils.
|
577
|
+
refute utils.being_crawled?(req)
|
544
578
|
end
|
545
579
|
end
|
546
580
|
end
|
data/web-utils.gemspec
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$:.unshift lib
|
5
|
+
require 'web_utils'
|
6
|
+
|
1
7
|
Gem::Specification.new do |s|
|
2
8
|
|
3
9
|
s.authors = ['Mickael Riga']
|
@@ -6,7 +12,7 @@ Gem::Specification.new do |s|
|
|
6
12
|
s.licenses = ['MIT']
|
7
13
|
|
8
14
|
s.name = 'web-utils'
|
9
|
-
s.version =
|
15
|
+
s.version = WebUtils::VERSION
|
10
16
|
s.summary = 'Web Utils'
|
11
17
|
s.description = 'Useful web-related helper methods for models, views or controllers.'
|
12
18
|
|
@@ -14,7 +20,11 @@ Gem::Specification.new do |s|
|
|
14
20
|
s.files = `git ls-files`.split("\n").sort
|
15
21
|
s.test_files = s.files.grep(/^test\//)
|
16
22
|
s.require_paths = ['lib']
|
17
|
-
|
23
|
+
|
24
|
+
s.add_dependency 'rack', '>= 1.0'
|
25
|
+
|
26
|
+
s.add_development_dependency 'bundler', '~> 1.13'
|
27
|
+
s.add_development_dependency 'minitest', '~> 5.8'
|
18
28
|
|
19
29
|
end
|
20
30
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: web-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mickael Riga
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-07-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -24,6 +24,34 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.13'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.13'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.8'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.8'
|
27
55
|
description: Useful web-related helper methods for models, views or controllers.
|
28
56
|
email:
|
29
57
|
- mig@mypeplum.com
|
@@ -58,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
86
|
version: '0'
|
59
87
|
requirements: []
|
60
88
|
rubyforge_project:
|
61
|
-
rubygems_version: 2.
|
89
|
+
rubygems_version: 2.6.11
|
62
90
|
signing_key:
|
63
91
|
specification_version: 4
|
64
92
|
summary: Web Utils
|