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'
}
CONNECTIVES =

Logical connectives supported by the arXiv search API.

{
  and: 'AND',
  or: 'OR',
  and_not: 'ANDNOT'
}
CONNECTIVE_METHODS =

Logical connective method names.

{
  '&': :and,
  '!': :and_not,
  '|': :or
}
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

Instance Method Summary collapse

Constructor Details

#initialize(*ids, sort_by: :relevance, sort_order: :descending) {|_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).

Yields:

  • (_self)

Yield Parameters:

  • _self (Arx::Query)

    the object that the method was called on



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/arx/query/query.rb', line 68

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| extract_id id}
    Validate.ids ids
    @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



11
12
13
# File 'lib/arx/query/query.rb', line 11

def query
  @query
end

Instance Method Details

#!self

Logical negated conjunction (ANDNOT) of subqueries.

Returns:

  • (self)

See Also:



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

#&self

Logical conjunction (AND) of subqueries.

Returns:

  • (self)

See Also:



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

#abstract(*values, exact: false, connective: :and) ⇒ self

Search for papers by abstract.

Parameters:

  • values (Array<String>)

    Abstract(s) of papers to search for.

  • exact (Boolean) (defaults to: false)

    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 120

#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)


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/arx/query/query.rb', line 160

FIELDS.each do |name, field|
  define_method(name) do |*values, exact: true, connective: :and|
    return if values.empty?

    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

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

Search for papers by author.

Parameters:

  • values (Array<String>)

    Author(s) of papers to search for.

  • exact (Boolean) (defaults to: false)

    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 113

#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 141

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

Search for papers by comment.

Parameters:

  • values (Array<String>)

    Comment(s) of papers to search for.

  • exact (Boolean) (defaults to: false)

    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 127

#journal(*values, exact: false, 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: false)

    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 134

#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 147

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

Search for papers by title.

Parameters:

  • values (Array<String>)

    Title(s) of papers to search for.

  • exact (Boolean) (defaults to: false)

    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 106

#to_sString

Returns the query string.

Returns:

  • (String)


188
189
190
# File 'lib/arx/query/query.rb', line 188

def to_s
  @query
end

#|self

Logical disjunction (OR) of subqueries.

Returns:

  • (self)

See Also:



102
103
104
# File 'lib/arx/query/query.rb', line 102

CONNECTIVE_METHODS.each do |symbol, connective|
  define_method(symbol) { add_connective connective }
end