class DOI

Constants

CrossRefURL
HTTPURI
InfoURI
JSON_Type
UAString
VERSION

Attributes

dir[R]
dss[R]
reg[R]

Public Class Methods

data(doi) click to toggle source

Gets data about a DOI from CrossRef.

# File lib/libdoi/network.rb, line 49
def data doi
  doi = parse(doi) unless doi.is_a? DOI
  doi.data
end
find(doi) click to toggle source

Looks for a DOI at doi.org.

Returns a URI if it finds a match, otherwise returns nil.

# File lib/libdoi/network.rb, line 41
def find doi
  doi = parse(doi) unless doi.is_a? DOI
  doi.find
end
parse(str) click to toggle source

Parses the given string as a DOI.

Raises an ArgumentError if parsing fails.

# File lib/libdoi.rb, line 62
def parse str
  str = "#{str}"
  if str =~ %r[^https?://(?:(?:dx\.)?doi\.org|doi\.acm\.org|doi\.ieeecomputersociety\.org)/+(?:doi:)?(.*)]i
    # It looks like a HTTP proxy URL.
    doi = CGI.unescape $1
  elsif str =~ %r[^info:doi/(.*)]i
    # It looks like an info URI.
    doi = CGI.unescape $1
  else
    # It's probably a DOI string.
    doi = str.sub %r[^doi:\s*]i, ''
  end

  # ANSI/NISO Z39.84-2005
  # <http://www.niso.org/apps/group_public/download.php/6587/Syntax%20for%20the%20Digital%20Object%20Identifier.pdf>
  if doi =~ %r[^(10)\.([^/]+)/(\p{Graph}(?:[^/]\p{Graph}*)?)$]
    # FIXME: $2 and $3 may contain characters outside of /\p{Graph}/
    new $1, $2, $3
  else
    raise ArgumentError, "'#{str}' is not a valid DOI string";
  end
end

Public Instance Methods

+(other) click to toggle source

Concatenation–Returns a new DOI containing other concatenated to this DOI's suffix string.

# File lib/libdoi.rb, line 20
def + other
  self.class.new @dir, @reg, @dss + other.to_s
end
<<(other) click to toggle source

Append–Concatenates the given object to this DOI's suffix string.

# File lib/libdoi.rb, line 27
def << other
  @dss << other.to_s
  self
end
data() click to toggle source

Gets data about this DOI from CrossRef.

# File lib/libdoi/network.rb, line 27
def data
  uri = URI(_data_url)
  _http_get(uri, 'Accept'=>JSON_Type) do |response|
    return JSON.parse(response.body) if response.code.to_i == 200
  end
  nil
end
find() click to toggle source

Looks for a DOI at doi.org.

Returns a URI if it finds a match, otherwise returns nil.

# File lib/libdoi/network.rb, line 15
def find
  _http_get(self.to_uri) do |response|
    # FIXME: this is both presumptuous and intolerant
    loc = response['Location']
    return URI(loc) if loc
  end
  nil
end
to_s(prefix: true) click to toggle source

Returns a String that represents this DOI.

  • prefix: Prepends 'doi:' to the returned string.

# File lib/libdoi.rb, line 37
def to_s prefix: true
  (prefix ? 'doi:' : '') + "#{@dir}.#{@reg}/#{@dss}"
end
to_uri(info: false) click to toggle source

Returns a URI.

For example: “doi.org/10.1000/foo%23bar

  • info: Returns an 'info:' URI instead of 'https:'

# File lib/libdoi.rb, line 48
def to_uri info: false
  if info
    URI(_info_uri)
  else
    URI(_http_url)
  end
end

Private Instance Methods

_data_url() click to toggle source

Returns a data.crossref.org URI string.

# File lib/libdoi/network.rb, line 74
def _data_url
  CrossRefURL + _uri_path
end
_http_get(uri, opts={}) click to toggle source
# File lib/libdoi/network.rb, line 61
def _http_get uri, opts={}, &block
  Net::HTTP.start(uri.host, uri.port,
                  :use_ssl=>uri.is_a?(URI::HTTPS),
                 ) do |http|
    query = Net::HTTP::Get.new uri, 'Accept-Encoding'=>''
    query['Connection'] = 'close'
    query['User-Agent'] = UAString
    opts.each_pair {|k,v| query[k] = v }
    http.request query, &block
  end
end
_http_url() click to toggle source

Returns a “doi.org/…” URI string.

# File lib/libdoi.rb, line 102
def _http_url
  HTTPURI + _uri_path
end
_info_uri() click to toggle source

Returns an “info:doi/…” URI string.

# File lib/libdoi.rb, line 97
def _info_uri
  InfoURI + _uri_path
end
_uri_path() click to toggle source

Returns a percent-encoded “dir.reg/dss” string.

# File lib/libdoi.rb, line 92
def _uri_path
  "#{@dir}.#{CGI.escape @reg}/#{CGI.escape @dss}"
end