Class: Crossref::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/crossref.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Metadata

Returns a new instance of Metadata.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/crossref.rb', line 11

def initialize(opts = {})
  @base_url =  opts[:base_url] || 'http://crossref.org/openurl/?noredirect=true&format=unixref'      
  @doi = opts[:doi]
  @pid = opts[:pid]
  @base_url += '&pid=' + @pid if @pid
  
  if @doi
    @doi = sanitize_doi(@doi)
    @url = @base_url + "&id=doi:" + @doi
    @xml = get_xml(@url)
  end
end

Instance Attribute Details

#doi(doi) ⇒ Object

Returns the value of attribute doi.



9
10
11
# File 'lib/crossref.rb', line 9

def doi
  @doi
end

#urlObject

Returns the value of attribute url.



9
10
11
# File 'lib/crossref.rb', line 9

def url
  @url
end

#xmlObject

Returns the value of attribute xml.



9
10
11
# File 'lib/crossref.rb', line 9

def xml
  @xml
end

Instance Method Details

#authorsObject



44
45
46
47
48
49
50
# File 'lib/crossref.rb', line 44

def authors
  authors = []
  xpath_ns('contributors/person_name[@contributor_role="author"]').each do |a| 
   authors << hashify_nodes(a.children) 
  end
  authors
end

#journalObject



61
62
63
64
65
66
67
68
69
# File 'lib/crossref.rb', line 61

def journal
  journal = hashify_nodes(xpath_ns('journal_metadata').first.children)
  journal[:volume] = xpath_first('journal_issue/journal_volume/volume') 
  journal[:issue] = xpath_first('journal_issue/issue')
  journal[:first_page] = xpath_first('first_page')
  journal[:last_page] = xpath_first('last_page')

  journal
end

#publishedObject



53
54
55
56
57
58
# File 'lib/crossref.rb', line 53

def published
  pub = Hash.new
  pub[:year] = xpath_first('publication_date/year')
  pub[:month] = xpath_first('publication_date/month')
  pub
end

#resourceObject



71
72
73
# File 'lib/crossref.rb', line 71

def resource
  xpath_first('doi_data/resource')
end

#result?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
# File 'lib/crossref.rb', line 30

def result?
  if self.xml.nil? || xpath_ns('error').size == 1
    false
  else
    xpath_ns('doi_record').size == 1
  end
end

#titleObject



39
40
41
# File 'lib/crossref.rb', line 39

def title
  xpath_first('titles/title')
end

#xpath_first(q) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/crossref.rb', line 79

def xpath_first(q)
  if info = xpath_ns(q).first
    info.content
  else
    nil
  end
end

#xpath_ns(q, ns = 'http://www.crossref.org/xschema/1.0') ⇒ Object



75
76
77
# File 'lib/crossref.rb', line 75

def xpath_ns(q, ns = 'http://www.crossref.org/xschema/1.0')
  self.xml.xpath("//#{q.split('/').map {|e| "xmlns:#{e}"}.join('/')}", 'xmlns' => ns)
end