joenoon-url_safe_base64 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.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Joe Noon
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,13 @@
1
+ UrlSafeBase64
2
+ =============
3
+
4
+ URL safe Base64
5
+
6
+
7
+ Example
8
+ =======
9
+
10
+ UrlSafeBase64
11
+
12
+
13
+ Copyright (c) 2008 Joe Noon, released under the MIT license
@@ -0,0 +1,53 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the url_safe_base64 plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the url_safe_base64 plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'UrlSafeBase64'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
23
+
24
+ ## Rake task to create/update a .manifest file in your project, as well as update *.gemspec
25
+ desc %{Update ".manifest" with the latest list of project filenames. Respect\
26
+ .gitignore by excluding everything that git ignores. Update `files` and\
27
+ `test_files` arrays in "*.gemspec" file if it's present.}
28
+ task :manifest do
29
+ list = Dir['**/*'].sort
30
+ spec_file = Dir['*.gemspec'].first
31
+ list -= [spec_file] if spec_file
32
+
33
+ if File.exist?('.gitignore')
34
+ File.read('.gitignore').each_line do |glob|
35
+ glob = glob.chomp.sub(/^\//, '')
36
+ list -= Dir[glob]
37
+ list -= Dir["#{glob}/**/*"] if File.directory?(glob) and !File.symlink?(glob)
38
+ puts "excluding #{glob}"
39
+ end
40
+ end
41
+
42
+ if spec_file
43
+ spec = File.read spec_file
44
+ spec.gsub! /^(\s* s.(test_)?files \s* = \s* )( \[ [^\]]* \] | %w\( [^)]* \) )/mx do
45
+ assignment = $1
46
+ bunch = $2 ? list.grep(/^test\//) : list
47
+ '%s%%w(%s)' % [assignment, bunch.join(' ')]
48
+ end
49
+
50
+ File.open(spec_file, 'w') {|f| f << spec }
51
+ end
52
+ File.open('.manifest', 'w') {|f| f << list.join("\n") }
53
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init"
@@ -0,0 +1,11 @@
1
+ module Statim
2
+ module UrlSafeBase64StringExtension
3
+ def url_safe_encode64
4
+ UrlSafeBase64.encode64(self)
5
+ end
6
+
7
+ def url_safe_decode64
8
+ UrlSafeBase64.decode64(self)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,18 @@
1
+ require 'base64'
2
+ module UrlSafeBase64
3
+
4
+ def self.encode64(str)
5
+ Base64.encode64(str).gsub(/[\s=]+/, "").gsub("+", "-").gsub("/", "_")
6
+ end
7
+
8
+ def self.decode64(str)
9
+ case str.length.modulo(4)
10
+ when 2
11
+ str += '=='
12
+ when 3
13
+ str += '='
14
+ end
15
+ Base64.decode64(str.gsub("-", "+").gsub("_", "/"))
16
+ end
17
+
18
+ end
@@ -0,0 +1,5 @@
1
+ require 'url_safe_base64'
2
+ require 'string_ext'
3
+ unless String.included_modules.include?(Statim::UrlSafeBase64StringExtension)
4
+ String.send :include, Statim::UrlSafeBase64StringExtension
5
+ end
Binary file
@@ -0,0 +1,49 @@
1
+ require 'test/unit'
2
+ require File.join(File.dirname(__FILE__),"..","lib","url_safe_base64.rb")
3
+
4
+ class UrlSafeBase64Test < Test::Unit::TestCase
5
+
6
+ TEST_FILE = File.read(File.join(File.dirname(__FILE__),"test_png.png"))
7
+ TEST_NORM_ENCODE64 = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAGXRFWHRTb2Z0\nd2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAjpJREFUeNpMUs9LG1EQnmw2\n260lalIqadIVqSHQNpKAGL2JBoTSBkIPuXnqyUvAP0JPgvVQ8JZa6NFADoGI\n6UHwaEBUSE2kJErp2nSbXek2+yN56+RtNnQYlvdmvm/fzDfjsiwL/rPf9Xq1\nWPxVreL58fR0eHk5NDs7zLrQLGp4ka6uCtlsrVRiMEHTGCUAzxKJd7u7wXh8\nwCEEg/D96OhzOq0rCgfAAjAOugdgYprjMnt7sUym/wISWpeXHxcWTEXhATSe\nV3l+VNfHOx0kdAEM6oTj3pdKzxcX+4RcKlUrFh8CNAIBEotNhsNis8mcnLwS\nRZO+oAN0AJ5Eo9lKhRXPz79RtOz1epeWVtfXPR4PdvVlZ+dnoRCUZbsZrE28\nuKgdHDD1w0OGFi36fIlkEtOGYUiS9GJ+vunzsU5LbuoIZuWbm8Gd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1gdX-fCczMTOIQnAR-eU3rWZZB1RwGkTkmCJGVFewE3m5vWxynU4SN0x30MILnN1tbbo7rE4S5uUwuh7PU6Lv2mDpOGRrlvN7cjKbTg9Ww969xfJxfW8M9dTvLZ4sxLghYwstUymXbkIBGej3c1nq53G40TMPwT01NJ5NYNzcygkh7ve8FGACA_yuDGxf1iwAAAABJRU5ErkJggg"
9
+
10
+ # this is a silly trial and error way to test it
11
+ MOD_GROUPS = {
12
+ "0" => [TEST_FILE+'AB', TEST_FILE+'ABCDE'],
13
+ "2" => [TEST_FILE, TEST_FILE+'ABC'],
14
+ "3" => [TEST_FILE+'A', TEST_FILE+'ABCD']
15
+ }
16
+
17
+ def test_the_test
18
+ result = Base64.encode64(TEST_FILE)
19
+ assert_equal TEST_NORM_ENCODE64, result
20
+ result = Base64.decode64(result)
21
+ assert_equal TEST_FILE, result
22
+ end
23
+
24
+ def test_url_safe_base64_on_png
25
+ result = UrlSafeBase64.encode64(TEST_FILE)
26
+ assert_equal TEST_SAFE_EXPECTED, result
27
+ result = UrlSafeBase64.decode64(result)
28
+ assert_equal TEST_FILE, result
29
+ end
30
+
31
+ def test_mod_groups
32
+ MOD_GROUPS.keys.each do |key|
33
+ MOD_GROUPS[key].each do |str|
34
+ result = UrlSafeBase64.encode64(str)
35
+ result = UrlSafeBase64.decode64(result)
36
+ assert_equal str, result
37
+ end
38
+ end
39
+ end
40
+
41
+ def test_for_correct_mod_values
42
+ MOD_GROUPS.keys.each do |key|
43
+ MOD_GROUPS[key].each do |str|
44
+ assert_equal key.to_i, UrlSafeBase64.encode64(str).length.modulo(4)
45
+ end
46
+ end
47
+ end
48
+
49
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: joenoon-url_safe_base64
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Joe Noon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-08-21 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: URL-safe base64 encoding/decoding
17
+ email: joe@stat.im
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - MIT-LICENSE
26
+ - README
27
+ - Rakefile
28
+ - init.rb
29
+ - lib
30
+ - lib/string_ext.rb
31
+ - lib/url_safe_base64.rb
32
+ - rails
33
+ - rails/init.rb
34
+ - test
35
+ - test/test_png.png
36
+ - test/url_safe_base64_test.rb
37
+ has_rdoc: false
38
+ homepage: http://github.com/joenoon/url_safe_base64
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.2.0
60
+ signing_key:
61
+ specification_version: 2
62
+ summary: URL-safe base64 encoding/decoding
63
+ test_files: []
64
+