Skip to content

Commit

Permalink
Prototype.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaHydrae committed Oct 9, 2014
1 parent 3fe70af commit 1c67081
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/errapi.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module Errapi
VERSION = '0.1.0'
end

Dir[File.join File.dirname(__FILE__), File.basename(__FILE__, '.*'), '*.rb'].each{ |lib| require lib }
39 changes: 39 additions & 0 deletions lib/errapi/validation_context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Errapi

class ValidationContext
attr_reader :errors

def initialize
@errors = []
end

def add message, options = {}
@errors << ValidationError.new(message, options)
end

def error? criteria = {}
return !@errors.empty? if criteria.empty?

@errors.any? do |err|
string_matches?(err, criteria, :message) &&
value_matches?(err, criteria, :code) &&
string_matches?(err, criteria, :type) &&
string_matches?(err, criteria, :location)
end
end

private

def error_matches? err, criteria, attr, &block
!criteria.key?(attr) || block.call(err.send(attr), criteria[attr])
end

def value_matches? err, criteria, attr
error_matches?(err, criteria, attr){ |err_value,criterion| err_value == criterion }
end

def string_matches? err, criteria, attr
error_matches?(err, criteria, attr){ |err_value,criterion| criterion.kind_of?(Regexp) ? !err_value.match(criterion).nil? : err_value == criterion }
end
end
end
13 changes: 13 additions & 0 deletions lib/errapi/validation_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Errapi

class ValidationError
attr_accessor :message, :code, :type, :location

def initialize message, options = {}
@message = message
@code = options[:code]
@type = options[:type]
@location = options[:location]
end
end
end
33 changes: 33 additions & 0 deletions spec/validation_context_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'helper'

RSpec.describe Errapi::ValidationContext do

it "should have an empty array of errors" do
expect(subject.errors).to eq([])
end

it "should indicate that there are no errors" do
expect(subject.error?).to be(false)
end

describe "with errors" do
before :each do
subject.add 'foo'
subject.add 'bar'
subject.add 'baz'
end

it "should match an error by message" do
expect(subject.error?(message: 'foo')).to be(true)
expect(subject.error?(message: 'bar')).to be(true)
expect(subject.error?(message: 'baz')).to be(true)
expect(subject.error?(message: 'qux')).to be(false)
end

it "should match an error by message with a regexp" do
expect(subject.error?(message: /foo/)).to be(true)
expect(subject.error?(message: /ba/)).to be(true)
expect(subject.error?(message: /q/)).to be(false)
end
end
end
24 changes: 24 additions & 0 deletions spec/validation_error_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'helper'

RSpec.describe Errapi::ValidationError do
let(:options){ {} }
subject{ described_class.new 'foo', options }

it "should have a message" do
expect_error message: 'foo'
end

describe "with options" do
let(:options){ { code: 100, type: 'json', location: '/pointer' } }

it "should have a code, type and location" do
expect_error options.merge(message: 'foo')
end
end

def expect_error options = {}
[ :message, :code, :type, :location ].each do |attr|
expect(subject.send(attr)).to eq(options.key?(attr) ? options[attr] : nil)
end
end
end

0 comments on commit 1c67081

Please sign in to comment.