json_schematize 0.1.0 → 0.3.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: cba47a2b20c23bb096ad36a9f71c0ebb8608da8b5ecf42746dcb7c1c223b4aeb
4
- data.tar.gz: 227345574280f3dc5de25990b600976393e2bb6f970370b90cf00d1a371913e4
3
+ metadata.gz: 545006e10e9c73489b4a2db37d7b787a49c4427dd58d541ac938bad4c73954ce
4
+ data.tar.gz: 38f4d89c9cb2ffc8b9863531d8745eae5acd2b2bc1379ea6e9f459d66550525e
5
5
  SHA512:
6
- metadata.gz: d53863642a7b6b1fec0e22917508ee6c9a0a8a6452ea36955f3d8349878edf42487d267978d4175f3da72b0fc87651b66f4f4a28a3c1a98b7cc8c61858b0eb1e
7
- data.tar.gz: 639c2eabdcabb64fbbcc89aa83e8e6f9e314424d2bcc31a44b710aef887e98fda3a2120edae8952c2ff3d34caacbaf450a5a40ed06417397d68743c7883c9a60
6
+ metadata.gz: d640bd68de88a3e6e3b4a73fd8688e9c017bbb8948a73abb3a2f4bf36a87c3ba1bd31bb08f052b8fd36bca9a7758f825f32448ae39930610eaae7323ccd336fb
7
+ data.tar.gz: 344f9dd96df21d5b5f8f8b6daca078281403606c5b75dcc958d999186380e83a56ddb592f071b271656882ad9d4a117ce7211ca050cb901e4f57fbb90fe2355e
data/.circleci/config.yml CHANGED
@@ -182,8 +182,8 @@ workflows:
182
182
  - publish-rubygems:
183
183
  requires:
184
184
  - required-matrix-tests
185
- # filters:
186
- # branches:
187
- # only:
188
- # - main
185
+ filters:
186
+ branches:
187
+ only:
188
+ - main
189
189
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json_schematize (0.1.0)
4
+ json_schematize (0.3.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -85,7 +85,14 @@ required -- Default is true. When not set, each instance class can optionally de
85
85
  converter -- Proc return is set to the field value. No furter validation is done. Given (value) as a parameter
86
86
  array_of_types -- Detailed example above. Set this value to true when the dig param is to an array and you want all values in array to be parsed the given type
87
87
  ```
88
+ ### Custom Classes
88
89
 
90
+ ```ruby
91
+ class CustomClasses < JsonSchematize::Generator
92
+ # JsonSchematize::Boolean can be used as a type when expecting a conversion of possible true or false values converted into a TrueClass or FalseClass
93
+ add_field name: :internals, type: JsonSchematize::Boolean
94
+ end
95
+ ```
89
96
 
90
97
  ## Development
91
98
 
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.authors = ["Matt Taylor"]
9
9
  spec.email = ["mattius.taylor@gmail.com"]
10
10
 
11
- spec.summary = "Describe the gem here"
12
- spec.description = "Describe the gem here"
11
+ spec.summary = "This gem gives you the ability to turn API results into a standardized schema's that can be easily grocked"
12
+ spec.description = "Take standardized API results and turn them into a Schema"
13
13
  spec.homepage = "https://github.com/matt-taylor/json_schematize"
14
14
  spec.license = "MIT"
15
15
 
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class JsonSchematize::Base
4
+ def self.acceptable_types
5
+ raise NoMethodError, "Expected acceptable_values to be defined in parent class"
6
+ end
7
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json_schematize/base"
4
+
5
+ class JsonSchematize::Boolean < JsonSchematize::Base
6
+ FALSE_VALUES = ["false", "f", "0", false]
7
+ TRUE_VALUES = ["true", "t", "1", true]
8
+
9
+ def self.new(val)
10
+ return false if FALSE_VALUES.include?(val)
11
+ return true if TRUE_VALUES.include?(val)
12
+
13
+ raise JsonSchematize::UndefinedBoolean, "#{val} is not a valid #{self.class}"
14
+ end
15
+
16
+ def self.acceptable_types
17
+ [TrueClass, FalseClass]
18
+ end
19
+ end
@@ -36,9 +36,9 @@ class JsonSchematize::Field
36
36
 
37
37
  def acceptable_value?(transformed_value:, raise_on_error:)
38
38
  if array_of_types
39
- boolean = transformed_value.all? { |val| @acceptable_types.include?(val.class) }
39
+ boolean = transformed_value.all? { |val| validate_acceptable_types(val: val) }
40
40
  else
41
- boolean = @acceptable_types.include?(transformed_value.class)
41
+ boolean = validate_acceptable_types(val: transformed_value)
42
42
  end
43
43
 
44
44
  if raise_on_error && (boolean==false)
@@ -77,6 +77,18 @@ class JsonSchematize::Field
77
77
 
78
78
  private
79
79
 
80
+ def validate_acceptable_types(val:)
81
+ (all_allowed_types + @acceptable_types).include?(val.class)
82
+ end
83
+
84
+ def all_allowed_types
85
+ @all_allowed_types ||= begin
86
+ @acceptable_types.map do |t|
87
+ t.acceptable_types if t.ancestors.include?(JsonSchematize::Base)
88
+ end.compact.flatten
89
+ end
90
+ end
91
+
80
92
  def iterate_array_of_types(value:)
81
93
  return raw_converter_call(value: value) unless array_of_types
82
94
 
@@ -67,9 +67,9 @@ class JsonSchematize::Generator
67
67
 
68
68
  attr_reader :__raw_params, :raise_on_error
69
69
 
70
-
71
- def initialize(raise_on_error: true, **params)
72
- @__params = params
70
+ # stringified_params allows for params with stringed keys
71
+ def initialize(stringified_params = {}, raise_on_error: true, **params)
72
+ @__params = stringified_params.empty? ? params : stringified_params
73
73
  @raise_on_error = raise_on_error
74
74
 
75
75
  validate_required!
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonSchematize
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.1"
5
5
  end
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "json_schematize/version"
3
+ require "json_schematize/base"
4
+ require "json_schematize/boolean"
4
5
  require "json_schematize/generator"
6
+ require "json_schematize/version"
5
7
 
6
8
  module JsonSchematize
7
9
  class Error < StandardError; end
@@ -10,4 +12,7 @@ module JsonSchematize
10
12
  class InvalidFieldByValidator < InvalidField; end
11
13
  class InvalidFieldByType < InvalidField; end
12
14
  class InvalidFieldByArrayOfTypes < InvalidField; end
15
+
16
+ ## Customized class errors
17
+ class UndefinedBoolean < Error; end
13
18
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_schematize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Taylor
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-08 00:00:00.000000000 Z
11
+ date: 2022-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry-byebug
@@ -66,7 +66,7 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 0.17.0
69
- description: Describe the gem here
69
+ description: Take standardized API results and turn them into a Schema
70
70
  email:
71
71
  - mattius.taylor@gmail.com
72
72
  executables: []
@@ -88,6 +88,8 @@ files:
88
88
  - docker-compose.yml
89
89
  - json_schematize.gemspec
90
90
  - lib/json_schematize.rb
91
+ - lib/json_schematize/base.rb
92
+ - lib/json_schematize/boolean.rb
91
93
  - lib/json_schematize/field.rb
92
94
  - lib/json_schematize/field_transformations.rb
93
95
  - lib/json_schematize/field_validators.rb
@@ -117,5 +119,6 @@ requirements: []
117
119
  rubygems_version: 3.2.3
118
120
  signing_key:
119
121
  specification_version: 4
120
- summary: Describe the gem here
122
+ summary: This gem gives you the ability to turn API results into a standardized schema's
123
+ that can be easily grocked
121
124
  test_files: []