class DOI
Constants
- CrossRefURL
- HTTPURI
- InfoURI
- JSON_Type
- UAString
- VERSION
Attributes
Public Class Methods
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
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
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
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
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
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
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
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
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
Returns a data.crossref.org URI string.
# File lib/libdoi/network.rb, line 74 def _data_url CrossRefURL + _uri_path end
# 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
Returns a “doi.org/…” URI string.
# File lib/libdoi.rb, line 102 def _http_url HTTPURI + _uri_path end
Returns an “info:doi/…” URI string.
# File lib/libdoi.rb, line 97 def _info_uri InfoURI + _uri_path end
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