Ruby: How to Escape Url with Square Brackets [ and ]

Ruby: How to escape url with square brackets [ and ]?

encode doesn't escape brackets because they aren't special -- they have no special meaning in the path part of a URI, so they don't actually need escaping.

If you want to escape chars other than just the "unsafe" ones, pass a second arg to the encode method. That arg should be a regex matching, or a string containing, every char you want encoded (including chars the function would otherwise already match!).

How to escape square brackets in the content of an HTML using slim syntax?

The brackets are being parsed as containing the attributes for the element. You could add a “dummy” set of attribute wrapping brackets:

td () [Send] Value

You could also nest the content on the next line, using the pipe character to signify it’s plain text:

td
| [Send] Value

Both of these produce <td>[Send] Value</td> as output.

Escape the square brackets and quotation marks or fetch this array without the quotes and brackets

You can use the join method on your array :

["William", "Alexis", "John", "Richard"].join(", ")

So here you can just do :

self.students.collect {|x| x.name}.join(", ")

but your method will then return a string and not an array.

How to extract content within square brackets in ruby

Almost.

a = "This is such a great day [cool awesome]"
a[/\[(.*?)\]/, 1]
# => "cool awesome"
a[/(?<=\[).*?(?=\])/]
# => "cool awesome"

The first one relies on extracting a group instead of a full match; the second one leverages lookahead and lookbehind to avoid the delimiters in the final match.

Escaping just the querystring part of a url in Rails

This is fixed in rails 3.2. Under the hood it's the method Hash#to_query that does the job.

3.0

{ :x => { :y => '[]' } }.to_query
=> "x[y]=%5B%5D"

3.2

{ :x => { :y => '[]' } }.to_query
=> "x%5By%5D=%5B%5D"

Perhaps dig into the source to see what the changes are and backport them into your app

URL encoding issues with curly braces

The GitHub Archive example for retrieving a range of files is:

wget http://data.githubarchive.org/2015-01-01-{0..23}.json.gz

The {0..23} part is being interpreted by wget itself as a range of 0 .. 23. You can test this by executing that command with the -v flag which returns:

wget -v http://data.githubarchive.org/2015-01-01-{0..1}.json.gz
--2015-06-11 13:31:07-- http://data.githubarchive.org/2015-01-01-0.json.gz
Resolving data.githubarchive.org... 74.125.25.128, 2607:f8b0:400e:c03::80
Connecting to data.githubarchive.org|74.125.25.128|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2615399 (2.5M) [application/x-gzip]
Saving to: '2015-01-01-0.json.gz'

2015-01-01-0.json.gz 100%[===========================================================================================================================================>] 2.49M 3.03MB/s in 0.8s

2015-06-11 13:31:09 (3.03 MB/s) - '2015-01-01-0.json.gz' saved [2615399/2615399]

--2015-06-11 13:31:09-- http://data.githubarchive.org/2015-01-01-1.json.gz
Reusing existing connection to data.githubarchive.org:80.
HTTP request sent, awaiting response... 200 OK
Length: 2535599 (2.4M) [application/x-gzip]
Saving to: '2015-01-01-1.json.gz'

2015-01-01-1.json.gz 100%[===========================================================================================================================================>] 2.42M 867KB/s in 2.9s

2015-06-11 13:31:11 (867 KB/s) - '2015-01-01-1.json.gz' saved [2535599/2535599]

FINISHED --2015-06-11 13:31:11--
Total wall clock time: 4.3s
Downloaded: 2 files, 4.9M in 3.7s (1.33 MB/s)

In other words, wget is substituting values into the URL and then getting that new URL. This isn't obvious behavior, nor is it well documented, but you can find mention of it "out there". For instance in "All the Wget Commands You Should Know":

7. Download a list of sequentially numbered files from a server
wget http://example.com/images/{1..20}.jpg

To do what you want, you need to iterate over the range in Ruby using something like this untested code:

0.upto(23) do |i|
response = conn.get("/2015-01-01-#{ i }.json.gz")
p response.body
end


Related Topics



Leave a reply



Submit