-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
96 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bundler/gem_tasks" | ||
require 'bundler/gem_tasks' | ||
|
||
require "rspec/core/rake_task" | ||
require 'rspec/core/rake_task' | ||
RSpec::Core::RakeTask.new(:spec) | ||
|
||
require "rubocop/rake_task" | ||
require 'rubocop/rake_task' | ||
RuboCop::RakeTask.new(:rubocop) | ||
|
||
task default: %i[rubocop spec] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
module OStruct | ||
module Sanitizer | ||
VERSION = "0.7.0" | ||
VERSION = '0.7.0' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,32 @@ | ||
# coding: utf-8 | ||
lib = File.expand_path('../lib', __FILE__) | ||
# frozen_string_literal: true | ||
|
||
lib = File.expand_path('lib', __dir__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
require 'ostruct/sanitizer/version' | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "ostruct-sanitizer" | ||
spec.name = 'ostruct-sanitizer' | ||
spec.version = OStruct::Sanitizer::VERSION | ||
spec.authors = ["Diego Borges"] | ||
spec.email = ["[email protected]"] | ||
spec.authors = ['Diego Borges'] | ||
spec.email = ['[email protected]'] | ||
|
||
spec.summary = %q{Provides Rails-like sanitization rules for OpenStruct fields.} | ||
spec.description = %q{Provides Rails-like sanitization rules for OpenStruct fields.} | ||
spec.homepage = "https://github.com/powerhome/ostruct-sanitizer" | ||
spec.license = "MIT" | ||
spec.summary = 'Provides Rails-like sanitization rules for OpenStruct fields.' | ||
spec.description = 'Provides Rails-like sanitization rules for OpenStruct fields.' | ||
spec.homepage = 'https://github.com/powerhome/ostruct-sanitizer' | ||
spec.license = 'MIT' | ||
|
||
spec.files = `git ls-files -z`.split("\x0").reject do |f| | ||
f.match(%r{^(test|spec|features)/}) | ||
end | ||
spec.bindir = "exe" | ||
spec.bindir = 'exe' | ||
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } | ||
spec.require_paths = ["lib"] | ||
spec.require_paths = ['lib'] | ||
|
||
spec.required_ruby_version = '>= 2.6' | ||
|
||
spec.add_development_dependency "bundler", "~> 2.4.7" | ||
spec.add_development_dependency "rake", "~> 10.0" | ||
spec.add_development_dependency "rspec", "~> 3.0" | ||
spec.add_development_dependency "rubocop-powerhome", "0.5.0" | ||
spec.add_development_dependency "byebug" | ||
spec.add_development_dependency 'bundler', '~> 2.4.7' | ||
spec.add_development_dependency 'byebug' | ||
spec.add_development_dependency 'rake', '~> 10.0' | ||
spec.add_development_dependency 'rspec', '~> 3.0' | ||
spec.add_development_dependency 'rubocop-powerhome', '0.5.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
require "spec_helper" | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe OStruct::Sanitizer do | ||
it "has a version number" do | ||
it 'has a version number' do | ||
expect(OStruct::Sanitizer::VERSION).not_to be nil | ||
end | ||
|
||
describe "invalid usage" do | ||
it "fails including OStruct::Sanitizer within a non OpenStruct class" do | ||
define_invalid_usage = -> { | ||
describe 'invalid usage' do | ||
it 'fails including OStruct::Sanitizer within a non OpenStruct class' do | ||
define_invalid_usage = lambda { | ||
class InvalidUsage | ||
include OStruct::Sanitizer | ||
end | ||
|
@@ -17,7 +19,7 @@ class InvalidUsage | |
end | ||
end | ||
|
||
describe "#truncate" do | ||
describe '#truncate' do | ||
class User < OpenStruct | ||
include OStruct::Sanitizer | ||
truncate :first_name, :last_name, length: 10 | ||
|
@@ -26,139 +28,139 @@ class User < OpenStruct | |
|
||
let(:user) do | ||
User.new( | ||
first_name: " first name longer than 10 characters", | ||
last_name: " last name longer than 10 characters", | ||
middle_name: " Rose ", | ||
first_name: ' first name longer than 10 characters', | ||
last_name: ' last name longer than 10 characters', | ||
middle_name: ' Rose ' | ||
) | ||
end | ||
|
||
it "truncates user's first name to 10 characters" do | ||
expect(user.first_name).to eq "first name" | ||
expect(user.first_name).to eq 'first name' | ||
end | ||
|
||
it "truncates user's last name to 10 characters" do | ||
expect(user.last_name).to eq "last name" | ||
expect(user.last_name).to eq 'last name' | ||
end | ||
|
||
it "truncates user's middle name without stipping whitespaces" do | ||
expect(user.middle_name).to eq " Ro" | ||
expect(user.middle_name).to eq ' Ro' | ||
end | ||
|
||
it "does not sanitize if value is nil" do | ||
it 'does not sanitize if value is nil' do | ||
user = User.new first_name: nil, last_name: nil | ||
expect(user.first_name).to be nil | ||
expect(user.last_name).to be nil | ||
end | ||
end | ||
|
||
describe "#alphanumeric" do | ||
describe '#alphanumeric' do | ||
class User < OpenStruct | ||
include OStruct::Sanitizer | ||
alphanumeric :city, :country | ||
end | ||
|
||
let(:user) do | ||
User.new( | ||
city: "Porto, Alegre!", | ||
country: "B.r!_a,z#i%l^", | ||
city: 'Porto, Alegre!', | ||
country: 'B.r!_a,z#i%l^' | ||
) | ||
end | ||
|
||
it "drops punctuation from user's city name" do | ||
expect(user.city).to eq "Porto Alegre" | ||
expect(user.city).to eq 'Porto Alegre' | ||
end | ||
|
||
it "drops punctuation from user's country name" do | ||
expect(user.country).to eq "Brazil" | ||
expect(user.country).to eq 'Brazil' | ||
end | ||
|
||
it "does not sanitize if value is nil" do | ||
it 'does not sanitize if value is nil' do | ||
user = User.new city: nil, country: nil | ||
expect(user.city).to be nil | ||
expect(user.country).to be nil | ||
end | ||
end | ||
|
||
describe "#strip" do | ||
describe '#strip' do | ||
class User < OpenStruct | ||
include OStruct::Sanitizer | ||
strip :email, :phone | ||
end | ||
|
||
let(:user) do | ||
User.new( | ||
email: " [email protected] ", | ||
phone: " (55) 51 00000000 ", | ||
email: ' [email protected] ', | ||
phone: ' (55) 51 00000000 ' | ||
) | ||
end | ||
|
||
it "strips out leading and trailing spaces from user's email" do | ||
expect(user.email).to eq "[email protected]" | ||
expect(user.email).to eq '[email protected]' | ||
end | ||
|
||
it "strips out leading and trailing spaces from user's phone number" do | ||
expect(user.phone).to eq "(55) 51 00000000" | ||
expect(user.phone).to eq '(55) 51 00000000' | ||
end | ||
|
||
it "does not sanitize if value is nil" do | ||
it 'does not sanitize if value is nil' do | ||
user = User.new email: nil, phone: nil | ||
expect(user.email).to be nil | ||
expect(user.phone).to be nil | ||
end | ||
end | ||
|
||
describe "#digits" do | ||
describe '#digits' do | ||
class User < OpenStruct | ||
include OStruct::Sanitizer | ||
digits :ssn, :cell_phone | ||
end | ||
|
||
let(:user) do | ||
User.new( | ||
ssn: "111-11-1111", | ||
cell_phone: "+1-541-000-0000", | ||
ssn: '111-11-1111', | ||
cell_phone: '+1-541-000-0000' | ||
) | ||
end | ||
|
||
it "keeps only digits from ssn field" do | ||
expect(user.ssn).to eq "111111111" | ||
it 'keeps only digits from ssn field' do | ||
expect(user.ssn).to eq '111111111' | ||
end | ||
|
||
it "keeps only digits from cell_phone field" do | ||
expect(user.cell_phone).to eq "15410000000" | ||
it 'keeps only digits from cell_phone field' do | ||
expect(user.cell_phone).to eq '15410000000' | ||
end | ||
|
||
it "does not sanitize if value is nil" do | ||
it 'does not sanitize if value is nil' do | ||
user = User.new ssn: nil, cell_phone: nil | ||
expect(user.ssn).to be nil | ||
expect(user.cell_phone).to be nil | ||
end | ||
end | ||
|
||
describe "#sanitize" do | ||
describe '#sanitize' do | ||
class User < OpenStruct | ||
include OStruct::Sanitizer | ||
sanitize "my ssn" do |value| | ||
sanitize 'my ssn' do |value| | ||
value.to_s.gsub(/[^0-9]/, '') | ||
end | ||
end | ||
|
||
context "hash syntax" do | ||
it "applies sanitization rules using string as key" do | ||
context 'hash syntax' do | ||
it 'applies sanitization rules using string as key' do | ||
user = User.new | ||
user["my ssn"] = "111-11-1111" | ||
expect(user["my ssn"]).to eq "111111111" | ||
user['my ssn'] = '111-11-1111' | ||
expect(user['my ssn']).to eq '111111111' | ||
end | ||
|
||
it "applies sanitization rules using symbol as key" do | ||
it 'applies sanitization rules using symbol as key' do | ||
user = User.new | ||
user[:"my ssn"] = "111-11-1111" | ||
expect(user["my ssn"]).to eq "111111111" | ||
user[:"my ssn"] = '111-11-1111' | ||
expect(user['my ssn']).to eq '111111111' | ||
end | ||
|
||
it "applies sanitization rules using string as key in the constructor" do | ||
user = User.new "my ssn" => "111-11-1111" | ||
expect(user[:"my ssn"]).to eq "111111111" | ||
it 'applies sanitization rules using string as key in the constructor' do | ||
user = User.new 'my ssn' => '111-11-1111' | ||
expect(user[:"my ssn"]).to eq '111111111' | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters