uri_encoding 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c5f8aae210a6dd4c612344e0df46f05def7de411
4
+ data.tar.gz: 83c0d50dc3d711b5a422f8b676f919e70f2709ba
5
+ SHA512:
6
+ metadata.gz: d6cdbc41a611966f2f9d56fb3713bea5422ac87fc2955c8ca945c2fd4953dbc20a45fcb91f935bf0c565c44be40b933106b8267d1e0004a68f5ac82d42884e11
7
+ data.tar.gz: f17f755131eba4bf92dbe60cfe31d004cb2296600923752b90c9c522366e7ba42d94d3f4ebee663447d10d67b1cb6be03137c029d43bf9d52a0589c577c0a8b8
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format doc
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
4
+ - 2.0.0
5
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dech.gemspec
4
+ gemspec
5
+
6
+ gem "coveralls", require: false
data/Guardfile ADDED
@@ -0,0 +1,7 @@
1
+ guard :rspec, cmd: "bundle exec rspec" do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ watch(%r{^spec/support/.+}) { "spec" }
6
+ end
7
+
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 OSA Shunsuke
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # UriEncoding
2
+
3
+ [![Build Status](https://travis-ci.org/s-osa/uri_encoding.svg?branch=master)](https://travis-ci.org/s-osa/uri_encoding)
4
+ [![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
+ TODO: Write a gem description
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'uri_encoding'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install uri_encoding
21
+
22
+ ## Usage
23
+
24
+ TODO: Write usage instructions here
25
+
26
+ ## Contributing
27
+
28
+ 1. Fork it ( https://github.com/[my-github-username]/uri_encoding/fork )
29
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
30
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
31
+ 4. Push to the branch (`git push origin my-new-feature`)
32
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ RSpec::Core::RakeTask.new
4
+
5
+ task default: :spec
6
+
@@ -0,0 +1,27 @@
1
+ module UriEncoding
2
+ class Decoder
3
+ EncodedCharRegexp = /^%([0-9A-Fa-f]{2})$/
4
+
5
+ def decode(str)
6
+ encoded_chars = split_encoded_string(str)
7
+ decoded_chars = encoded_chars.map{|c| decode_char(c) }
8
+ decoded_chars.join
9
+ end
10
+
11
+ private
12
+
13
+ def split_encoded_string(str)
14
+ arr, pos = [], 0
15
+ while pos < str.size
16
+ step = str[pos] == "%" ? 3 : 1
17
+ arr << str[pos...(pos + step)]
18
+ pos += step
19
+ end
20
+ arr
21
+ end
22
+
23
+ def decode_char(char)
24
+ char =~ EncodedCharRegexp ? Integer("0x#{$1}").chr : char
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,17 @@
1
+ module UriEncoding
2
+ class Encoder
3
+ def initialize(excepted_chars=nil)
4
+ @excepted_chars = excepted_chars || DefaultExceptions
5
+ end
6
+
7
+ def encode(str)
8
+ str.to_s.each_char.map{|c| @excepted_chars.include?(c) ? c : escape_char(c) }.join("")
9
+ end
10
+
11
+ private
12
+
13
+ def escape_char(char)
14
+ char.each_byte.map{|byte| "%#{byte.to_s(16).upcase}" }.join("")
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module UriEncoding
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,28 @@
1
+ require "uri_encoding/decoder"
2
+ require "uri_encoding/encoder"
3
+ require "uri_encoding/version"
4
+
5
+ module UriEncoding
6
+ GenDelims = %w(: / ? # [ ] @)
7
+ SubDelims = %w(! $ & ' ( ) * + , ; =)
8
+ Alphabets = ("A".."Z").to_a + ("a".."z").to_a
9
+ Digits = (0..9).to_a.map(&:to_s)
10
+ UnreservedSigns = %w(- . _ ~)
11
+
12
+ ReservedCharacters = GenDelims + SubDelims
13
+ UnreservedCharacters = Alphabets + Digits + UnreservedSigns
14
+
15
+ DefaultExceptions = ReservedCharacters + UnreservedCharacters
16
+
17
+ module_function
18
+
19
+ def encode(str, exceptions=DefaultExceptions)
20
+ encoder = UriEncoding::Encoder.new(exceptions)
21
+ encoder.encode(str)
22
+ end
23
+
24
+ def decode(str)
25
+ decoder = UriEncoding::Decoder.new
26
+ decoder.decode(str)
27
+ end
28
+ end
@@ -0,0 +1,11 @@
1
+ # coding: utf-8
2
+
3
+ project_root = File.join(File.dirname(__FILE__), '..')
4
+ $: << project_root
5
+
6
+ Dir[File.join(File.dirname(__FILE__), "support", "*")].each {|f| require f }
7
+
8
+ require 'lib/uri_encoding'
9
+
10
+ require 'coveralls'
11
+ Coveralls.wear!
@@ -0,0 +1,27 @@
1
+ def gen_delims
2
+ %w(: / ? # [ ] @)
3
+ end
4
+
5
+ def sub_delims
6
+ %w(! $ & ' ( ) * + , ; =)
7
+ end
8
+
9
+ def reserved_characters
10
+ gen_delims + sub_delims
11
+ end
12
+
13
+ def unreserved_characters
14
+ alphabets + digits + unreserved_signs
15
+ end
16
+
17
+ def alphabets
18
+ ("A".."Z").to_a + ("a".."z").to_a
19
+ end
20
+
21
+ def digits
22
+ (0..9).to_a.map(&:to_s)
23
+ end
24
+
25
+ def unreserved_signs
26
+ %w(- . _ ~)
27
+ end
@@ -0,0 +1,93 @@
1
+ def default_mappings
2
+ {
3
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ" => "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
4
+ "abcdefghijklmnopqrstuvwxyz" => "abcdefghijklmnopqrstuvwxyz",
5
+ "0123456789" => "0123456789",
6
+
7
+ # gen-delims
8
+ ":" => ":",
9
+ "/" => "/",
10
+ "?" => "?",
11
+ "#" => "#",
12
+ "[" => "[",
13
+ "]" => "]",
14
+ "@" => "@",
15
+
16
+ # sub-delims
17
+ "!" => "!",
18
+ "$" => "$",
19
+ "&" => "&",
20
+ "'" => "'",
21
+ "(" => "(",
22
+ ")" => ")",
23
+ "*" => "*",
24
+ "+" => "+",
25
+ "," => ",",
26
+ ";" => ";",
27
+ "=" => "=",
28
+
29
+ # unreserved
30
+ "-" => "-",
31
+ "." => ".",
32
+ "_" => "_",
33
+ "~" => "~",
34
+
35
+ # others
36
+ " " => "%20",
37
+ "%" => "%25",
38
+ "<" => "%3C",
39
+ ">" => "%3E",
40
+ "\\" => "%5C",
41
+ "`" => "%60",
42
+ "{" => "%7B",
43
+ "|" => "%7C",
44
+ "}" => "%7D",
45
+ }
46
+ end
47
+
48
+ def everything_escaped_mappings
49
+ {
50
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ" => "%41%42%43%44%45%46%47%48%49%4A%4B%4C%4D%4E%4F%50%51%52%53%54%55%56%57%58%59%5A",
51
+ "abcdefghijklmnopqrstuvwxyz" => "%61%62%63%64%65%66%67%68%69%6A%6B%6C%6D%6E%6F%70%71%72%73%74%75%76%77%78%79%7A",
52
+ "0123456789" => "%30%31%32%33%34%35%36%37%38%39",
53
+
54
+ # gen-delims
55
+ ":" => "%3A",
56
+ "/" => "%2F",
57
+ "?" => "%3F",
58
+ "#" => "%23",
59
+ "[" => "%5B",
60
+ "]" => "%5D",
61
+ "@" => "%40",
62
+
63
+ # sub-delims
64
+ "!" => "%21",
65
+ "$" => "%24",
66
+ "&" => "%26",
67
+ "'" => "%27",
68
+ "(" => "%28",
69
+ ")" => "%29",
70
+ "*" => "%2A",
71
+ "+" => "%2B",
72
+ "," => "%2C",
73
+ ";" => "%3B",
74
+ "=" => "%3D",
75
+
76
+ # unreserved
77
+ "-" => "%2D",
78
+ "." => "%2E",
79
+ "_" => "%5F",
80
+ "~" => "%7E",
81
+
82
+ # others
83
+ " " => "%20",
84
+ "%" => "%25",
85
+ "<" => "%3C",
86
+ ">" => "%3E",
87
+ "\\" => "%5C",
88
+ "`" => "%60",
89
+ "{" => "%7B",
90
+ "|" => "%7C",
91
+ "}" => "%7D",
92
+ }
93
+ end
@@ -0,0 +1,32 @@
1
+ require "spec_helper"
2
+
3
+ describe UriEncoding::Decoder do
4
+ let(:decoder){ described_class.new }
5
+
6
+ describe "#initialize" do
7
+ context "without excepted characters" do
8
+ subject{ described_class.new }
9
+ it{ is_expected.to be_an_instance_of(described_class) }
10
+ end
11
+ end
12
+
13
+ describe "#decode" do
14
+ context "default exceptions" do
15
+ default_mappings.each do |source, encoded|
16
+ describe "#{encoded}" do
17
+ subject{ decoder.decode(encoded) }
18
+ it{ is_expected.to eq(source) }
19
+ end
20
+ end
21
+ end
22
+
23
+ context "no exceptions" do
24
+ everything_escaped_mappings.each do |source, encoded|
25
+ describe "#{encoded}" do
26
+ subject{ decoder.decode(encoded) }
27
+ it{ is_expected.to eq(source) }
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,38 @@
1
+ require "spec_helper"
2
+
3
+ describe UriEncoding::Encoder do
4
+ describe "#initialize" do
5
+ context "without excepted characters" do
6
+ subject{ described_class.new }
7
+ it{ is_expected.to be_an_instance_of(described_class) }
8
+ end
9
+
10
+ context "with excepted characters" do
11
+ let(:excepted_chars){ %w(a b c) }
12
+ subject{ described_class.new(excepted_chars) }
13
+ it{ is_expected.to be_an_instance_of(described_class) }
14
+ end
15
+ end
16
+
17
+ describe "#encode" do
18
+ context "default exceptions" do
19
+ let(:encoder){ described_class.new }
20
+ default_mappings.each do |source, result|
21
+ describe "#{source}" do
22
+ subject{ encoder.encode(source) }
23
+ it{ is_expected.to eq(result) }
24
+ end
25
+ end
26
+ end
27
+
28
+ context "no exceptions" do
29
+ let(:encoder){ described_class.new([]) }
30
+ everything_escaped_mappings.each do |source, result|
31
+ describe "#{source}" do
32
+ subject{ encoder.encode(source) }
33
+ it{ is_expected.to eq(result) }
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,78 @@
1
+ require "spec_helper"
2
+
3
+ describe UriEncoding do
4
+ describe "::GenDelims" do
5
+ subject{ described_class::GenDelims }
6
+ it{ is_expected.to eq(gen_delims)}
7
+ end
8
+
9
+ describe "::SubDelims" do
10
+ subject{ described_class::SubDelims }
11
+ it{ is_expected.to eq(sub_delims)}
12
+ end
13
+
14
+ describe "::Reserved characters" do
15
+ subject{ described_class::ReservedCharacters }
16
+ it{ is_expected.to eq(reserved_characters)}
17
+ end
18
+
19
+ describe "::Alphabets" do
20
+ subject{ described_class::Alphabets }
21
+ it{ is_expected.to eq(alphabets)}
22
+ end
23
+
24
+ describe "::Digits" do
25
+ subject{ described_class::Digits }
26
+ it{ is_expected.to eq(digits)}
27
+ end
28
+
29
+ describe "::UnreservedSigns" do
30
+ subject{ described_class::UnreservedSigns }
31
+ it{ is_expected.to eq(unreserved_signs)}
32
+ end
33
+
34
+ describe "::Unreserved characters" do
35
+ subject{ described_class::UnreservedCharacters }
36
+ it{ is_expected.to eq(unreserved_characters)}
37
+ end
38
+
39
+ describe ".encode" do
40
+ context "default exceptions" do
41
+ default_mappings.each do |source, result|
42
+ describe "#{source}" do
43
+ subject{ described_class.encode(source) }
44
+ it{ is_expected.to eq(result) }
45
+ end
46
+ end
47
+ end
48
+
49
+ context "no exceptions" do
50
+ everything_escaped_mappings.each do |source, result|
51
+ describe "#{source}" do
52
+ subject{ described_class.encode(source, []) }
53
+ it{ is_expected.to eq(result) }
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ describe ".decode" do
60
+ context "default exceptions" do
61
+ default_mappings.each do |source, encoded|
62
+ describe "#{encoded}" do
63
+ subject{ described_class.decode(encoded) }
64
+ it{ is_expected.to eq(source) }
65
+ end
66
+ end
67
+ end
68
+
69
+ context "no exceptions" do
70
+ everything_escaped_mappings.each do |source, encoded|
71
+ describe "#{encoded}" do
72
+ subject{ described_class.decode(encoded) }
73
+ it{ is_expected.to eq(source) }
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'uri_encoding/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "uri_encoding"
8
+ spec.version = UriEncoding::VERSION
9
+ spec.authors = ["OSA Shunsuke"]
10
+ spec.email = ["hhelibebcnofnenamg@gmail.com"]
11
+ spec.summary = %q{Simple utility for URI encoding.}
12
+ spec.description = %q{Simple utility for URI encoding.}
13
+ spec.homepage = "https://github.com/s-osa/uri_encoding"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "guard-rspec"
25
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uri_encoding
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - OSA Shunsuke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Simple utility for URI encoding.
70
+ email:
71
+ - hhelibebcnofnenamg@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - Guardfile
81
+ - LICENSE
82
+ - README.md
83
+ - Rakefile
84
+ - lib/uri_encoding.rb
85
+ - lib/uri_encoding/decoder.rb
86
+ - lib/uri_encoding/encoder.rb
87
+ - lib/uri_encoding/version.rb
88
+ - spec/spec_helper.rb
89
+ - spec/support/characters.rb
90
+ - spec/support/mappings.rb
91
+ - spec/uri_encoding/decoder_spec.rb
92
+ - spec/uri_encoding/encoder_spec.rb
93
+ - spec/uri_encoding_spec.rb
94
+ - uri_encoding.gemspec
95
+ homepage: https://github.com/s-osa/uri_encoding
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.2.2
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Simple utility for URI encoding.
119
+ test_files:
120
+ - spec/spec_helper.rb
121
+ - spec/support/characters.rb
122
+ - spec/support/mappings.rb
123
+ - spec/uri_encoding/decoder_spec.rb
124
+ - spec/uri_encoding/encoder_spec.rb
125
+ - spec/uri_encoding_spec.rb