-
Notifications
You must be signed in to change notification settings - Fork 93
add UriFilter to additional filters #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
estraph
wants to merge
1
commit into
cypriss:master
Choose a base branch
from
estraph:raph/uri-filter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| require 'uri' | ||
|
|
||
| module Mutations | ||
| class UriFilter < AdditionalFilter | ||
| @default_options = { | ||
| :nils => false, # true allows an explicit nil to be valid. Overrides any other options | ||
| :scheme => nil, # restrict the URI to a specific scheme, i.e. 'https' | ||
| } | ||
|
|
||
| def filter(data) | ||
| if data.nil? | ||
| return [nil, nil] if options[:nils] | ||
| return [nil, :nils] | ||
| end | ||
|
|
||
| case data | ||
| when URI # we're good! | ||
| when String then | ||
| return [nil, :blank] if blank?(data) | ||
| begin | ||
| data = URI.parse(data) | ||
| rescue StandardError => e | ||
| return [nil, e.message] | ||
| end | ||
| else return [nil, :invalid] | ||
| end | ||
|
|
||
| if !options[:scheme].nil? | ||
| return [nil, :scheme] if blank?(data.scheme) | ||
| return [nil, :scheme] if data.scheme.to_sym != options[:scheme] | ||
| end | ||
|
|
||
| [data, nil] | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def blank?(value) | ||
| return true if value.nil? | ||
| value = value.strip if value.is_a?(String) | ||
| return value.empty? if value.respond_to?(:empty?) | ||
| return false | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| require 'spec_helper' | ||
|
|
||
| describe Mutations::UriFilter do | ||
| let(:options){ {} } | ||
| let(:outcome){ Mutations::UriFilter.new(options).filter(input) } | ||
| let(:result){ outcome[0] } | ||
| let(:errors){ outcome[1] } | ||
|
|
||
| describe "#blank" do | ||
| subject{ Mutations::UriFilter.new.send(:blank?, value) } | ||
|
|
||
| describe "nil" do | ||
| let(:value){ nil } | ||
| it{ assert(subject) } | ||
| end | ||
|
|
||
| describe "empty string" do | ||
| let(:value){ "" } | ||
| it{ assert(subject) } | ||
| end | ||
|
|
||
| describe "whitespace" do | ||
| let(:value){ " " } | ||
| it{ assert(subject) } | ||
| end | ||
|
|
||
| describe "some text" do | ||
| let(:value){ "abc" } | ||
| it{ assert(!subject) } | ||
| end | ||
| end | ||
|
|
||
| describe 'invalid type input' do | ||
| let(:input){ true } | ||
|
|
||
| it{ assert_nil(result) } | ||
| it{ assert_equal(errors, :invalid) } | ||
| end | ||
|
|
||
| describe 'string input' do | ||
| let(:input){ 'http://www.altavista.com' } | ||
|
|
||
| describe 'is blank' do | ||
| let(:input){ '' } | ||
|
|
||
| it{ assert_nil(result) } | ||
| it{ assert_equal(errors, :blank) } | ||
| end | ||
|
|
||
| describe 'invalid uri' do | ||
| let(:input){ 'oops' } | ||
|
|
||
| it "returns the error" do | ||
| URI.stub :parse, lambda {|x| raise 'invalid URI'} do | ||
| assert_nil(result) | ||
| assert_equal(errors, 'invalid URI') | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe 'with scheme constraint' do | ||
|
|
||
| describe 'matching constraint' do | ||
| let(:options){ { scheme: :http } } | ||
|
|
||
| it{ assert_equal(result, URI.parse(input)) } | ||
| it{ assert_nil(errors) } | ||
| end | ||
|
|
||
| describe 'not matching constraint' do | ||
| let(:options){ { scheme: :https} } | ||
|
|
||
| it{ assert_nil(result) } | ||
| it{ assert_equal(errors, :scheme) } | ||
| end | ||
|
|
||
| describe 'and blank url scheme' do | ||
| let(:options){ { scheme: :http} } | ||
| let(:input){ 'altavista.com' } | ||
|
|
||
| it{ assert_nil(result) } | ||
| it{ assert_equal(errors, :scheme) } | ||
| end | ||
| end | ||
|
|
||
| describe 'without scheme constraint' do | ||
| describe 'and blank url scheme' do | ||
| let(:input){ 'altavista.com' } | ||
|
|
||
| it{ assert_equal(result, URI.parse(input)) } | ||
| it{ assert_nil(errors) } | ||
| end | ||
| end | ||
|
|
||
| end | ||
|
|
||
| describe 'uri input' do | ||
| let(:input){ URI.parse('http://www.altavista.com') } | ||
|
|
||
| describe 'with scheme constraint' do | ||
| describe 'matching constraint' do | ||
| let(:options){ { scheme: :http } } | ||
|
|
||
| it{ assert_equal(result, input) } | ||
| it{ assert_nil(errors) } | ||
| end | ||
|
|
||
| describe 'not matching constraint' do | ||
| let(:options){ { scheme: :https} } | ||
|
|
||
| it{ assert_nil(result) } | ||
| it{ assert_equal(errors, :scheme) } | ||
| end | ||
|
|
||
| describe 'and blank url scheme' do | ||
| let(:options){ { scheme: :http} } | ||
| let(:input){ URI.parse('altavista.com') } | ||
|
|
||
| it{ assert_nil(result) } | ||
| it{ assert_equal(errors, :scheme) } | ||
| end | ||
| end | ||
|
|
||
| describe 'without scheme constraint' do | ||
| describe 'and blank url scheme' do | ||
| let(:input){ URI.parse('altavista.com') } | ||
|
|
||
| it{ assert_equal(result, input) } | ||
| it{ assert_nil(errors) } | ||
| end | ||
| end | ||
|
|
||
| end | ||
|
|
||
| describe 'nil input' do | ||
| let(:input){ nil } | ||
|
|
||
| describe 'nils allowed' do | ||
| let(:options){ { nils: true } } | ||
|
|
||
| it{ assert_nil(result) } | ||
| it{ assert_nil(errors) } | ||
| end | ||
|
|
||
| describe 'nils not allowed' do | ||
| let(:options){ { nils: false } } | ||
|
|
||
| it{ assert_nil(result) } | ||
| it{ assert_equal(errors, :nils) } | ||
| end | ||
|
|
||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@estraph The filter operation always starts from the
HashFilterand from there it will filter the nested input. The results of the nested filter will always flow back toHashFilter.What I can see is all basic filter like
StringFilter,FloatFilterand etc always returnArray<Object, Symbol>in#filter. Are you sure returninge.messagehere will be handled gracefully by the error handler?I think you might see the issue if you write a rspec with a
Mutation::Commandsthat will trigger error on this filter.