Class: Arx::Paper

Inherits:
Object
  • Object
show all
Includes:
Inspector, HappyMapper
Defined in:
lib/arx/entities/paper.rb

Overview

Entity/model representing an arXiv paper.

Constant Summary collapse

ATTRIBUTES =
Note:

#comment, #journal, #pdf_url and #doi_url may raise errors when called.

The attributes of an arXiv paper.

%i[
  id url version revision?
  title summary authors
  primary_category categories
  published_at updated_at
  comment? comment
  journal? journal
  pdf? pdf_url
  doi? doi_url
]

Instance Method Summary collapse

Methods included from Inspector

included, #inspect, inspected

Instance Method Details

#==(paper) ⇒ Boolean

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.

Equality check against another paper.

Parameters:

  • paper (Paper)

    The paper to compare against.

Returns:

  • (Boolean)


230
231
232
233
234
235
236
# File 'lib/arx/entities/paper.rb', line 230

def ==(paper)
  if paper.is_a? Paper
    id == paper.id
  else
    false
  end
end

#as_jsonHash

Note:

Deep-serializes Author and Category objects.

Serializes the Arx::Paper object into a valid JSON hash.

Returns:

  • (Hash)

    The resulting JSON hash.



212
213
214
# File 'lib/arx/entities/paper.rb', line 212

def as_json
  JSON.parse to_json
end

#authorsArray<Author>

The authors of the paper.

Returns:



84
# File 'lib/arx/entities/paper.rb', line 84

has_many :authors, Author, tag: 'author'

#categoriesArray<Category>

The categories of the paper.

Returns:



97
# File 'lib/arx/entities/paper.rb', line 97

has_many :categories, Category, tag: 'category'

#commentString

Note:

This is an optional metadata field on an arXiv paper. To check whether the paper has a comment, use #comment?

The comment of the paper.

Returns:

  • (String)

Raises:



117
# File 'lib/arx/entities/paper.rb', line 117

element :comment, Cleaner, parser: :clean, tag: 'comment'

#comment?Boolean

Whether or not the paper has a comment.

Returns:

  • (Boolean)


# File 'lib/arx/entities/paper.rb', line 106

#doi?Boolean

Whether or not the paper has a DOI (Digital Object Identifier) link.



# File 'lib/arx/entities/paper.rb', line 162

#doi_urlString

Note:

This is an optional metadata field on an arXiv paper. To check whether the paper has a DOI link, use #doi?

Link to the DOI (Digital Object Identifier) of the paper.

Returns:

  • (String)

Raises:

See Also:



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/arx/entities/paper.rb', line 178

%i[pdf doi].each do |link_type|
  exists = "#{link_type}?".to_sym

  define_method exists do
    links.any? &exists
  end

  define_method "#{link_type}_url" do
    if self.send exists
      links.find(&exists).href
    else
      raise Error::MissingLink.new id, link_type.to_s.upcase
    end
  end
end

#id(version = false) ⇒ String

Note:

The identifier of the paper.

Examples:

1705.01662v1
cond-mat/0211034

Parameters:

  • version (Boolean) (defaults to: false)

    Whether or not to include the paper’s version.

Returns:

  • (String)

    The paper’s identifier.



32
33
34
# File 'lib/arx/entities/paper.rb', line 32

def id(version = false)
  Cleaner.extract_id @id, version: version
end

#journalString

Note:

This is an optional metadata field on an arXiv paper. To check whether the paper has a journal reference, use #journal?

The journal reference of the paper.

Returns:

  • (String)

Raises:



130
# File 'lib/arx/entities/paper.rb', line 130

element :journal, Cleaner, parser: :clean, tag: 'journal_ref'

#journal?Boolean

Whether or not the paper has a journal reference.

Returns:

  • (Boolean)


# File 'lib/arx/entities/paper.rb', line 119

#pdf?Boolean

Whether or not the paper has a PDF link.

Returns:

  • (Boolean)


# File 'lib/arx/entities/paper.rb', line 150

#pdf_urlString

Note:

This is an optional metadata field on an arXiv paper. To check whether the paper has a PDF link, use #pdf?

Link to the PDF version of the paper.

Returns:

  • (String)

Raises:



# File 'lib/arx/entities/paper.rb', line 155

#primary_categoryCategory Also known as: category

The primary category of the paper.

Returns:



90
# File 'lib/arx/entities/paper.rb', line 90

element :primary_category, Category, tag: 'primary_category'

#published_atDateTime

The original publish/submission date of the paper.

Returns:

  • (DateTime)


72
# File 'lib/arx/entities/paper.rb', line 72

element :published_at, DateTime, tag: 'published'

#revision?Boolean

Note:

A paper is a revision if its #version is greater than 1.

Whether the paper is a revision or not.

Returns:

  • (Boolean)


58
59
60
# File 'lib/arx/entities/paper.rb', line 58

def revision?
  version > 1
end

#save(path) ⇒ Object

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

Parameters:

  • path (String)

    The file path to store the PDF at.



241
242
243
244
245
246
247
248
249
# 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

#summaryString Also known as: abstract

The summary (or abstract) of the paper.

Returns:

  • (String)


103
# File 'lib/arx/entities/paper.rb', line 103

element :summary, Cleaner, parser: :clean, tag: 'summary'

#titleDateTime

The title of the paper.

Returns:

  • (DateTime)


78
# File 'lib/arx/entities/paper.rb', line 78

element :title, Cleaner, parser: :clean, tag: 'title'

#to_h(deep = false) ⇒ Hash

Serializes the Arx::Paper object into a Hash.

Parameters:

  • deep (Boolean) (defaults to: false)

    Whether to deep-serialize Author and Category objects.

Returns:

  • (Hash)


198
199
200
201
202
203
204
205
206
# 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_jsonString

Note:

Deep-serializes Author and Category objects.

Serializes the Arx::Paper object into a valid JSON string.

Returns:

  • (String)

    The resulting JSON string.



220
221
222
# File 'lib/arx/entities/paper.rb', line 220

def to_json
  to_h(true).to_json
end

#to_sString

A string representation of the Arx::Paper object.

Returns:

  • (String)


254
255
256
257
258
259
260
# 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

#updated_atDateTime

The date that the paper was last updated.

Returns:

  • (DateTime)


66
# File 'lib/arx/entities/paper.rb', line 66

element :updated_at, DateTime, tag: 'updated'

#url(version = false) ⇒ String

The URL of the paper on the arXiv website.

Examples:

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

Parameters:

  • version (Boolean) (defaults to: false)

    Whether or not to include the paper’s version.

Returns:

  • (String)

    The paper’s arXiv URL.



43
44
45
# File 'lib/arx/entities/paper.rb', line 43

def url(version = false)
  "http://arxiv.org/abs/#{id version}"
end

#versionInteger

The version of the paper.

Returns:

  • (Integer)

    The paper’s version.



50
51
52
# File 'lib/arx/entities/paper.rb', line 50

def version
  Cleaner.extract_version @id
end