gate 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ca10961ba809a110a1fe5af46de9ff94b50a0134
4
- data.tar.gz: 3f9ed5c3837ee371396248371202d80590aa141d
3
+ metadata.gz: 09c3db4ff831be9cbcee9a0fc5c4666cf25e0fd6
4
+ data.tar.gz: b8fe4ff7b05976382e00b91dca04496b330cd0a0
5
5
  SHA512:
6
- metadata.gz: 0e8126657fe7a0624943d221a36706783aaa8876fc8f97fc64f7f3e305ce96e03e78e7509ae6f2df9294c690feb69f2b834f7389dc109c2a95451b6d21e49bb9
7
- data.tar.gz: 4887dd75399c5962689a63c05dd1af62cd994df927d382f98b09ac6148c10b95548471859d23ed05077d42ae13d1f8429d530fcb81b15ae067ed6219fc2eee74
6
+ metadata.gz: f2e1837f27d8e33f1f73365c9a231f465b3e3abdcca1b036f22b7194dfd1d918341de980d492115b5e1feac1b797450d2b83356e303d4633ccd06414e77d50b1
7
+ data.tar.gz: 32fd22c8502b461615a89d6fe4b8edf6592218083ead3d796d627593b84b6fa990d1c23926c32eb557b41b8a04eba90eedff0f70446e8f439a060d16a1fa5ac6
data/.codeclimate.yml ADDED
@@ -0,0 +1,8 @@
1
+ engines:
2
+ rubocop:
3
+ enabled: true
4
+ bundler-audit:
5
+ enabled: true
6
+ ratings:
7
+ paths:
8
+ - "lib/**"
data/.rubocop.yml ADDED
@@ -0,0 +1,19 @@
1
+ Style/Documentation:
2
+ Description: Document classes and non-namespace modules.
3
+ Enabled: false
4
+ Style/StringLiterals:
5
+ Description: Checks if uses of quotes match the configured preference.
6
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#consistent-string-literals
7
+ Enabled: true
8
+ EnforcedStyle: double_quotes
9
+ SupportedStyles:
10
+ - single_quotes
11
+ - double_quotes
12
+ Style/StringLiteralsInInterpolation:
13
+ Description: Checks if uses of quotes inside expressions in interpolated strings
14
+ match the configured preference.
15
+ Enabled: true
16
+ EnforcedStyle: single_quotes
17
+ SupportedStyles:
18
+ - single_quotes
19
+ - double_quotes
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in gate.gemspec
4
4
  gemspec
data/README.md CHANGED
@@ -1,13 +1,12 @@
1
1
  # Gate
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/gate.svg)](http://badge.fury.io/rb/gate)
3
4
  [![Circle CI](https://circleci.com/gh/monterail/gate.svg?style=shield)](https://circleci.com/gh/monterail/gate)
4
5
  [![Dependency Status](https://gemnasium.com/monterail/gate.svg)](https://gemnasium.com/monterail/gate)
5
6
  [![Code Climate](https://codeclimate.com/github/monterail/gate/badges/gpa.svg)](https://codeclimate.com/github/monterail/gate)
6
7
  [![Test Coverage](https://codeclimate.com/github/monterail/gate/badges/coverage.svg)](https://codeclimate.com/github/monterail/gate/coverage)
7
8
 
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
9
+ Gate is a small library which allows you to define allowed structure for user input with required and optional parameters and to coerce them into defined types.
11
10
 
12
11
  ## Installation
13
12
 
@@ -27,7 +26,26 @@ Or install it yourself as:
27
26
 
28
27
  ## Usage
29
28
 
30
- TODO: Write usage instructions here
29
+ Define structure
30
+
31
+ ```ruby
32
+ gate = Gate.rules do
33
+ required :id, :Integer
34
+ required :message do
35
+ required :title # :String by default
36
+ optional :value, :Decimal
37
+ end
38
+ end
39
+ ```
40
+
41
+ Verify it
42
+
43
+ ```ruby
44
+ result = gate.verify(params)
45
+ result.valid? # => true / false
46
+ result.attributes # => hash with only allowed parameters
47
+ result.errors # => hash { key => error }
48
+ ```
31
49
 
32
50
  ## Development
33
51
 
data/Rakefile CHANGED
@@ -2,8 +2,8 @@ require "bundler/gem_tasks"
2
2
  require "rake/testtask"
3
3
 
4
4
  Rake::TestTask.new do |t|
5
- t.libs << 'test'
6
- t.pattern = 'test/**/test_*.rb'
5
+ t.libs << "test"
6
+ t.pattern = "test/**/test_*.rb"
7
7
  end
8
8
 
9
9
  task default: [:test]
data/bin/setup ADDED
@@ -0,0 +1,5 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
data/gate.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
2
+ lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'gate/version'
4
+ require "gate/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "gate"
@@ -9,12 +9,12 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Jan Dudulski"]
10
10
  spec.email = ["jan@dudulski.pl"]
11
11
 
12
- spec.summary = %q{Handling user input with ease}
13
- spec.description = %q{Validate and coerce user input against defined structure.}
12
+ spec.summary = "Handling user input with ease"
13
+ spec.description = "Validate and coerce user input against defined structure."
14
14
  spec.homepage = "https://github.com/monterail/gate"
15
15
  spec.license = "MIT"
16
16
 
17
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^test/}) }
18
18
  spec.require_paths = ["lib"]
19
19
 
20
20
  spec.add_runtime_dependency "coercible", "~> 1.0"
data/lib/gate.rb CHANGED
@@ -8,13 +8,13 @@ module Gate
8
8
  end
9
9
  end
10
10
 
11
- require 'axiom-types'
12
- require 'coercible'
13
- require 'forwardable'
14
- require 'set'
11
+ require "axiom-types"
12
+ require "coercible"
13
+ require "forwardable"
14
+ require "set"
15
15
 
16
- require 'gate/coercer'
17
- require 'gate/configuration'
18
- require 'gate/guard'
19
- require 'gate/result'
20
- require 'gate/version'
16
+ require "gate/coercer"
17
+ require "gate/configuration"
18
+ require "gate/guard"
19
+ require "gate/result"
20
+ require "gate/version"
data/lib/gate/coercer.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  module Gate
2
2
  class Coercer
3
3
  def initialize(engine, type)
4
- raise CoercionError, "Doesn't know how to coerce into #{type}" unless Axiom::Types.const_defined?(type)
4
+ unless Axiom::Types.const_defined?(type)
5
+ fail CoercionError, "Doesn't know how to coerce into #{type}"
6
+ end
5
7
 
6
8
  @engine = engine
7
9
  @type = type
@@ -9,7 +11,7 @@ module Gate
9
11
  end
10
12
 
11
13
  def coerce(input)
12
- engine[String].public_send(coercion_method, input)
14
+ engine[detect_input_type(input)].public_send(coercion_method, input)
13
15
  rescue Coercible::UnsupportedCoercion
14
16
  raise CoercionError, "Doesn't know how to coerce #{input} into #{type}"
15
17
  end
@@ -17,5 +19,14 @@ module Gate
17
19
  private
18
20
 
19
21
  attr_reader :engine, :type, :coercion_method
22
+
23
+ def detect_input_type(input)
24
+ case input
25
+ when TrueClass, FalseClass, Array, Hash
26
+ input.class
27
+ else
28
+ String
29
+ end
30
+ end
20
31
  end
21
32
  end
data/lib/gate/guard.rb CHANGED
@@ -16,7 +16,7 @@ module Gate
16
16
 
17
17
  def _verify(input, name, rule, result)
18
18
  case
19
- when input.has_key?(name)
19
+ when input.key?(name)
20
20
  coerced = handle(input[name], name, rule)
21
21
  update(result, coerced)
22
22
  when configuration.required?(name)
@@ -28,7 +28,7 @@ module Gate
28
28
  end
29
29
 
30
30
  def handle(input, name, rule)
31
- if rule.kind_of?(Gate::Configuration)
31
+ if rule.is_a?(Gate::Configuration)
32
32
  result = Gate::Guard.new(rule).verify(input)
33
33
  { attributes: { name => result.attributes },
34
34
  errors: { name => result.errors } }
data/lib/gate/result.rb CHANGED
@@ -25,18 +25,14 @@ module Gate
25
25
  private
26
26
 
27
27
  def _errors(hash)
28
- hash.reduce({}) do |result, (k, v)|
29
- if v.kind_of?(Hash)
28
+ hash.each_with_object({}) do |(k, v), result|
29
+ if v.is_a?(Hash)
30
30
  nested = _errors(v)
31
31
 
32
- if nested.any?
33
- result[k] = nested
34
- end
32
+ result[k] = nested if nested.any?
35
33
  else
36
34
  result[k] = v
37
35
  end
38
-
39
- result
40
36
  end
41
37
  end
42
38
  end
data/lib/gate/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Gate
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Dudulski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-23 00:00:00.000000000 Z
11
+ date: 2015-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: coercible
@@ -115,13 +115,16 @@ executables: []
115
115
  extensions: []
116
116
  extra_rdoc_files: []
117
117
  files:
118
+ - ".codeclimate.yml"
118
119
  - ".gitignore"
120
+ - ".rubocop.yml"
119
121
  - ".travis.yml"
120
122
  - Gemfile
121
123
  - LICENSE.txt
122
124
  - README.md
123
125
  - Rakefile
124
126
  - bin/console
127
+ - bin/setup
125
128
  - circle.yml
126
129
  - gate.gemspec
127
130
  - lib/gate.rb
@@ -150,9 +153,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
153
  version: '0'
151
154
  requirements: []
152
155
  rubyforge_project:
153
- rubygems_version: 2.2.2
156
+ rubygems_version: 2.4.5
154
157
  signing_key:
155
158
  specification_version: 4
156
159
  summary: Handling user input with ease
157
160
  test_files: []
158
- has_rdoc: