activemodel-base64_validator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b7b40f967847819db430991f7d79079ae395f583
4
+ data.tar.gz: 8336d743b91775585d2ba0197eccb87e363d655a
5
+ SHA512:
6
+ metadata.gz: 6b78d9f162c19d49558c4f9b9485a8044714d079e64cfab3dcd9b78bcac2262dae6a3ebfde7b73e54477441d7bc5be8855e56d8f5ce312bc7373f0e2e3d152ef
7
+ data.tar.gz: 34fc57a20d4bf3d9eb07d9d65f4372321150bdce0304649ae2807650044afbc34b316d389f7458b8a61ba1619aa4a51b0332d02b6721132e25866289b567b627
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ Gemfile.lock
30
+ .ruby-version
31
+ .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in base64_encoded_validator.gemspec
4
+ gemspec
@@ -0,0 +1,36 @@
1
+ # activemodel-base64_validator
2
+
3
+ [![Build Status](https://travis-ci.org/increments/activemodel-base64_validator.svg?branch=master)](https://travis-ci.org/increments/activemodel-base64_validator)
4
+
5
+ ## Usage
6
+
7
+ Add to your Gemfile:
8
+
9
+ ```rb
10
+ gem 'activemodel-base64_validator'
11
+ ```
12
+
13
+ Run:
14
+
15
+ ```
16
+ bundle install
17
+ ```
18
+
19
+ Then add the following to your model:
20
+
21
+ ```rb
22
+ validates :my_base64_attribute, base64: true
23
+ ```
24
+
25
+ ## Validation outside a model
26
+
27
+ If you need to validate a base64 outside a model, you can get the regexp:
28
+
29
+ ```rb
30
+ Base64Validator::REGEXP
31
+ Base64Validator.valid?(string) # true or false
32
+ ```
33
+
34
+ ## Credit
35
+
36
+ Regular Expression based on http://stackoverflow.com/a/475217/1297336
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+ task default: :spec
@@ -0,0 +1,22 @@
1
+ file = File.open(File.expand_path('../lib/base64_validator/version.rb', __FILE__))
2
+ version = file.read.scan(/\d+\.\d+\.\d+/).first
3
+ file.close
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'activemodel-base64_validator'
7
+ spec.version = version
8
+ spec.authors = ['Yuku Takahashi']
9
+ spec.email = ['yuku@qiita.com']
10
+ spec.summary = 'A base64 validator for Rails 3 and 4.'
11
+ spec.homepage = 'https://github.com/increments/activemodel-base64_validator'
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.require_paths = ['lib']
16
+
17
+ spec.add_dependency 'activemodel'
18
+
19
+ spec.add_development_dependency 'bundler', '~> 1.7'
20
+ spec.add_development_dependency 'rake', '~> 10.0'
21
+ spec.add_development_dependency 'rspec'
22
+ end
@@ -0,0 +1,4 @@
1
+ en:
2
+ errors:
3
+ messages:
4
+ invalid_base64: is invalid Base64 encoding.
@@ -0,0 +1,4 @@
1
+ ja:
2
+ errors:
3
+ messages:
4
+ invalid_base64: は有効なBase64形式ではありません。
@@ -0,0 +1,3 @@
1
+ require 'base64_validator'
2
+ require 'base64_validator/version'
3
+ require 'base64_validator/engine'
@@ -0,0 +1,13 @@
1
+ class Base64Validator < ActiveModel::EachValidator
2
+ REGEXP = %r<\A(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\z>
3
+
4
+ def self.valid?(value)
5
+ !!(value =~ REGEXP)
6
+ end
7
+
8
+ def validate_each(record, attribute, value)
9
+ unless self.class.valid?(value)
10
+ record.errors.add(attribute, options[:message] || :invalid_base64)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ require 'rails'
2
+
3
+ class Base64Validator
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ class Base64Validator
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,101 @@
1
+ require 'spec_helper'
2
+
3
+ shared_context 'valid base64 string is given', given: :valid do
4
+ let(:value) do
5
+ Base64.strict_encode64("Now is the time for all good coders\nto learn Ruby")
6
+ end
7
+ end
8
+
9
+ shared_context 'invalid base64 string is given', given: :invalid do
10
+ let(:value) do
11
+ "Now is the time for all good coders\nto learn Ruby"
12
+ end
13
+ end
14
+
15
+ shared_context 'nil is given', given: nil do
16
+ let(:value) do
17
+ nil
18
+ end
19
+ end
20
+
21
+ describe Base64Validator do
22
+ describe '.valid?' do
23
+ subject do
24
+ described_class.valid?(value)
25
+ end
26
+
27
+ context 'when a valid base64 string is given', given: :valid do
28
+ it { should be_truthy }
29
+ end
30
+
31
+ context 'when a invalid base64 string is given', given: :invalid do
32
+ it { should be_falsey }
33
+ end
34
+ end
35
+
36
+ describe 'validation' do
37
+ subject do
38
+ model_class.new(attr: value)
39
+ end
40
+
41
+ context 'when option is true' do
42
+ let(:model_class) do
43
+ Class.new(TestModel) do
44
+ validates :attr, base64: true
45
+ end
46
+ end
47
+
48
+ context 'and a valid base64 string is given', given: :valid do
49
+ it { should be_valid }
50
+ end
51
+
52
+ context 'and a invalid base64 string is given', given: :invalid do
53
+ it { should be_invalid }
54
+ end
55
+
56
+ context 'and nil is given', given: nil do
57
+ it { should be_invalid }
58
+ end
59
+ end
60
+
61
+ context 'when option is { allow_nil: true }' do
62
+ let(:model_class) do
63
+ Class.new(TestModel) do
64
+ validates :attr, base64: { allow_nil: true }
65
+ end
66
+ end
67
+
68
+ context 'and a valid base64 string is given', given: :valid do
69
+ it { should be_valid }
70
+ end
71
+
72
+ context 'and a invalid base64 string is given', given: :invalid do
73
+ it { should be_invalid }
74
+ end
75
+
76
+ context 'and nil is given', given: nil do
77
+ it { should be_valid }
78
+ end
79
+ end
80
+
81
+ context 'when option is { allow_nil: false }' do
82
+ let(:model_class) do
83
+ Class.new(TestModel) do
84
+ validates :attr, base64: { allow_nil: false }
85
+ end
86
+ end
87
+
88
+ context 'and a valid base64 string is given', given: :valid do
89
+ it { should be_valid }
90
+ end
91
+
92
+ context 'and a invalid base64 string is given', given: :invalid do
93
+ it { should be_invalid }
94
+ end
95
+
96
+ context 'and nil is given', given: nil do
97
+ it { should be_invalid }
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,24 @@
1
+ require 'base64'
2
+ require 'active_model'
3
+ require 'base64_validator'
4
+
5
+ RSpec.configure do |config|
6
+ config.color = true
7
+ config.run_all_when_everything_filtered = true
8
+ end
9
+
10
+ class TestModel
11
+ include ActiveModel::Validations
12
+
13
+ def self.name
14
+ 'TestModel'
15
+ end
16
+
17
+ def initialize(attributes = {})
18
+ @attributes = attributes
19
+ end
20
+
21
+ def read_attribute_for_validation(key)
22
+ @attributes[key]
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activemodel-base64_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yuku Takahashi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '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.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: 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:
70
+ email:
71
+ - yuku@qiita.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - README.md
80
+ - Rakefile
81
+ - activemodel-base64_validator.gemspec
82
+ - config/locales/en.yml
83
+ - config/locales/ja.yml
84
+ - lib/activemodel-base64_validator.rb
85
+ - lib/base64_validator.rb
86
+ - lib/base64_validator/engine.rb
87
+ - lib/base64_validator/version.rb
88
+ - spec/base64_validator_spec.rb
89
+ - spec/spec_helper.rb
90
+ homepage: https://github.com/increments/activemodel-base64_validator
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.2.2
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: A base64 validator for Rails 3 and 4.
114
+ test_files: []