Skip to content

Commit

Permalink
Add .yardopts + namespace errors + move regexes (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
eonu authored Mar 27, 2019
1 parent 439d4a4 commit 3f2521e
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 55 deletions.
4 changes: 4 additions & 0 deletions .yardopts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--no-private
-
CHANGELOG.md
LICENSE
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,13 @@ paper.revision?
paper.comment?
#=> false
paper.comment
#=> Arx::MissingFieldError (This arXiv paper is missing the `comment` field)
#=> Arx::Error::MissingField (This arXiv paper is missing the `comment` field)

# Paper's journal reference
paper.journal?
#=> false
paper.journal
#=> Arx::MissingFieldError (This arXiv paper is missing the `journal` field)
#=> Arx::Error::MissingField (This arXiv paper is missing the `journal` field)

# Paper's PDF URL
paper.pdf?
Expand Down
24 changes: 22 additions & 2 deletions lib/arx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
require 'arx/cleaner'
require 'arx/inspector'
require 'arx/categories'
require 'arx/exceptions'
require 'arx/error'
require 'arx/query/validate'
require 'arx/query/query'
require 'arx/entities/author'
Expand All @@ -22,6 +22,26 @@ module Arx
# The arXiv search API endpoint.
ENDPOINT = 'http://export.arxiv.org/api/query?'

# The current arxiv paper identifier scheme (1 April 2007 and onwards).
# The last block of digits can either be five digits (if the paper was published after 1501 - January 2015),
# or four digits (if the paper was published before 1501).
#
# @see https://arxiv.org/help/arxiv_identifier#new arXiv identifier (new)
# @example
# 1501.00001
# 1705.01662v1
# 1412.0135
# 0706.0001v2
NEW_IDENTIFIER_FORMAT = %r"^\d{4}\.\d{4,5}(v\d+)?$"

# The legacy arXiv paper identifier scheme (before 1 April 2007).
#
# @see https://arxiv.org/help/arxiv_identifier#old arXiv identifier (old)
# @example
# math/0309136v1
# cond-mat/0211034
OLD_IDENTIFIER_FORMAT = %r"^[a-z]+(\-[a-z]+)?\/\d{7}(v\d+)?$"

class << self

# Performs a search query for papers on the arXiv search API.
Expand All @@ -43,7 +63,7 @@ def search(*ids, query: nil, sort_by: :relevance, sort_order: :descending)
document = Nokogiri::XML(open ENDPOINT + query.to_s + '&max_results=10000').remove_namespaces!

results = Paper.parse(document, single: false).reject {|paper| paper.id.empty?}
raise MissingPaper.new(ids.first) if results.empty? && ids.size == 1
raise Error::MissingPaper.new(ids.first) if results.empty? && ids.size == 1
ids.size == 1 && results.size == 1 ? results.first : results
end

Expand Down
1 change: 1 addition & 0 deletions lib/arx/cleaner.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Arx

# Class for cleaning strings.
# @private
class Cleaner

# Cleans strings.
Expand Down
1 change: 1 addition & 0 deletions lib/arx/entities/link.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Arx

# Helper entity/model representing a link on an arXiv paper.
# @private
class Link
include HappyMapper

Expand Down
14 changes: 7 additions & 7 deletions lib/arx/entities/paper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Paper

element :id, Cleaner, parser: :clean, tag: 'id'
# The identifier of the paper.
# @note This is either in {Validate::OLD_IDENTIFIER_FORMAT} or {Validate::NEW_IDENTIFIER_FORMAT}.
# @note This is either in {OLD_IDENTIFIER_FORMAT} or {NEW_IDENTIFIER_FORMAT}.
# @example
# 1705.01662v1
# cond-mat/0211034
Expand Down Expand Up @@ -81,7 +81,7 @@ def revision?
# @!method comment
# The comment of the paper.
# @note This is an optional metadata field on an arXiv paper. To check whether the paper has a comment, use {comment?}
# @raise {MissingFieldError} If the paper does not have a comment.
# @raise {Error::MissingField} If the paper does not have a comment.
# @return [String]
element :comment, Cleaner, parser: :clean, tag: 'comment'

Expand All @@ -92,7 +92,7 @@ def revision?
# @!method journal
# The journal reference of the paper.
# @note This is an optional metadata field on an arXiv paper. To check whether the paper has a journal reference, use {journal?}
# @raise {MissingFieldError} If the paper does not have a journal reference.
# @raise {Error::MissingField} If the paper does not have a journal reference.
# @return [String]
element :journal, Cleaner, parser: :clean, tag: 'journal_ref'

Expand All @@ -107,7 +107,7 @@ def revision?
if self.send "#{optional}?"
instance_variable_get("@#{optional}")
else
raise MissingFieldError.new(optional)
raise Error::MissingField.new(optional)
end
end
end
Expand All @@ -121,7 +121,7 @@ def revision?
# @!method pdf_url
# Link to the PDF version of the paper.
# @note This is an optional metadata field on an arXiv paper. To check whether the paper has a PDF link, use {pdf?}
# @raise {MissingLinkError} If the paper does not have a PDF link.
# @raise {Error::MissingLink} If the paper does not have a PDF link.
# @return [String]

# @!method doi?
Expand All @@ -135,7 +135,7 @@ def revision?
# @see https://arxiv.org/help/jref#doi
# @see https://arxiv.org/help/prep#doi
# @note This is an optional metadata field on an arXiv paper. To check whether the paper has a DOI link, use {doi?}
# @raise {MissingLinkError} If the paper does not have a DOI link.
# @raise {Error::MissingLink} If the paper does not have a DOI link.
# @return [String]

%i[pdf doi].each do |link_type|
Expand All @@ -149,7 +149,7 @@ def revision?
if self.send exists
links.find(&exists).href
else
raise MissingLinkError.new link_type.to_s.upcase
raise Error::MissingLink.new link_type.to_s.upcase
end
end
end
Expand Down
27 changes: 27 additions & 0 deletions lib/arx/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Arx

# Various arXiv-related errors.
module Error

# Custom error for missing links on an arXiv paper.
class MissingLink < StandardError
def initialize(link_type)
super "This arXiv paper does not have a #{link_type} link"
end
end

# Custom error for missing fields on an arXiv paper.
class MissingField < StandardError
def initialize(field)
super "This arXiv paper is missing the `#{field}` field"
end
end

# Custom error for missing arXiv papers.
class MissingPaper < StandardError
def initialize(id)
super "Couldn't find an arXiv paper with ID: #{id}"
end
end
end
end
23 changes: 0 additions & 23 deletions lib/arx/exceptions.rb

This file was deleted.

1 change: 1 addition & 0 deletions lib/arx/inspector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Arx

# Restricts +inspect+ to dump a whitelist of methods on an object.
# It will always provide `object_id` at a minimum.
# @private
module Inspector

# Overwrites the object's own inspect method.
Expand Down
22 changes: 1 addition & 21 deletions lib/arx/query/validate.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
module Arx

# Validations for arXiv search query fields and identifier schemes.
# @private
module Validate

# The current arxiv paper identifier scheme (1 April 2007 and onwards).
# The last block of digits can either be five digits (if the paper was published after 1501 - January 2015),
# or four digits (if the paper was published before 1501).
#
# @see https://arxiv.org/help/arxiv_identifier#new arXiv identifier (new)
# @example
# 1501.00001
# 1705.01662v1
# 1412.0135
# 0706.0001v2
NEW_IDENTIFIER_FORMAT = %r"^\d{4}\.\d{4,5}(v\d+)?$"

# The legacy arXiv paper identifier scheme (before 1 April 2007).
#
# @see https://arxiv.org/help/arxiv_identifier#old arXiv identifier (old)
# @example
# math/0309136v1
# cond-mat/0211034
OLD_IDENTIFIER_FORMAT = %r"^[a-z]+(\-[a-z]+)?\/\d{7}(v\d+)?$"

class << self
# Validates the +sortBy+ field of the query string.
#
Expand Down

0 comments on commit 3f2521e

Please sign in to comment.