despecable 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3232f2f309244ea0e9e1d0385c4a585b7623b5ef
4
+ data.tar.gz: d294097b285d62c699515e7cd7640f5548961a0a
5
+ SHA512:
6
+ metadata.gz: a71ad8719c0b18a3a86546502efd749fce8020b2efb8df0599102f6c4779b54f39928bde439d94f0ebdaabca677a9319892abbf05c488068fd336baf4b8fed19
7
+ data.tar.gz: d905c46e795d619e7b0d462754bcad750e3b7374b241888f466a52966d1e2b2cc7a4444aee156f72af021998c468aff0250c07aa0904ac3c2d8001f283fcb458
@@ -0,0 +1,11 @@
1
+ module Despecable
2
+ module ActionController
3
+ def despec(&blk)
4
+ Despecable::Me.new(params.deep_dup).doit(&blk)
5
+ end
6
+
7
+ def dspec!(&blk)
8
+ Despecable::Me.new(params).doit(&blk)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1 @@
1
+ Despecable::DespecableError = Class.new(StandardError)
@@ -0,0 +1 @@
1
+ Despecable::IncorrectParameterError = Class.new(Despecable::DespecableError)
@@ -0,0 +1 @@
1
+ Despecable::InvalidParameterError = Class.new(Despecable::DespecableError)
@@ -0,0 +1 @@
1
+ Despecable::MissingParameterError = Class.new(Despecable::DespecableError)
@@ -0,0 +1,15 @@
1
+ module Despecable
2
+ class Me
3
+ attr_reader :params
4
+
5
+ def initialize(params)
6
+ @params = params
7
+ end
8
+
9
+ def doit(&blk)
10
+ spectator = Despecable::Spectator.new(@params)
11
+ spectator.instance_eval(&blk)
12
+ return spectator.params
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,55 @@
1
+ module Despecable
2
+ class Spectacles
3
+ attr_reader :params
4
+
5
+ def initialize(params)
6
+ @params = params
7
+ end
8
+
9
+ def validate_param(name, options)
10
+ validate_param_presence(name) if options[:required]
11
+ validate_param_value(name, options[:in]) if options.key?(:in) && params.key?(name)
12
+ end
13
+
14
+ def validate_param_presence(name)
15
+ raise Despecable::MissingParameterError, "Missing required param: '#{name}'" if !params.key?(name)
16
+ end
17
+
18
+ def validate_param_value(name, allowed_values)
19
+ raise Despecable::IncorrectParameterError, "Incorrect value for param: '#{name}'" if !allowed_values.include?(params[name])
20
+ end
21
+
22
+ def integer(name)
23
+ int = params[name].to_i
24
+ raise Despecable::InvalidParameterError, "Invalid value for param: '#{name}'. Required type: integer." if int.to_s != params[name].sub(/^0+/, "")
25
+ return int
26
+ end
27
+
28
+ def string(name)
29
+ # This is really a no-op.
30
+ params[name].to_s
31
+ end
32
+
33
+ def boolean(name)
34
+ case params[name].to_s
35
+ when "true", "1" then true
36
+ when "false", "0", nil then false
37
+ else raise Despecable::InvalidParameterError, "Invalid value for param: '#{name}'. Require type: boolean (1/0 or true/false)"
38
+ end
39
+ end
40
+
41
+ def date(name)
42
+ Date.rfc3339(params[name] + "T00:00:00+00:00")
43
+ rescue ArgumentError
44
+ raise unless $!.message == "invalid date"
45
+ raise Despecable::InvalidParameterError, "Invalid value for param: '#{name}'. Required type: date (e.g. '2012-12-31')."
46
+ end
47
+
48
+ def datetime(name)
49
+ DateTime.rfc3339(params[name])
50
+ rescue ArgumentError
51
+ raise unless $!.message == "invalid date"
52
+ raise Despecable::InvalidParameterError, "Invalid value for param: '#{name}'. Required type: rfc3339 datetime (e.g. '2009-06-19T00:00:00-04:00')."
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,43 @@
1
+ module Despecable
2
+ class Spectator < BasicObject
3
+ attr_reader :params
4
+
5
+ def initialize(params)
6
+ @params = params
7
+ @spectacles = ::Despecable::Spectacles.new(params)
8
+ end
9
+
10
+ def integer(name, options = {})
11
+ _spec(name, :integer, options)
12
+ end
13
+
14
+ def string(name, options = {})
15
+ _spec(name, :string, options)
16
+ end
17
+
18
+ def boolean(name, options = {})
19
+ _spec(name, :boolean, options)
20
+ end
21
+
22
+ def date(name, options = {})
23
+ _spec(name, :date, options)
24
+ end
25
+
26
+ def datetime(name, options = {})
27
+ _spec(name, :datetime, options)
28
+ end
29
+
30
+ private
31
+
32
+ def _spec(name, type, options = {})
33
+ if params.key?(name)
34
+ params[name] = @spectacles.public_send(type, name)
35
+ elsif options.key?(:default)
36
+ params[name] = options[:default]
37
+ end
38
+ @spectacles.validate_param(name, options)
39
+ return params[name]
40
+ end
41
+
42
+ end
43
+ end
data/lib/despecable.rb ADDED
@@ -0,0 +1,11 @@
1
+ module Despecable; end
2
+
3
+ require "despecable/action_controller.rb"
4
+ require "despecable/me.rb"
5
+ require "despecable/spectator.rb"
6
+ require "despecable/spectacles.rb"
7
+
8
+ require "despecable/errors/despecable_error.rb"
9
+ require "despecable/errors/missing_parameter_error.rb"
10
+ require "despecable/errors/incorrect_parameter_error.rb"
11
+ require "despecable/errors/invalid_parameter_error.rb"
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: despecable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Schwartz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Write self-documenting parameter validation and type-casting in your
14
+ API actions.
15
+ email: ozydingo@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/despecable.rb
21
+ - lib/despecable/action_controller.rb
22
+ - lib/despecable/errors/despecable_error.rb
23
+ - lib/despecable/errors/incorrect_parameter_error.rb
24
+ - lib/despecable/errors/invalid_parameter_error.rb
25
+ - lib/despecable/errors/missing_parameter_error.rb
26
+ - lib/despecable/me.rb
27
+ - lib/despecable/spectacles.rb
28
+ - lib/despecable/spectator.rb
29
+ homepage: https://github.com/ozydingo/despecable
30
+ licenses:
31
+ - MIT
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 2.6.13
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: Easy self-documenting parameter specifications for API routes in Rails
53
+ test_files: []