class Arx::Paper

Entity/model representing an arXiv paper.

Constants

ATTRIBUTES

The attributes of an arXiv paper. @note {comment}, {journal}, {pdf_url} and {doi_url} may raise errors when called.

Public Instance Methods

==(paper) click to toggle source

Equality check against another paper.

@note This only performs a basic equality check between the papers' identifiers (disregarding version).

This means that a different version of the same paper will be viewed as equal.

@param paper [Paper] The paper to compare against. @return [Boolean]

# File lib/arx/entities/paper.rb, line 230
def ==(paper)
  if paper.is_a? Paper
    id == paper.id
  else
    false
  end
end
as_json() click to toggle source

Serializes the {Paper} object into a valid JSON hash.

@note Deep-serializes {Author} and {Category} objects. @return [Hash] The resulting JSON hash.

# File lib/arx/entities/paper.rb, line 212
def as_json
  JSON.parse to_json
end
id(version = false) click to toggle source

The identifier of the paper.

@note This is either in {OLD_IDENTIFIER_FORMAT} or {NEW_IDENTIFIER_FORMAT}. @example

1705.01662v1
cond-mat/0211034

@param version [Boolean] Whether or not to include the paper's version. @return [String] The paper's identifier.

# File lib/arx/entities/paper.rb, line 32
def id(version = false)
  Cleaner.extract_id @id, version: version
end
revision?() click to toggle source

Whether the paper is a revision or not.

@note A paper is a revision if its {version} is greater than 1. @return [Boolean]

# File lib/arx/entities/paper.rb, line 58
def revision?
  version > 1
end
save(path) click to toggle source

Downloads the paper and saves it in PDF format at the specified path.

@param path [String] The file path to store the PDF at.

# File lib/arx/entities/paper.rb, line 241
def save(path)
  begin
    pdf_content = URI.open(pdf_url).read
    File.open(path, 'wb') {|f| f.write pdf_content}
  rescue
    File.delete(path) if File.file? path
    raise
  end
end
to_h(deep = false) click to toggle source

Serializes the {Paper} object into a Hash.

@param deep [Boolean] Whether to deep-serialize {Author} and {Category} objects. @return [Hash]

# File lib/arx/entities/paper.rb, line 198
def to_h(deep = false)
  Hash[*ATTRIBUTES.map {|_| [_, send(_)] rescue nil}.compact.flatten(1)].tap do |hash|
    if deep
      hash[:authors].map! &:to_h
      hash[:categories].map! &:to_h
      hash[:primary_category] = hash[:primary_category].to_h
    end
  end
end
to_json() click to toggle source

Serializes the {Paper} object into a valid JSON string.

@note Deep-serializes {Author} and {Category} objects. @return [String] The resulting JSON string.

# File lib/arx/entities/paper.rb, line 220
def to_json
  to_h(true).to_json
end
to_s() click to toggle source

A string representation of the {Paper} object.

@return [String]

# File lib/arx/entities/paper.rb, line 254
def to_s
  _id = id true
  _published_at = published_at.strftime("%Y-%m-%d")
  _authors = authors.map(&:name)
  _authors = [*_authors.first(2), '...'] if _authors.size > 2
  "Arx::Paper(id: #{_id}, published_at: #{_published_at}, authors: [#{_authors.join(', ')}], title: #{title})"
end
url(version = false) click to toggle source

The URL of the paper on the arXiv website.

@example

http://arxiv.org/abs/1705.01662v1
http://arxiv.org/abs/cond-mat/0211034

@param version [Boolean] Whether or not to include the paper's version. @return [String] The paper's arXiv URL.

# File lib/arx/entities/paper.rb, line 43
def url(version = false)
  "http://arxiv.org/abs/#{id version}"
end
version() click to toggle source

The version of the paper.

@return [Integer] The paper's version.

# File lib/arx/entities/paper.rb, line 50
def version
  Cleaner.extract_version @id
end