Module: Arx::Validate
- Defined in:
- lib/arx/query/validate.rb
Overview
Validations for arXiv search query fields and identifier schemes.
Class Method Summary collapse
-
.categories(categories) ⇒ Object
Validates a list of arXiv categories.
-
.connective(value, permitted:) ⇒ Object
Validates a logical connective.
-
.exact(value) ⇒ Object
Validates the
exact
parameter. -
.id?(id) ⇒ Boolean
Validates an arXiv identifier of both the old and new schemes.
-
.ids(ids) ⇒ Object
Validates a list of arXiv paper identifiers.
-
.paging(start, max_results) ⇒ Object
Validates the paging fields of the query string:
start
andmax_results
. -
.sort_by(value, permitted:) ⇒ Object
Validates the
sortBy
field of the query string. -
.sort_order(value, permitted:) ⇒ Object
Validates the
sortOrder
field of the query string. -
.values(values) ⇒ Object
Validates a list of values for the fields of the search query string.
Class Method Details
.categories(categories) ⇒ Object
This is only called after values, so there is no need to check types.
Validates a list of arXiv categories.
99 100 101 102 103 |
# File 'lib/arx/query/validate.rb', line 99 def categories(categories) categories.each do |category| raise ArgumentError.new("Unrecognized arXiv category (#{category}). See Arx::CATEGORIES.") unless Arx::CATEGORIES.keys.include? category end end |
.connective(value, permitted:) ⇒ Object
Validates a logical connective.
75 76 77 78 |
# File 'lib/arx/query/validate.rb', line 75 def connective(value, permitted:) raise TypeError.new("Expected `connective` to be a Symbol, got: #{value.class}") unless value.is_a? Symbol raise ArgumentError.new("Expected `connective` to be one of #{permitted}, got: #{value}") unless permitted.include? value end |
.exact(value) ⇒ Object
Validates the exact
parameter.
64 65 66 |
# File 'lib/arx/query/validate.rb', line 64 def exact(value) raise TypeError.new("Expected `exact` to be boolean (TrueClass or FalseClass), got: #{value.class}") unless value == !!value end |
.id?(id) ⇒ Boolean
Validates an arXiv identifier of both the old and new schemes.
109 110 111 112 113 |
# File 'lib/arx/query/validate.rb', line 109 def id?(id) return true if NEW_IDENTIFIER_FORMAT.match? id return true if OLD_IDENTIFIER_FORMAT.match?(id) && Arx::CATEGORIES.keys.include?(id.split('/').first) false end |
.ids(ids) ⇒ Object
Validates a list of arXiv paper identifiers.
51 52 53 54 55 56 57 |
# File 'lib/arx/query/validate.rb', line 51 def ids(ids) raise TypeError.new("Expected `ids` to be an Array, got: #{ids.class}") unless ids.is_a? Array ids.each do |id| raise TypeError.new("Expected identifier to be a String, got: #{id.class} (#{id})") unless id.is_a? String raise ArgumentError.new("Malformed arXiv identifier: #{id}") unless id? id end end |
.paging(start, max_results) ⇒ Object
Validates the paging fields of the query string: start
and max_results
.
39 40 41 42 |
# File 'lib/arx/query/validate.rb', line 39 def paging(start, max_results) raise TypeError.new("Expected `start` to be an Integer, got: #{start.class}") unless start.is_a? Integer raise TypeError.new("Expected `max_results` to be an Integer, got: #{max_results.class}") unless max_results.is_a? Integer end |
.sort_by(value, permitted:) ⇒ Object
Validates the sortBy
field of the query string.
15 16 17 18 |
# File 'lib/arx/query/validate.rb', line 15 def sort_by(value, permitted:) raise TypeError.new("Expected `sort_by` to be a Symbol, got: #{value.class}") unless value.is_a? Symbol raise ArgumentError.new("Expected `sort_by` to be one of #{permitted}, got: #{value}") unless permitted.include? value end |
.sort_order(value, permitted:) ⇒ Object
Validates the sortOrder
field of the query string.
27 28 29 30 |
# File 'lib/arx/query/validate.rb', line 27 def sort_order(value, permitted:) raise TypeError.new("Expected `sort_order` to be a Symbol, got: #{value.class}") unless value.is_a? Symbol raise ArgumentError.new("Expected `sort_order` to be one of #{permitted}, got: #{value}") unless permitted.include? value end |
.values(values) ⇒ Object
Validates a list of values for the fields of the search query string.
86 87 88 89 90 91 |
# File 'lib/arx/query/validate.rb', line 86 def values(values) raise TypeError.new("Expected `values` to be an Array, got: #{values.class}") unless values.is_a? Array values.each do |value| raise TypeError.new("Expected value to be a String, got: #{value.class} (#{value})") unless value.is_a? String end end |