uri_encoding 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +25 -3
- data/lib/uri_encoding/decoder.rb +6 -6
- data/lib/uri_encoding/version.rb +1 -1
- data/spec/support/mappings.rb +19 -0
- data/spec/uri_encoding/decoder_spec.rb +9 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 864c1bfccc6eaa73163f8a0b9647b90588dd7712
|
4
|
+
data.tar.gz: 5b3d86dcaa63bf612b9897a3de75ef5af5291b6a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 990a727a76729c3657a62b4f99b9b12e9007e0ec7dd9426f9f22232c530742d60ee717367f5318cfe56f4bda72ded98eceeda83f2c4e36c20eb988df21200b03
|
7
|
+
data.tar.gz: d85897b11abf01cd6ab16b4ac6116d3e20f721b918de6ef56577c9ef73d31c69b6245479471b97dbe76aeb93e2ab4b0b4310eabced788290de72ed0afb7044f6
|
data/README.md
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
# UriEncoding
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/uri_encoding.svg)](http://badge.fury.io/rb/uri_encoding)
|
3
4
|
[![Build Status](https://travis-ci.org/s-osa/uri_encoding.svg?branch=master)](https://travis-ci.org/s-osa/uri_encoding)
|
4
5
|
[![Coverage Status](https://coveralls.io/repos/s-osa/uri_encoding/badge.png?branch=master)](https://coveralls.io/r/s-osa/uri_encoding?branch=master)
|
5
6
|
|
6
|
-
|
7
|
+
Simple utility for URI encoding(URL encoding, percent encoding).
|
7
8
|
|
8
9
|
## Installation
|
9
10
|
|
@@ -18,10 +19,31 @@ And then execute:
|
|
18
19
|
Or install it yourself as:
|
19
20
|
|
20
21
|
$ gem install uri_encoding
|
21
|
-
|
22
|
+
|
22
23
|
## Usage
|
23
24
|
|
24
|
-
|
25
|
+
### Basic
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
UriEncoding.encode("Aa1!?_%") #=> "Aa1!?_%25"
|
29
|
+
UriEncoding.decode("Aa1!?_%25") #=> "Aa1!?_%"
|
30
|
+
```
|
31
|
+
|
32
|
+
### Optional
|
33
|
+
|
34
|
+
`UriEncoding.encode` receives exceptions for encoding.
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
UriEncoding.encode("Aa1!?_%", []) #=> "%41%61%31%21%3F%5F%25"
|
38
|
+
UriEncoding.encode("Aa1!?_%", UriEncoding::UnreservedCharacters) #=> "Aa1%21%3F_%25"
|
39
|
+
```
|
40
|
+
|
41
|
+
`UriEncoding.decode` receives decoded string encoding.
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
string = UriEncoding.decode("%82%A0%82%A2%82%A4", Encoding::Windows_31J) #=> "あいう"
|
45
|
+
string.encoding #=> #<Encoding:Windows-31J>
|
46
|
+
```
|
25
47
|
|
26
48
|
## Contributing
|
27
49
|
|
data/lib/uri_encoding/decoder.rb
CHANGED
@@ -2,10 +2,10 @@ module UriEncoding
|
|
2
2
|
class Decoder
|
3
3
|
EncodedCharRegexp = /^%([0-9A-Fa-f]{2})$/
|
4
4
|
|
5
|
-
def decode(str)
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
def decode(str, encoding=nil)
|
6
|
+
single_byte_expressions = split_encoded_string(str)
|
7
|
+
bytes_string = single_byte_expressions.map{|e| convert_to_byte(e) }.join
|
8
|
+
bytes_string.force_encoding(encoding || str.encoding)
|
9
9
|
end
|
10
10
|
|
11
11
|
private
|
@@ -20,8 +20,8 @@ module UriEncoding
|
|
20
20
|
arr
|
21
21
|
end
|
22
22
|
|
23
|
-
def
|
24
|
-
|
23
|
+
def convert_to_byte(exp)
|
24
|
+
exp =~ EncodedCharRegexp ? Integer("0x#{$1}").chr : exp
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
data/lib/uri_encoding/version.rb
CHANGED
data/spec/support/mappings.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
1
3
|
def default_mappings
|
2
4
|
{
|
3
5
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" => "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
@@ -42,6 +44,9 @@ def default_mappings
|
|
42
44
|
"{" => "%7B",
|
43
45
|
"|" => "%7C",
|
44
46
|
"}" => "%7D",
|
47
|
+
|
48
|
+
# multibyte character
|
49
|
+
"あいう" => "%E3%81%82%E3%81%84%E3%81%86",
|
45
50
|
}
|
46
51
|
end
|
47
52
|
|
@@ -89,5 +94,19 @@ def everything_escaped_mappings
|
|
89
94
|
"{" => "%7B",
|
90
95
|
"|" => "%7C",
|
91
96
|
"}" => "%7D",
|
97
|
+
|
98
|
+
# multibyte character
|
99
|
+
"あいう" => "%E3%81%82%E3%81%84%E3%81%86",
|
100
|
+
"あいう".encode(Encoding::Windows_31J) => "%82%A0%82%A2%82%A4".encode(Encoding::Windows_31J),
|
92
101
|
}
|
93
102
|
end
|
103
|
+
|
104
|
+
def multibyte_mappings
|
105
|
+
[
|
106
|
+
{
|
107
|
+
source: "あいう".encode(Encoding::Windows_31J),
|
108
|
+
encoded: "%82%A0%82%A2%82%A4",
|
109
|
+
encoding: Encoding::Windows_31J
|
110
|
+
},
|
111
|
+
]
|
112
|
+
end
|
@@ -28,5 +28,14 @@ describe UriEncoding::Decoder do
|
|
28
28
|
end
|
29
29
|
end
|
30
30
|
end
|
31
|
+
|
32
|
+
context "with encoding argument" do
|
33
|
+
multibyte_mappings.each do |mapping|
|
34
|
+
describe "#{mapping[:encoded]} as #{mapping[:encoding]}" do
|
35
|
+
subject{ decoder.decode(mapping[:encoded], mapping[:encoding]) }
|
36
|
+
it{ is_expected.to eq(mapping[:source]) }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
31
40
|
end
|
32
41
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uri_encoding
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- OSA Shunsuke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|