Class: Arx::Query

Inherits:
Object
  • Object
show all
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',
  start: 'start',
  max_results: 'max_results',
}
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
  updated_at: 'lastUpdatedDate', # Last updated date
  submitted_at: 'submittedDate', # Submission date
  all: 'all'                     # All (of the above)
}
SORT_BY =

Supported criteria for the sortBy parameter.

{
  relevance: 'relevance',
  updated_at: 'lastUpdatedDate',
  submitted_at: 'submittedDate'
}
SORT_ORDER =

Supported criteria for the sortOrder parameter.

{
  ascending: 'ascending',
  descending: 'descending'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*ids, sort_by: :relevance, sort_order: :descending, start: 0, max_results: 10) {|_self| ... } ⇒ Query

Initializes a new Query object.

Parameters:

  • ids (Array<String>)

    The IDs of the arXiv papers to restrict the query to.

  • sort_by (Symbol) (defaults to: :relevance)

    The sorting criteria for the returned results (see SORT_BY).

  • sort_order (Symbol) (defaults to: :descending)

    The sorting order for the returned results (see SORT_ORDER).

  • start (Integer) (defaults to: 0)

    The index of the first returned result.

  • max_results (Integer) (defaults to: 10)

    The number of results returned by the query

Yields:

  • (_self)

Yield Parameters:

  • _self (Arx::Query)

    the object that the method was called on



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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

Instance Attribute Details

#queryString

The string representing the search query.

Returns:

  • (String)

    the current value of 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.

Parameters:

  • values (Array<String>)

    Abstract(s) of papers to search for.

  • exact (Boolean) (defaults to: true)

    Whether to search for an exact match of the abstract(s).

  • connective (Symbol) (defaults to: :and)

    The logical connective to use (see CONNECTIVES). Only applies if there are multiple values.

Returns:

  • (self)


# File 'lib/arx/query/query.rb', line 124


#all(*values, exact: true, connective: :and) ⇒ self

Search for papers by all fields (see FIELDS).

Parameters:

  • values (Array<String>)

    Field value(s) of papers to search for.

  • exact (Boolean) (defaults to: true)

    Whether to search for an exact match of the comment(s).

  • connective (Symbol) (defaults to: :and)

    The logical connective to use (see CONNECTIVES). Only applies if there are multiple values.

Returns:

  • (self)


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
# File 'lib/arx/query/query.rb', line 184

FIELDS.each do |name, field|
  _exact = ![:updated_at, :submitted_at].include?(name)
  define_method(name) do |*values, exact: _exact, 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

#andself

Logical conjunction (AND) of subqueries.

Returns:

  • (self)

See Also:



# File 'lib/arx/query/query.rb', line 86


#and_notself

Logical negated conjunction (ANDNOT) of subqueries.

Returns:

  • (self)

See Also:



# File 'lib/arx/query/query.rb', line 92


#author(*values, exact: true, connective: :and) ⇒ self

Search for papers by author.

Parameters:

  • values (Array<String>)

    Author(s) of papers to search for.

  • exact (Boolean) (defaults to: true)

    Whether to search for an exact match of the author’s name(s).

  • connective (Symbol) (defaults to: :and)

    The logical connective to use (see CONNECTIVES). Only applies if there are multiple values.

Returns:

  • (self)


# File 'lib/arx/query/query.rb', line 116


#category(*values, connective: :and) ⇒ self

Search for papers by category.

Parameters:

  • values (Array<String>)

    Category(s) of papers to search for.

  • connective (Symbol) (defaults to: :and)

    The logical connective to use (see CONNECTIVES). Only applies if there are multiple values.

Returns:

  • (self)


# File 'lib/arx/query/query.rb', line 148


#comment(*values, exact: true, connective: :and) ⇒ self

Search for papers by comment.

Parameters:

  • values (Array<String>)

    Comment(s) of papers to search for.

  • exact (Boolean) (defaults to: true)

    Whether to search for an exact match of the comment(s).

  • connective (Symbol) (defaults to: :and)

    The logical connective to use (see CONNECTIVES). Only applies if there are multiple values.

Returns:

  • (self)


# File 'lib/arx/query/query.rb', line 132


#groupself

Creates a nested subquery (grouped statements with parentheses).

Returns:

  • (self)


215
216
217
218
219
220
221
222
223
224
# 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

#journal(*values, exact: true, connective: :and) ⇒ self

Search for papers by journal reference.

Parameters:

  • values (Array<String>)

    Journal reference(s) of papers to search for.

  • exact (Boolean) (defaults to: true)

    Whether to search for an exact match of the journal refernece(s).

  • connective (Symbol) (defaults to: :and)

    The logical connective to use (see CONNECTIVES). Only applies if there are multiple values.

Returns:

  • (self)


# File 'lib/arx/query/query.rb', line 140


#orself

Logical disjunction (OR) of subqueries.

Returns:

  • (self)

See Also:



104
105
106
# File 'lib/arx/query/query.rb', line 104

CONNECTIVES.keys.each do |connective|
  define_method(connective) { add_connective connective }
end

#report(*values, connective: :and) ⇒ self

Search for papers by report number.

Parameters:

  • values (Array<String>)

    Report number(s) of papers to search for.

  • connective (Symbol) (defaults to: :and)

    The logical connective to use (see CONNECTIVES). Only applies if there are multiple values.

Returns:

  • (self)


# File 'lib/arx/query/query.rb', line 155


#submitted_at(*values, connective: :and) ⇒ self

Search for papers by submittedDate.

Parameters:

  • values (Array<String>)

    submittedDate (string or range) of papers to search for.

  • connective (Symbol) (defaults to: :and)

    The logical connective to use (see CONNECTIVES). Only applies if there are multiple values.

Returns:

  • (self)


# File 'lib/arx/query/query.rb', line 169


#title(*values, exact: true, connective: :and) ⇒ self

Search for papers by title.

Parameters:

  • values (Array<String>)

    Title(s) of papers to search for.

  • exact (Boolean) (defaults to: true)

    Whether to search for an exact match of the title(s).

  • connective (Symbol) (defaults to: :and)

    The logical connective to use (see CONNECTIVES). Only applies if there are multiple values.

Returns:

  • (self)


# File 'lib/arx/query/query.rb', line 108


#to_sString

Returns the query string.

Returns:

  • (String)


229
230
231
# File 'lib/arx/query/query.rb', line 229

def to_s
  @query
end

#updated_at(*values, connective: :and) ⇒ self

Search for papers by lastUpdatedDate.

Parameters:

  • values (Array<String>)

    lastUpdatedDate (string or range) of papers to search for.

  • connective (Symbol) (defaults to: :and)

    The logical connective to use (see CONNECTIVES). Only applies if there are multiple values.

Returns:

  • (self)


# File 'lib/arx/query/query.rb', line 162