gate 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ca10961ba809a110a1fe5af46de9ff94b50a0134
4
+ data.tar.gz: 3f9ed5c3837ee371396248371202d80590aa141d
5
+ SHA512:
6
+ metadata.gz: 0e8126657fe7a0624943d221a36706783aaa8876fc8f97fc64f7f3e305ce96e03e78e7509ae6f2df9294c690feb69f2b834f7389dc109c2a95451b6d21e49bb9
7
+ data.tar.gz: 4887dd75399c5962689a63c05dd1af62cd994df927d382f98b09ac6148c10b95548471859d23ed05077d42ae13d1f8429d530fcb81b15ae067ed6219fc2eee74
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gate.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Jan Dudulski
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # Gate
2
+
3
+ [![Circle CI](https://circleci.com/gh/monterail/gate.svg?style=shield)](https://circleci.com/gh/monterail/gate)
4
+ [![Dependency Status](https://gemnasium.com/monterail/gate.svg)](https://gemnasium.com/monterail/gate)
5
+ [![Code Climate](https://codeclimate.com/github/monterail/gate/badges/gpa.svg)](https://codeclimate.com/github/monterail/gate)
6
+ [![Test Coverage](https://codeclimate.com/github/monterail/gate/badges/coverage.svg)](https://codeclimate.com/github/monterail/gate/coverage)
7
+
8
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/gate`. To experiment with that code, run `bin/console` for an interactive prompt.
9
+
10
+ TODO: Delete this and the text above, and describe your gem
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'gate'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install gate
27
+
28
+ ## Usage
29
+
30
+ TODO: Write usage instructions here
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it ( https://github.com/[my-github-username]/gate/fork )
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.pattern = 'test/**/test_*.rb'
7
+ end
8
+
9
+ task default: [:test]
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "gate"
5
+
6
+ require "pry"
7
+ Pry.start
data/circle.yml ADDED
@@ -0,0 +1,3 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.2.0
data/gate.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gate/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gate"
8
+ spec.version = Gate::VERSION
9
+ spec.authors = ["Jan Dudulski"]
10
+ spec.email = ["jan@dudulski.pl"]
11
+
12
+ spec.summary = %q{Handling user input with ease}
13
+ spec.description = %q{Validate and coerce user input against defined structure.}
14
+ spec.homepage = "https://github.com/monterail/gate"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_runtime_dependency "coercible", "~> 1.0"
21
+ spec.add_runtime_dependency "axiom-types", "~> 0.1"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.9"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "pry", "~> 0.10"
26
+ spec.add_development_dependency "minitest", "~> 5.7"
27
+ spec.add_development_dependency "codeclimate-test-reporter", "~> 0.4"
28
+ end
data/lib/gate.rb ADDED
@@ -0,0 +1,20 @@
1
+ module Gate
2
+ class CoercionError < StandardError; end
3
+
4
+ def self.rules(&block)
5
+ configuration = Configuration.new(&block)
6
+
7
+ Guard.new(configuration)
8
+ end
9
+ end
10
+
11
+ require 'axiom-types'
12
+ require 'coercible'
13
+ require 'forwardable'
14
+ require 'set'
15
+
16
+ require 'gate/coercer'
17
+ require 'gate/configuration'
18
+ require 'gate/guard'
19
+ require 'gate/result'
20
+ require 'gate/version'
@@ -0,0 +1,21 @@
1
+ module Gate
2
+ class Coercer
3
+ def initialize(engine, type)
4
+ raise CoercionError, "Doesn't know how to coerce into #{type}" unless Axiom::Types.const_defined?(type)
5
+
6
+ @engine = engine
7
+ @type = type
8
+ @coercion_method = Axiom::Types.const_get(type).coercion_method
9
+ end
10
+
11
+ def coerce(input)
12
+ engine[String].public_send(coercion_method, input)
13
+ rescue Coercible::UnsupportedCoercion
14
+ raise CoercionError, "Doesn't know how to coerce #{input} into #{type}"
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :engine, :type, :coercion_method
20
+ end
21
+ end
@@ -0,0 +1,50 @@
1
+ module Gate
2
+ class Configuration
3
+ extend Forwardable
4
+
5
+ attr_reader :coercer
6
+ attr_reader :required_set, :optional_set, :nested_set
7
+ attr_reader :rules
8
+
9
+ def_delegator :@rules, :reduce
10
+
11
+ def initialize(coercer = Coercible::Coercer.new, &block)
12
+ @coercer = coercer
13
+ @required_set = Set.new
14
+ @optional_set = Set.new
15
+ @nested_set = Set.new
16
+ @rules = {}
17
+
18
+ instance_eval(&block)
19
+ end
20
+
21
+ def required?(name)
22
+ required_set.include?(name)
23
+ end
24
+
25
+ private
26
+
27
+ def required(name, type = :String, &block)
28
+ required_set.add(name)
29
+ register(name, type, &block)
30
+ end
31
+
32
+ def optional(name, type = :String, &block)
33
+ optional_set.add(name)
34
+ register(name, type, &block)
35
+ end
36
+
37
+ def register(name, type, &block)
38
+ @rules[name] = setup_rule(name, type, &block)
39
+ end
40
+
41
+ def setup_rule(name, type, &block)
42
+ if block_given?
43
+ nested_set.add(name)
44
+ Configuration.new(coercer, &block)
45
+ else
46
+ Coercer.new(coercer, type)
47
+ end
48
+ end
49
+ end
50
+ end
data/lib/gate/guard.rb ADDED
@@ -0,0 +1,52 @@
1
+ module Gate
2
+ class Guard
3
+ attr_reader :configuration
4
+
5
+ def initialize(configuration)
6
+ @configuration = configuration
7
+ end
8
+
9
+ def verify(input)
10
+ configuration.reduce(Result.new) do |result, (name, rule)|
11
+ Result.new(_verify(input, name, rule, result))
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def _verify(input, name, rule, result)
18
+ case
19
+ when input.has_key?(name)
20
+ coerced = handle(input[name], name, rule)
21
+ update(result, coerced)
22
+ when configuration.required?(name)
23
+ errors = result.errors.merge(name => :missing)
24
+ update(result, errors: errors)
25
+ else
26
+ result.to_h
27
+ end
28
+ end
29
+
30
+ def handle(input, name, rule)
31
+ if rule.kind_of?(Gate::Configuration)
32
+ result = Gate::Guard.new(rule).verify(input)
33
+ { attributes: { name => result.attributes },
34
+ errors: { name => result.errors } }
35
+ else
36
+ coerce(input, name, rule)
37
+ end
38
+ end
39
+
40
+ def coerce(input, name, rule)
41
+ { attributes: { name => rule.coerce(input) } }
42
+ rescue CoercionError
43
+ # TODO: log error
44
+ { errors: { name => :coercion_error } }
45
+ end
46
+
47
+ def update(result, attributes: {}, errors: {})
48
+ { attributes: result.attributes.merge(attributes),
49
+ errors: result.errors.merge(errors) }
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,43 @@
1
+ module Gate
2
+ class Result
3
+ attr_reader :attributes
4
+
5
+ def initialize(attributes: {}, errors: {})
6
+ @attributes = attributes
7
+ @errors = errors
8
+ end
9
+
10
+ def valid?
11
+ errors.empty?
12
+ end
13
+
14
+ def errors
15
+ _errors(@errors)
16
+ end
17
+
18
+ def to_h
19
+ {
20
+ attributes: attributes,
21
+ errors: @errors
22
+ }
23
+ end
24
+
25
+ private
26
+
27
+ def _errors(hash)
28
+ hash.reduce({}) do |result, (k, v)|
29
+ if v.kind_of?(Hash)
30
+ nested = _errors(v)
31
+
32
+ if nested.any?
33
+ result[k] = nested
34
+ end
35
+ else
36
+ result[k] = v
37
+ end
38
+
39
+ result
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module Gate
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jan Dudulski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: coercible
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: axiom-types
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.10'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.10'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.7'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5.7'
97
+ - !ruby/object:Gem::Dependency
98
+ name: codeclimate-test-reporter
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.4'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.4'
111
+ description: Validate and coerce user input against defined structure.
112
+ email:
113
+ - jan@dudulski.pl
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".travis.yml"
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - bin/console
125
+ - circle.yml
126
+ - gate.gemspec
127
+ - lib/gate.rb
128
+ - lib/gate/coercer.rb
129
+ - lib/gate/configuration.rb
130
+ - lib/gate/guard.rb
131
+ - lib/gate/result.rb
132
+ - lib/gate/version.rb
133
+ homepage: https://github.com/monterail/gate
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.2.2
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Handling user input with ease
157
+ test_files: []
158
+ has_rdoc: