Class: Mangadex::Internal::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/mangadex/internal/definition.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, value, converts: nil, accepts: nil, required: false) ⇒ Definition

Returns a new instance of Definition.



15
16
17
18
19
20
21
22
23
# File 'lib/mangadex/internal/definition.rb', line 15

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

#acceptsObject (readonly)

Returns the value of attribute accepts.



12
13
14
# File 'lib/mangadex/internal/definition.rb', line 12

def accepts
  @accepts
end

#convertsObject (readonly)

Returns the value of attribute converts.



12
13
14
# File 'lib/mangadex/internal/definition.rb', line 12

def converts
  @converts
end

#errorsObject (readonly)

Returns the value of attribute errors.



13
14
15
# File 'lib/mangadex/internal/definition.rb', line 13

def errors
  @errors
end

#keyObject (readonly)

Returns the value of attribute key.



12
13
14
# File 'lib/mangadex/internal/definition.rb', line 12

def key
  @key
end

#requiredObject (readonly)

Returns the value of attribute required.



12
13
14
# File 'lib/mangadex/internal/definition.rb', line 12

def required
  @required
end

#valueObject (readonly)

Returns the value of attribute value.



12
13
14
# File 'lib/mangadex/internal/definition.rb', line 12

def value
  @value
end

Class Method Details

.chapter_list(args) ⇒ Object



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
150
# File 'lib/mangadex/internal/definition.rb', line 123

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



113
114
115
116
117
118
119
120
121
# File 'lib/mangadex/internal/definition.rb', line 113

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



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/mangadex/internal/definition.rb', line 152

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



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
211
# File 'lib/mangadex/internal/definition.rb', line 165

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.error_message
    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?
    error_message = 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: #{error_message}"
  end

  args.symbolize_keys.select do |_, value|
    value.presence
  end
end

Instance Method Details

#convert_value(value) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mangadex/internal/definition.rb', line 48

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

Returns:

  • (Boolean)


25
26
27
# File 'lib/mangadex/internal/definition.rb', line 25

def empty?
  value.respond_to?(:empty?) ? value.empty? : value.to_s.strip.empty?
end

#errorObject



42
43
44
45
46
# File 'lib/mangadex/internal/definition.rb', line 42

def error
  validate! && nil
rescue ArgumentError => error
  error.message
end

#valid?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
# File 'lib/mangadex/internal/definition.rb', line 36

def valid?
  validate!
rescue ArgumentError
  false
end

#validate!Object



29
30
31
32
33
34
# File 'lib/mangadex/internal/definition.rb', line 29

def validate!
  validate_required!
  validate_accepts!

  true
end

#validate_accepts!Object



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
110
# File 'lib/mangadex/internal/definition.rb', line 76

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



68
69
70
71
72
73
74
# File 'lib/mangadex/internal/definition.rb', line 68

def validate_required!
  return unless required

  if empty?
    raise ArgumentError, "Missing :#{key}"
  end
end