Class: Mangadex::Internal::Definition
- Defined in:
- lib/mangadex/internal/definition.rb
Instance Attribute Summary collapse
-
#accepts ⇒ Object
readonly
Returns the value of attribute accepts.
-
#converts ⇒ Object
readonly
Returns the value of attribute converts.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
- .chapter_list(args) ⇒ Object
- .converts(key = nil) ⇒ Object
- .must(*args) ⇒ Object
- .validate(args, definition) ⇒ Object
Instance Method Summary collapse
- #convert_value(value) ⇒ Object
- #empty? ⇒ Boolean
- #error ⇒ Object
-
#initialize(key, value, converts: nil, accepts: nil, required: false) ⇒ Definition
constructor
A new instance of Definition.
- #valid? ⇒ Boolean
- #validate! ⇒ Object
- #validate_accepts! ⇒ Object
- #validate_required! ⇒ Object
Constructor Details
#initialize(key, value, converts: nil, accepts: nil, required: false) ⇒ Definition
Returns a new instance of Definition.
14 15 16 17 18 19 20 21 22 |
# File 'lib/mangadex/internal/definition.rb', line 14 def initialize(key, value, converts: nil, accepts: nil, required: false) @converts = converts @key = key @value = convert_value(value.presence) @raw_value = value @accepts = accepts @required = required ? true : false @errors = Array.new end |
Instance Attribute Details
#accepts ⇒ Object (readonly)
Returns the value of attribute accepts.
11 12 13 |
# File 'lib/mangadex/internal/definition.rb', line 11 def accepts @accepts end |
#converts ⇒ Object (readonly)
Returns the value of attribute converts.
11 12 13 |
# File 'lib/mangadex/internal/definition.rb', line 11 def converts @converts end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
12 13 14 |
# File 'lib/mangadex/internal/definition.rb', line 12 def errors @errors end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
11 12 13 |
# File 'lib/mangadex/internal/definition.rb', line 11 def key @key end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
11 12 13 |
# File 'lib/mangadex/internal/definition.rb', line 11 def required @required end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
11 12 13 |
# File 'lib/mangadex/internal/definition.rb', line 11 def value @value end |
Class Method Details
.chapter_list(args) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/mangadex/internal/definition.rb', line 122 def chapter_list(args) validate( args, { limit: { accepts: Integer, converts: :to_i }, offset: { accepts: Integer, converts: :to_i }, ids: { accepts: [String], converts: :to_a }, title: { accepts: String }, manga: { accepts: String }, groups: { accepts: [String], converts: :to_a }, uploader: { accepts: [String], converts: :to_a }, chapter: { accepts: [String], converts: :to_a }, translated_language: { accepts: [String], converts: :to_a }, original_language: { accepts: [String], converts: :to_a }, excluded_original_language: { accepts: [String], converts: :to_a }, content_rating: Definitions::ContentRating, include_future_updates: { accepts: %w(0 1) }, created_at_since: { accepts: %r{^\d{4}-[0-1]\d-([0-2]\d|3[0-1])T([0-1]\d|2[0-3]):[0-5]\d:[0-5]\d$} }, updated_at_since: { accepts: %r{^\d{4}-[0-1]\d-([0-2]\d|3[0-1])T([0-1]\d|2[0-3]):[0-5]\d:[0-5]\d$} }, publish_at_since: { accepts: %r{^\d{4}-[0-1]\d-([0-2]\d|3[0-1])T([0-1]\d|2[0-3]):[0-5]\d:[0-5]\d$} }, order: { accepts: Hash }, includes: { accepts: [String], converts: :to_a }, include_empty_pages: { accepts: [0, 1], converts: converts(:to_i) }, include_future_publish_at: { accepts: [0, 1], converts: converts(:to_i) }, include_external_url: { accepts: [0, 1], converts: converts(:to_i) }, }, ) end |
.converts(key = nil) ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/mangadex/internal/definition.rb', line 112 def converts(key=nil) procs = { to_a: -> ( x ) { Array(x) }, to_i: -> ( x ) { Integer(x.to_s, 10) if x }, } return procs if key.nil? procs[key] end |
.must(*args) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/mangadex/internal/definition.rb', line 151 def must(*args) args = args.each_with_index.map do |arg, index| ["arg_at_position_#{index}".to_sym, arg] end.to_h definition = args.keys.map do |key| [key, { required: true }] end.to_h validate(args, definition) args.values end |
.validate(args, definition) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/mangadex/internal/definition.rb', line 164 def validate(args, definition) args = Hash(args) definition = Hash(definition) return args if definition.empty? errors = [] extra_keys = args.keys - definition.keys extra_keys.each do |extra_key| errors << { extra: extra_key } end definition.each do |key, definition| validation_error = if definition.is_a?(Class) && definition < Definitions::Base validator = definition.new(args[key]) validator.validate validator. elsif !definition.is_a?(Class) validator = Definition.new(key, args[key], **definition.symbolize_keys) validator.error else raise "Invalid definition class: #{definition}" end if validation_error errors << { message: validation_error } elsif !validator.empty? && validator.value args[key] = validator.value end end if errors.any? = errors.map do |error| if error[:extra] "params[:#{error[:extra]}] does not exist and cannot be passed to this request" elsif error[:message] error[:message] else error.to_s end end.join(', ') raise ArgumentError, "Validation error: #{}" end args.symbolize_keys.select do |_, value| value.presence end end |
Instance Method Details
#convert_value(value) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/mangadex/internal/definition.rb', line 47 def convert_value(value) if converts.is_a?(Symbol) converts_proc = self.class.converts(converts) if converts_proc begin return converts_proc.call(value) rescue => exception raise ArgumentError.new("Proc parsing error: #{exception}") end end end if converts.is_a?(Proc) converts.call(value) elsif converts.is_a?(String) || converts.is_a?(Symbol) value.send(converts) else value end end |
#empty? ⇒ Boolean
24 25 26 |
# File 'lib/mangadex/internal/definition.rb', line 24 def empty? value.respond_to?(:empty?) ? value.empty? : value.to_s.strip.empty? end |
#error ⇒ Object
41 42 43 44 45 |
# File 'lib/mangadex/internal/definition.rb', line 41 def error validate! && nil rescue ArgumentError => error error. end |
#valid? ⇒ Boolean
35 36 37 38 39 |
# File 'lib/mangadex/internal/definition.rb', line 35 def valid? validate! rescue ArgumentError false end |
#validate! ⇒ Object
28 29 30 31 32 33 |
# File 'lib/mangadex/internal/definition.rb', line 28 def validate! validate_required! validate_accepts! true end |
#validate_accepts! ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/mangadex/internal/definition.rb', line 75 def validate_accepts! return if value.nil? && !required return unless accepts if accepts.is_a?(Class) && !value.is_a?(accepts) raise ArgumentError, "Expected :#{key} to be a #{accepts}, but got a #{value.class}" end if accepts.is_a?(Regexp) && !(accepts === value) raise ArgumentError, "Expected :#{key} to match /#{accepts}/" end if accepts.is_a?(Array) if accepts.count == 1 && accepts[0].is_a?(Class) expected_class = accepts[0] if !value.is_a?(Array) raise ArgumentError, "Expected :#{key} to be an Array of #{expected_class}, but got #{value}" end invalid_elements = [] value.each do |x| invalid_elements << x unless x.is_a?(expected_class) end return if invalid_elements.empty? bad = invalid_elements.map { |x| "<#{x}:#{x.class}>" } raise ArgumentError, "Expected elements in :#{key} to be an Array of #{expected_class}, but found #{bad}" else if value.is_a?(Array) extra_elements = value - accepts return if extra_elements.empty? raise ArgumentError, "Expected elements in :#{key} to be one of #{accepts}, but found #{extra_elements}" elsif !(value.nil? || (value.respond_to?(:empty?) && value.empty?)) && !accepts.include?(value) raise ArgumentError, "Expected :#{key} to be one of #{accepts}, but got #{@raw_value}:#{@raw_value.class}" end end end end |
#validate_required! ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/mangadex/internal/definition.rb', line 67 def validate_required! return unless required if empty? raise ArgumentError, "Missing :#{key}" end end |