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.



8
9
10
11
12
13
14
15
16
# File 'lib/mangadex/internal/definition.rb', line 8

def initialize(key, value, converts: nil, accepts: nil, required: false)
  @converts = converts
  @key = key
  @value = convert_value(value)
  @raw_value = value
  @accepts = accepts
  @required = required ? true : false
  @errors = Array.new
end

Instance Attribute Details

#acceptsObject (readonly)

Returns the value of attribute accepts.



5
6
7
# File 'lib/mangadex/internal/definition.rb', line 5

def accepts
  @accepts
end

#convertsObject (readonly)

Returns the value of attribute converts.



5
6
7
# File 'lib/mangadex/internal/definition.rb', line 5

def converts
  @converts
end

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'lib/mangadex/internal/definition.rb', line 6

def errors
  @errors
end

#keyObject (readonly)

Returns the value of attribute key.



5
6
7
# File 'lib/mangadex/internal/definition.rb', line 5

def key
  @key
end

#requiredObject (readonly)

Returns the value of attribute required.



5
6
7
# File 'lib/mangadex/internal/definition.rb', line 5

def required
  @required
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/mangadex/internal/definition.rb', line 5

def value
  @value
end

Class Method Details

.chapter_list(args) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/mangadex/internal/definition.rb', line 103

def chapter_list(args)
  validate(
    args,
    {
      limit: { accepts: Integer },
      offset: { accepts: Integer },
      translated_language: { accepts: String },
      original_language: { accepts: [String] },
      excluded_original_language: { accepts: [String] },
      content_rating: { accepts: %w(safe suggestive erotica pornographic), converts: converts(:to_a) },
      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: converts(:to_a) },
    },
  )
end

.converts(key = nil) ⇒ Object



96
97
98
99
100
101
# File 'lib/mangadex/internal/definition.rb', line 96

def converts(key=nil)
  procs = { to_a: -> ( x ) { Array(x) } }
  return procs if key.nil?

  procs[key]
end

.validate(args, definition) ⇒ 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
151
152
153
154
155
156
157
158
# File 'lib/mangadex/internal/definition.rb', line 123

def validate(args, definition)
  args = Hash(args).with_indifferent_access
  definition = Hash(definition).with_indifferent_access
  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|
    validator = Definition.new(key, args[key], **definition.symbolize_keys)
    validation_error = validator.error
    if validation_error
      errors << { message: validation_error }
    elsif !validator.empty?
      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
end

Instance Method Details

#convert_value(value) ⇒ Object



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

def convert_value(value)
  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)


18
19
20
# File 'lib/mangadex/internal/definition.rb', line 18

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

#errorObject



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

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

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  validate!
rescue ArgumentError
  false
end

#validate!Object



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

def validate!
  validate_required!
  validate_accepts!

  true
end

#validate_accepts!Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mangadex/internal/definition.rb', line 59

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



51
52
53
54
55
56
57
# File 'lib/mangadex/internal/definition.rb', line 51

def validate_required!
  return unless required

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