class Arx::Query
Class for generating arXiv search API query strings.
@attr query [String] The string representing the search query.
Constants
- CONNECTIVES
Logical connectives supported by the arXiv search API.
- FIELDS
Supported fields for the search queries made to the arXiv search API.
@see arxiv.org/help/prep arXiv metadata fields @see arxiv.org/help/api/user-manual#query_details arXiv user manual (query details)
- PARAMS
Mapping for URL query parameters supported by the arXiv search API.
- SORT_BY
Supported criteria for the
sortBy
parameter.- SORT_ORDER
Supported criteria for the
sortOrder
parameter.
Public Class Methods
Initializes a new Query
object.
@param ids [Array<String>] The IDs of the arXiv papers to restrict the query to. @param sort_by [Symbol] The sorting criteria for the returned results (see {SORT_BY}). @param sort_order [Symbol] The sorting order for the returned results (see {SORT_ORDER}). @param start [Integer] The index of the first returned result. @param max_results [Integer] The number of results returned by the query @return [Query] The initialized query object.
# File lib/arx/query/query.rb, line 65 def initialize(*ids, sort_by: :relevance, sort_order: :descending, start: 0, max_results: 10) @query = String.new Validate.sort_by sort_by, permitted: SORT_BY.keys @query << "#{PARAMS[:sort_by]}=#{SORT_BY[sort_by]}" Validate.sort_order sort_order, permitted: SORT_ORDER.keys @query << "&#{PARAMS[:sort_order]}=#{SORT_ORDER[sort_order]}" Validate.paging start, max_results @query << "&#{PARAMS[:start]}=#{start}&#{PARAMS[:max_results]}=#{max_results}" ids.flatten! unless ids.empty? ids.map! {|id| Cleaner.extract_id(id, version: true)} @query << "&#{PARAMS[:id_list]}=#{ids * ','}" end yield self if block_given? end
Public Instance Methods
Creates a nested subquery (grouped statements with parentheses).
@return [self]
# File lib/arx/query/query.rb, line 215 def group add_connective :and unless end_with_connective? @query << (search_query? ? '+' : "&#{PARAMS[:search_query]}=") @query << CGI.escape('(') yield @query << CGI.escape(')') self end
Returns the query string.
@return [String]
# File lib/arx/query/query.rb, line 229 def to_s @query end
Private Instance Methods
Appends a logical connective to the end of the query string.
@see CONNECTIVES
@param connective [Symbol] The symbol of the logical connective to add. @return [self]
# File lib/arx/query/query.rb, line 240 def add_connective(connective) if search_query? @query << "+#{CONNECTIVES[connective]}" unless end_with_connective? || start_of_group? end self end
Appends a subquery to the end of the query string.
@param subquery [String] The subquery to add.
# File lib/arx/query/query.rb, line 250 def add_subquery(subquery) add_connective :and unless end_with_connective? if search_query? @query << (start_of_group? ? "#{subquery}" : "+#{subquery}") else @query << "&#{PARAMS[:search_query]}=#{subquery}" end end
Whether the query string ends in a logical connective.
@see CONNECTIVES
@return [Boolean]
# File lib/arx/query/query.rb, line 272 def end_with_connective? CONNECTIVES.values.any? &@query.method(:end_with?) end
Enquotes a string with CGI-escaped double quotes.
@param string [String] The string to enquote. @return [String] The enquoted string.
# File lib/arx/query/query.rb, line 295 def enquote(string) CGI.escape("\"") + string + CGI.escape("\"") end
Parenthesizes a string with CGI-escaped parentheses.
@param string [String] The string to parenthesize. @return [String] The parenthesized string.
# File lib/arx/query/query.rb, line 287 def parenthesize(string) CGI.escape('(') + string + CGI.escape(')') end
Whether the query string contains the search_query
parameter.
@see PARAMS
@return [Boolean]
# File lib/arx/query/query.rb, line 264 def search_query? @query.include? PARAMS[:search_query] end
Whether the query string ends in a start-of-group character '('.
@return [Boolean]
# File lib/arx/query/query.rb, line 279 def start_of_group? @query.end_with? CGI.escape('(') end