lucky_param 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a017ce0d189d7f1c397675300a770e553ef058769f5b8250b451e0c5411bdc10
4
- data.tar.gz: 7cab2988621b49fd9c0b0d5075d8301ff554b2c72e403e0e110906b84117fcd5
3
+ metadata.gz: ccec36d0fcd976004cc8464dde95eaa07c28addad21077dde9081585c53d38ef
4
+ data.tar.gz: 98a62a979a11ef8f2c35c2963cace3a0e580baf834b22a3ba83049eb3e7f34e2
5
5
  SHA512:
6
- metadata.gz: 2926ebff40cdf3171069d567d287995bfbb41254903b2ea40266e398ce08645e7d6da9c8a54482c24abb6551eac6a938359f9032437f8d4ef3ccacef83594818
7
- data.tar.gz: 8185c5c344ae28cf992a575dce343a29c73748220712176710ff04eea4c1a35ca318c1e82b0466d23d052cadcfe3bda70d9ac9f5b96d49c9c78523486c766964
6
+ metadata.gz: f5ae2f511f00e5198f52832e8c9d4fb0e1095ad9fd5334924a3c11c4e208acf7a88bd780eb1843ad4d26478b6c69b755eec0b68472c47b87578704c15e39e0c0
7
+ data.tar.gz: 6c6c7d91ae8c226b796329d659b1d28dc0a9970087c7fc58b0b4f2a3ace1a6bad8f9654ead328ca0df665ae7d0a2b177d8cb1b61796689c76241a498bf794361
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lucky_param (0.1.0)
4
+ lucky_param (0.1.1)
5
5
  rails (>= 4.2)
6
6
 
7
7
  GEM
@@ -99,7 +99,7 @@ GEM
99
99
  method_source
100
100
  rake (>= 0.8.7)
101
101
  thor (>= 0.19.0, < 2.0)
102
- rake (10.5.0)
102
+ rake (12.3.1)
103
103
  sprockets (3.7.2)
104
104
  concurrent-ruby (~> 1.0)
105
105
  rack (> 1, < 3)
data/README.md CHANGED
@@ -2,28 +2,25 @@
2
2
 
3
3
  [![Build Status](https://travis-ci.org/shootingfly/lucky_param.svg?branch=master)](https://travis-ci.org/shootingfly/lucky_param)
4
4
 
5
- A simple parameter validation in controller for Rails.
5
+ A simple parameter checker in controller for Rails Api.
6
6
 
7
- Compare to `apipie`, `grape`, it is neccessary to check param in controller.
7
+ ### Do Check, Fail Fast
8
8
 
9
9
  ## Usage
10
10
 
11
+ Gemfile
11
12
  ```rb
12
13
  gem "lucky_param"
13
14
  ```
14
15
 
15
- ### Define any validator for your app:
16
16
  app/controllers/application_controller.rb
17
17
 
18
18
  ```rb
19
- class SessionControllers < ApplicationController
19
+ class ApplicationController
20
20
  include LuckyParam
21
21
 
22
+ # If necessary, you can overide or define your own parameter checkers.
22
23
  LuckyParam::CUSTOM_CHECKER = {
23
- Email: [
24
- ->(obj) { obj =~ /([0-9a-zA-Z]){6,30}/ },
25
- "must be valid email"
26
- ],
27
24
  Password: [
28
25
  ->(obj) { obj =~ /([0-9a-zA-Z]){6,30}/ },
29
26
  "must be valid password"
@@ -35,15 +32,30 @@ end
35
32
  app/controllers/sessions_controller.rb
36
33
 
37
34
  ```rb
38
- class SessionControllers < ApplicationController
35
+ class SessionsController < ApplicationController
39
36
  def create
40
37
  required :email, :Email
41
38
  required :password, :Password
39
+ optional :nick_name, :String
42
40
  render json: 'ok'
43
41
  end
44
42
  end
45
43
  ```
46
44
 
45
+ ## Internal Checkers
46
+
47
+ [Checkers](https://github.com/shootingfly/lucky_param/blob/master/lib/lucky_param/checker.rb)
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it ( https://github.com/shootingfly/lucky_param/fork )
52
+ 2. Create your feature branch (git checkout -b my-new-feature)
53
+ 3. Make your changes
54
+ 4. Run `ruby test/lucky_param_test.rb` to run the tests
55
+ 5. Commit your changes (git commit -am 'Add some feature')
56
+ 6. Push to the branch (git push origin my-new-feature)
57
+ 7. Create a new Pull Request
58
+
47
59
  ## License
48
60
 
49
61
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -6,21 +6,21 @@ module LuckyParam
6
6
  ->(_obj) { true }
7
7
  ],
8
8
  String: [
9
- ->(obj) { obj =~ /\w/ },
9
+ ->(obj) { !obj.empty? },
10
10
  "must be valid String"
11
11
  ],
12
12
  Integer: [
13
- ->(obj) { obj =~ /\d+/ },
13
+ ->(obj) { obj.to_i.to_s == obj },
14
14
  "must be valid Integer"
15
15
  ],
16
- Email: [
17
- ->(obj) { obj =~ /\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/ },
18
- "must be valid Email"
19
- ],
20
16
  Float: [
21
- ->(obj) { obj =~ /^(-?\d+)(\.\d+)?$/ },
17
+ ->(obj) { obj.to_f.to_s == obj },
22
18
  "must be valid Float"
23
19
  ],
20
+ Email: [
21
+ ->(obj) { obj =~ /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/ },
22
+ "must be valid Email"
23
+ ],
24
24
  Timestamp: [
25
25
  ->(obj) { obj =~ /^(\+|\-)?\d+$/ },
26
26
  "must be valid Timestamp"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LuckyParam
4
- VERSION = "0.1.0".freeze
4
+ VERSION = "0.1.1".freeze
5
5
  end
data/lib/lucky_param.rb CHANGED
@@ -5,37 +5,41 @@ require_relative "./lucky_param/checker"
5
5
 
6
6
  module LuckyParam
7
7
  class ParamMissError < StandardError
8
- def initialize(column)
9
- super "Missing Params: #{column}"
10
- end
11
8
  end
12
9
 
13
10
  class ParamFormatError < StandardError
14
- def initialize(message)
15
- super "Wrong Params Format: #{message}"
16
- end
17
11
  end
18
12
 
19
- def required(column, checker_type = :NONE)
20
- raise ParamMissError, column unless params.key?(column)
21
-
22
- message = _checker_message(column, checker_type)
23
- raise ParamFormatError, message if message
13
+ class UnknownCheckerError < StandardError
24
14
  end
25
15
 
26
- def optional(column, checker_type = :NONE)
27
- return unless params.key?(column)
28
-
29
- message = _checker_message(column, checker_type)
30
- raise ParamFormatError, message if message
16
+ def required(column, checker_type)
17
+ required_optional(:required, column, checker_type)
31
18
  end
32
19
 
33
- def _checker_message(column, checker_type)
34
- checker = CUSTOM_CHECKER[checker_type] if LuckyParam.const_defined?(:CUSTOM_CHECKER)
35
- checker ||= CHECKER.fetch(checker_type) {
36
- raise "Unknown checker `#{checker_type}`, try to define checker with const `LuckyParam::CUSTOM_CHECKER`"
37
- }
38
- result = checker[0].call(params[column])
39
- result ? nil : checker[1]
20
+ def optional(column, checker_type)
21
+ required_optional(:optional, column, checker_type)
40
22
  end
23
+
24
+ private
25
+
26
+ def required_optional(type, column, checker_type)
27
+ unless params[column]
28
+ return if type == :optional
29
+ raise ParamMissError, "Missing Params: #{column}"
30
+ end
31
+ message = checker_message(column, checker_type)
32
+ if message
33
+ raise ParamFormatError, "Wrong Params Format: #{message}"
34
+ end
35
+ end
36
+
37
+ def checker_message(column, checker_type)
38
+ checker = CUSTOM_CHECKER[checker_type] if LuckyParam.const_defined?(:CUSTOM_CHECKER)
39
+ checker ||= CHECKER.fetch(checker_type) {
40
+ raise UnknownCheckerError, "Unknown checker `#{checker_type}`, try to define checker with const `LuckyParam::CUSTOM_CHECKER`"
41
+ }
42
+ result = checker[0].call(params[column])
43
+ result ? nil : "#{column} #{checker[1]}"
44
+ end
41
45
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lucky_param
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shooting Fly