Class: DOI
- Inherits:
-
Object
- Object
- DOI
- Defined in:
- lib/libdoi.rb,
lib/libdoi/network.rb
Constant Summary collapse
- VERSION =
'1.0.0'
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#dss ⇒ Object
readonly
Returns the value of attribute dss.
-
#reg ⇒ Object
readonly
Returns the value of attribute reg.
Class Method Summary collapse
-
.data(doi) ⇒ Object
Gets data about a DOI from CrossRef.
-
.find(doi) ⇒ Object
Looks for a DOI at doi.org.
-
.parse(str) ⇒ Object
Parses the given string as a DOI.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Concatenation–Returns a new DOI containing
otherconcatenated to this DOI’s suffix string. -
#<<(other) ⇒ Object
Append–Concatenates the given object to this DOI’s suffix string.
-
#data ⇒ Object
Gets data about this DOI from CrossRef.
-
#find ⇒ Object
Looks for a DOI at doi.org.
-
#initialize(dir, reg, dss) ⇒ DOI
constructor
:nodoc:.
-
#to_s(prefix: true) ⇒ Object
Returns a String that represents this DOI.
-
#to_uri(info: false) ⇒ Object
Returns a URI.
Constructor Details
#initialize(dir, reg, dss) ⇒ DOI
:nodoc:
10 11 12 13 14 |
# File 'lib/libdoi.rb', line 10 def initialize dir, reg, dss #:nodoc: @dir = dir @reg = reg @dss = dss end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
15 16 17 |
# File 'lib/libdoi.rb', line 15 def dir @dir end |
#dss ⇒ Object (readonly)
Returns the value of attribute dss.
15 16 17 |
# File 'lib/libdoi.rb', line 15 def dss @dss end |
#reg ⇒ Object (readonly)
Returns the value of attribute reg.
15 16 17 |
# File 'lib/libdoi.rb', line 15 def reg @reg end |
Class Method Details
.data(doi) ⇒ Object
Gets data about a DOI from CrossRef.
49 50 51 52 |
# File 'lib/libdoi/network.rb', line 49 def data doi doi = parse(doi) unless doi.is_a? DOI doi.data end |
.find(doi) ⇒ Object
Looks for a DOI at doi.org.
Returns a URI if it finds a match, otherwise returns nil.
41 42 43 44 |
# File 'lib/libdoi/network.rb', line 41 def find doi doi = parse(doi) unless doi.is_a? DOI doi.find end |
.parse(str) ⇒ Object
Parses the given string as a DOI.
Raises an ArgumentError if parsing fails.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# 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 |
Instance Method Details
#+(other) ⇒ Object
Concatenation–Returns a new DOI containing other concatenated to this DOI’s suffix string.
20 21 22 |
# File 'lib/libdoi.rb', line 20 def + other self.class.new @dir, @reg, @dss + other.to_s end |
#<<(other) ⇒ Object
Append–Concatenates the given object to this DOI’s suffix string.
27 28 29 30 |
# File 'lib/libdoi.rb', line 27 def << other @dss << other.to_s self end |
#data ⇒ Object
Gets data about this DOI from CrossRef.
27 28 29 30 31 32 33 |
# 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 ⇒ Object
Looks for a DOI at doi.org.
Returns a URI if it finds a match, otherwise returns nil.
15 16 17 18 19 20 21 22 |
# 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) ⇒ Object
Returns a String that represents this DOI.
-
prefix: Prepends ‘doi:’ to the returned string.
37 38 39 |
# File 'lib/libdoi.rb', line 37 def to_s prefix: true (prefix ? 'doi:' : '') + "#{@dir}.#{@reg}/#{@dss}" end |
#to_uri(info: false) ⇒ Object
Returns a URI.
For example: “doi.org/10.1000/foo%23bar”
-
info: Returns an ‘info:’ URI instead of ‘https:’
48 49 50 51 52 53 54 |
# File 'lib/libdoi.rb', line 48 def to_uri info: false if info URI(_info_uri) else URI(_http_url) end end |