Class: Arx::Query
- Inherits:
-
Object
- Object
- Arx::Query
- Defined in:
- lib/arx/query/query.rb
Overview
Class for generating arXiv search API query strings.
Constant Summary collapse
- PARAMS =
Mapping for URL query parameters supported by the arXiv search API.
{ search_query: 'search_query', id_list: 'id_list', sort_by: 'sortBy', sort_order: 'sortOrder' }
- CONNECTIVES =
Logical connectives supported by the arXiv search API.
{ and: 'AND', or: 'OR', and_not: 'ANDNOT' }
- FIELDS =
Supported fields for the search queries made to the arXiv search API.
{ title: 'ti', # Title author: 'au', # Author abstract: 'abs', # Abstract comment: 'co', # Comment journal: 'jr', # Journal reference category: 'cat', # Subject category report: 'rn', # Report number all: 'all' # All (of the above) }
- SORT_BY =
Supported criteria for the
sortBy
parameter. { relevance: 'relevance', last_updated: 'lastUpdated', date_submitted: 'submittedDate' }
- SORT_ORDER =
Supported criteria for the
sortOrder
parameter. { ascending: 'ascending', descending: 'descending' }
Instance Attribute Summary collapse
-
#query ⇒ String
The string representing the search query.
Instance Method Summary collapse
-
#abstract(*values, exact: true, connective: :and) ⇒ self
Search for papers by abstract.
-
#all(*values, exact: true, connective: :and) ⇒ self
Search for papers by all fields (see FIELDS).
-
#and ⇒ self
Logical conjunction (
AND
) of subqueries. -
#and_not ⇒ self
Logical negated conjunction (
ANDNOT
) of subqueries. -
#author(*values, exact: true, connective: :and) ⇒ self
Search for papers by author.
-
#category(*values, connective: :and) ⇒ self
Search for papers by category.
-
#comment(*values, exact: true, connective: :and) ⇒ self
Search for papers by comment.
-
#group ⇒ self
Creates a nested subquery (grouped statements with parentheses).
-
#initialize(*ids, sort_by: :relevance, sort_order: :descending) {|_self| ... } ⇒ Query
constructor
Initializes a new Query object.
-
#journal(*values, exact: true, connective: :and) ⇒ self
Search for papers by journal reference.
-
#or ⇒ self
Logical disjunction (
OR
) of subqueries. -
#report(*values, connective: :and) ⇒ self
Search for papers by report number.
-
#title(*values, exact: true, connective: :and) ⇒ self
Search for papers by title.
-
#to_s ⇒ String
Returns the query string.
Constructor Details
#initialize(*ids, sort_by: :relevance, sort_order: :descending) {|_self| ... } ⇒ Query
Initializes a new Query object.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/arx/query/query.rb', line 59 def initialize(*ids, sort_by: :relevance, sort_order: :descending) @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]}" 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 |
Instance Attribute Details
#query ⇒ String
The string representing the search query.
8 9 10 |
# File 'lib/arx/query/query.rb', line 8 def query @query end |
Instance Method Details
#abstract(*values, exact: true, connective: :and) ⇒ self
Search for papers by abstract.
|
# File 'lib/arx/query/query.rb', line 115
|
#all(*values, exact: true, connective: :and) ⇒ self
Search for papers by all fields (see FIELDS).
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/arx/query/query.rb', line 161 FIELDS.each do |name, field| define_method(name) do |*values, exact: true, connective: :and| return if values.empty? values.flatten! Validate.values values Validate.categories values if name == :category Validate.exact exact Validate.connective connective, permitted: CONNECTIVES.keys values.map! &CGI.method(:escape) # Forms a field:value pair pair = ->(value){"#{field}:#{exact ? enquote(value) : value}"} subquery = if values.size > 1 parenthesize values.map(&pair).join("+#{CONNECTIVES[connective]}+") else pair.(values.first) end add_subquery subquery self end end |
#and ⇒ self
Logical conjunction (AND
) of subqueries.
|
# File 'lib/arx/query/query.rb', line 77
|
#and_not ⇒ self
Logical negated conjunction (ANDNOT
) of subqueries.
|
# File 'lib/arx/query/query.rb', line 83
|
#author(*values, exact: true, connective: :and) ⇒ self
Search for papers by author.
|
# File 'lib/arx/query/query.rb', line 107
|
#category(*values, connective: :and) ⇒ self
Search for papers by category.
|
# File 'lib/arx/query/query.rb', line 139
|
#comment(*values, exact: true, connective: :and) ⇒ self
Search for papers by comment.
|
# File 'lib/arx/query/query.rb', line 123
|
#group ⇒ self
Creates a nested subquery (grouped statements with parentheses).
191 192 193 194 195 196 197 198 199 200 |
# File 'lib/arx/query/query.rb', line 191 def group add_connective :and unless end_with_connective? @query << (search_query? ? '+' : "&#{PARAMS[:search_query]}=") @query << CGI.escape('(') yield @query << CGI.escape(')') self end |
#journal(*values, exact: true, connective: :and) ⇒ self
Search for papers by journal reference.
|
# File 'lib/arx/query/query.rb', line 131
|
#or ⇒ self
Logical disjunction (OR
) of subqueries.
95 96 97 |
# File 'lib/arx/query/query.rb', line 95 CONNECTIVES.keys.each do |connective| define_method(connective) { add_connective connective } end |
#report(*values, connective: :and) ⇒ self
Search for papers by report number.
|
# File 'lib/arx/query/query.rb', line 146
|
#title(*values, exact: true, connective: :and) ⇒ self
Search for papers by title.
|
# File 'lib/arx/query/query.rb', line 99
|
#to_s ⇒ String
Returns the query string.
205 206 207 |
# File 'lib/arx/query/query.rb', line 205 def to_s @query end |