Class: Relaton::Bibdata

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

Constant Summary collapse

ATTRIBS =
i[
  docidentifier
  doctype
  title
  stage
  relation
  xml
  pdf
  doc
  html
  uri
  rxl
  revdate
  abstract
  technical_committee
  copyright_from
  copyright_owner
  contributor_author_role
  contributor_author_organization
  contributor_publisher_role
  contributor_publisher_organization
  language
  script
  edition
  datetype
]
FILENAME_BAD_CHARS =
[ '/', '\\', '?', '%', '*', ':', '|', '"', '<', '>', '.', ' ' ]
DOC_NUMBER_REGEX =
/([\w\/]+)\s+(\d+):?(\d*)/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Bibdata

Returns a new instance of Bibdata.



34
35
36
37
38
39
# File 'lib/relaton/bibdata.rb', line 34

def initialize(options)
  options.each_pair do |k,v|
    send("#{k.to_s}=", v)
  end
  self
end

Class Method Details

.from_xml(source) ⇒ Object



56
57
58
# File 'lib/relaton/bibdata.rb', line 56

def self.from_xml(source)
  new(Relaton::XmlDocument.parse(source))
end

Instance Method Details

#doc_numberObject



52
53
54
# File 'lib/relaton/bibdata.rb', line 52

def doc_number
  docidentifier&.match(DOC_NUMBER_REGEX) ? $2.to_i : 999999
end

#docidentifier_codeObject



44
45
46
47
48
49
# File 'lib/relaton/bibdata.rb', line 44

def docidentifier_code
  return "" if docidentifier.nil?
  a = FILENAME_BAD_CHARS.inject(docidentifier.downcase) do |result, bad_char|
    result.gsub(bad_char, '-')
  end
end

#to_hObject



110
111
112
113
114
115
116
# File 'lib/relaton/bibdata.rb', line 110

def to_h
  ATTRIBS.inject({}) do |acc, k|
    value = send(k)
    acc[k.to_s] = value unless value.nil?
    acc
  end
end

#to_xmlObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/relaton/bibdata.rb', line 60

def to_xml
  #datetype = stage&.casecmp("published") == 0 ? "published" : "circulated"

  ret = "<bibdata type='#{doctype}'>\n"
  ret += "<fetched>#{Date.today.to_s}</fetched>\n"
  ret += "<title>#{title}</title>\n"
  ret += "<docidentifier>#{docidentifier}</docidentifier>\n" if docidentifier
  ret += "<uri>#{uri}</uri>\n" if uri
  ret += "<uri type='xml'>#{xml}</uri>\n" if xml
  ret += "<uri type='html'>#{html}</uri>\n" if html
  ret += "<uri type='pdf'>#{pdf}</uri>\n" if pdf
  ret += "<uri type='doc'>#{doc}</uri>\n" if doc
  ret += "<uri type='rxl'>#{rxl}</uri>\n" if rxl

  ret += "<language>#{language}</language>\n"
  ret += "<script>#{script}</script>\n"

  if copyright_from
    ret += "<copyright>"
    ret += "<from>#{copyright_from}</from>\n" if copyright_from
    ret += "<owner><organization><name>#{copyright_owner}</name></organization></owner>\n" if copyright_owner
    ret += "</copyright>"
  end

  if contributor_author_role
    ret += "<contributor>\n"
    ret += "<role type='author'/>\n"
    ret += "<organization><name>#{contributor_author_organization}</name></organization>\n"
    ret += "</contributor>\n"
  end

  if contributor_publisher_role
    ret += "<contributor>\n"
    ret += "<role type='publisher'/>\n"
    ret += "<organization><name>#{contributor_publisher_organization}</name></organization>\n"
    ret += "</contributor>\n"
  end

  ret += "<date type='#{datetype}'><on>#{revdate}</on></date>\n" if revdate
  # ret += "<contributor><role type='author'/><organization><name>#{agency}</name></organization></contributor>" if agency
  # ret += "<contributor><role type='publisher'/><organization><name>#{agency}</name></organization></contributor>" if agency
  ret += "<edition>#{edition}</edition>\n" if edition
  ret += "<language>#{language}</language>\n" if language
  ret += "<script>#{script}</script>\n" if script
  ret += "<abstract>#{abstract}</abstract>\n" if abstract
  ret += "<status>#{stage}</status>\n" if stage
  ret += "<editorialgroup><technical-committee>#{technical_committee}</technical-committee></editorialgroup>\n" if technical_committee
  ret += "</bibdata>\n"
end

#to_yamlObject



118
119
120
# File 'lib/relaton/bibdata.rb', line 118

def to_yaml
  to_h.to_yaml
end