relaton-bib 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ module RelatonBib
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,289 @@
1
+ require "nokogiri"
2
+
3
+ module RelatonBib
4
+ class XMLParser
5
+ class << self
6
+ def from_xml(xml)
7
+ doc = Nokogiri::XML(xml)
8
+ bibitem = doc.at "/bibitem"
9
+ BibliographicItem.new(
10
+ id: bibitem[:id],
11
+ type: bibitem[:type],
12
+ fetched: bibitem.at("./fetched")&.text,
13
+ titles: fetch_titles(bibitem),
14
+ link: fetch_link(bibitem),
15
+ docid: fetch_docid(bibitem),
16
+ docnumber: bibitem.at("./docnumber")&.text,
17
+ dates: fetch_dates(bibitem),
18
+ contributors: fetch_contributors(bibitem),
19
+ edition: bibitem.at("./edition")&.text,
20
+ version: fetch_version(bibitem),
21
+ biblionote: fetch_note(bibitem),
22
+ language: bibitem.xpath("./language").map(&:text),
23
+ script: bibitem.xpath("./script").map(&:text),
24
+ abstract: fetch_abstract(bibitem),
25
+ docstatus: fetch_status(bibitem),
26
+ copyright: fetch_copyright(bibitem),
27
+ relations: fetch_relations(bibitem),
28
+ series: fetch_series(bibitem),
29
+ medium: fetch_medium(bibitem),
30
+ place: bibitem.xpath("./place").map(&:text),
31
+ extent: fetch_extent(bibitem),
32
+ accesslocation: bibitem.xpath("./accesslocation").map(&:text),
33
+ classification: fetch_classification(bibitem),
34
+ validity: fetch_validity(bibitem),
35
+ )
36
+ end
37
+
38
+ private
39
+
40
+ def fetch_version(item)
41
+ version = item.at "./version"
42
+ return unless version
43
+
44
+ revision_date = version.at("revision_date").text
45
+ draft = version.xpath("draft").map &:text
46
+ RelatonBib::BibliographicItem::Version.new revision_date, draft
47
+ end
48
+
49
+ def fetch_note(item)
50
+ item.xpath("./note").map do |n|
51
+ FormattedString.new(
52
+ content: n.text, format: n[:format], language: n[:language], script: n[:script]
53
+ )
54
+ end
55
+ end
56
+
57
+ def fetch_series(item)
58
+ item.xpath("./series").map do |sr|
59
+ abbr = sr.at "abbreviation"
60
+ abbreviation = abbr ? LocalizedString.new(abbr.text, abbr[:language], abbr[:script]) : nil
61
+ Series.new(type: sr[:type], formattedref: fref(sr),
62
+ title: ttitle(sr.at "title"), place: sr.at("place")&.text,
63
+ organization: sr.at("organization")&.text, abbreviation: abbreviation,
64
+ from: sr.at("from")&.text, to: sr.at("to")&.text, number: sr.at("number")&.text,
65
+ partnumber: sr.at("partnumber")&.text
66
+ )
67
+ end
68
+ end
69
+
70
+ def fetch_medium(item)
71
+ medium = item.at("./medium")
72
+ return unless medium
73
+
74
+ Medium.new(
75
+ form: medium.at("form")&.text, size: medium.at("size")&.text,
76
+ scale: medium.at("scale")&.text
77
+ )
78
+ end
79
+
80
+ def fetch_extent(item)
81
+ item.xpath("./locality").map do |ex|
82
+ BibItemLocality.new(
83
+ ex[:type], ex.at("referenceFrom")&.text, ex.at("referenceTo")&.text
84
+ )
85
+ end
86
+ end
87
+
88
+ def fetch_classification(item)
89
+ cls = item.at "classification"
90
+ return unless cls
91
+
92
+ Classification.new type: cls[:type], value: cls.text
93
+ end
94
+
95
+ def fetch_validity(item)
96
+ vl = item.at("./validity")
97
+ return unless vl
98
+
99
+ begins = (b = vl.at("validityBegins")) ? Time.strptime(b.text, "%Y-%m-%d %H:%M") : nil
100
+ ends = (e = vl.at("validityEnds")) ? Time.strptime(e.text, "%Y-%m-%d %H:%M") : nil
101
+ revision = (r = vl.at("validityRevision")) ? Time.strptime(r.text, "%Y-%m-%d %H:%M") : nil
102
+ Validity.new begins: begins, ends: ends, revision: revision
103
+ end
104
+
105
+ # def get_id(did)
106
+ # did.text.match(/^(?<project>.*?\d+)(?<hyphen>-)?(?(<hyphen>)(?<part>\d*))/)
107
+ # end
108
+
109
+ def fetch_docid(item)
110
+ ret = []
111
+ item.xpath("./docidentifier").each do |did|
112
+ #did = doc.at('/bibitem/docidentifier')
113
+ # type = did.at("./@type")
114
+ # if did.text == "IEV" then
115
+ # ret << DocumentIdentifier.new(id: "IEV", nil)
116
+ # else
117
+ # id = get_id did
118
+ ret << DocumentIdentifier.new(id: did.text, type: did[:type])
119
+ # end
120
+ end
121
+ ret
122
+ end
123
+
124
+ def fetch_titles(item)
125
+ item.xpath("./title").map { |t| ttitle t }
126
+ end
127
+
128
+ def ttitle(title)
129
+ return unless title
130
+
131
+ TypedTitleString.new(
132
+ type: title[:type], content: title.text, language: title[:language],
133
+ script: title[:script], format: title[:format]
134
+ )
135
+ end
136
+
137
+ def fetch_status(item)
138
+ status = item.at("./status")
139
+ return unless status
140
+
141
+ DocumentStatus.new(
142
+ LocalizedString.new status.text, status[:language], status[:script]
143
+ )
144
+ end
145
+
146
+ def fetch_dates(item)
147
+ item.xpath('./date').map do |d|
148
+ RelatonBib::BibliographicDate.new(
149
+ type: d[:type], on: d.at('on')&.text, from: d.at('from')&.text,
150
+ to: d.at('to')&.text
151
+ )
152
+ end
153
+ end
154
+
155
+ def get_org(org)
156
+ names = org.xpath("name").map do |n|
157
+ { content: n.text, language: n[:language], script: n[:script] }
158
+ end
159
+ identifiers = org.xpath("./identifier").map do |i|
160
+ OrgIdentifier.new(i[:type], i.text)
161
+ end
162
+ Organization.new(name: names,
163
+ abbreviation: org.at("abbreviation")&.text,
164
+ subdivision: org.at("subdivision")&.text,
165
+ url: org.at("uri")&.text,
166
+ identifiers: identifiers)
167
+ end
168
+
169
+ def get_person(person)
170
+ affilations = person.xpath("./affiliation").map do |a|
171
+ org = a.at "./organization"
172
+ Affilation.new get_org(org)
173
+ end
174
+
175
+ contacts = person.xpath("./address | ./phone | ./email | ./uri").map do |contact|
176
+ if contact.name == "address"
177
+ streets = contact.xpath("./street").map(&:text)
178
+ Address.new(
179
+ street: streets,
180
+ city: contact.at("./city").text,
181
+ state: contact.at("./state").text,
182
+ country: contact.at("./country").text,
183
+ postcode: contact.at("./postcode").text,
184
+ )
185
+ else
186
+ Contact.new(type: contact.name, value: contact.text)
187
+ end
188
+ end
189
+
190
+ identifiers = person.xpath("./identifier").map do |pi|
191
+ PersonIdentifier.new pi[:type], pi.text
192
+ end
193
+
194
+ cname = person.at "./name/completename"
195
+ completename = cname ? LocalizedString.new(cname.text, cname[:language]) : nil
196
+
197
+ sname = person.at "./name/surname"
198
+ surname = sname ? LocalizedString.new(sname.text, sname[:language]) : nil
199
+
200
+ name = FullName.new(
201
+ completename: completename, surname: surname,
202
+ initials: name_part(person, "initial"), forenames: name_part(person, "forename"),
203
+ additions: name_part(person, "addidtion"), prefix: name_part(person, "prefix")
204
+ )
205
+
206
+ Person.new(
207
+ name: name,
208
+ affiliation: affilations,
209
+ contacts: contacts,
210
+ identifiers: identifiers,
211
+ )
212
+ end
213
+
214
+ def name_part(person, part)
215
+ additions = person.xpath("./name/#{part}").map do |np|
216
+ LocalizedString.new np.text, np[:language]
217
+ end
218
+ end
219
+
220
+ def fetch_contributors(item)
221
+ item.xpath("./contributor").map do |c|
222
+ entity = if (org = c.at "./organization") then get_org(org)
223
+ elsif (person = c.at "./person") then get_person(person)
224
+ end
225
+ role_descr = c.xpath("./role/description").map &:text
226
+ ContributionInfo.new entity: entity, role: [[c.at("role")[:type], role_descr]]
227
+ end
228
+ end
229
+
230
+ def fetch_abstract(item)
231
+ item.xpath("./abstract").map do |a|
232
+ FormattedString.new(
233
+ content: a.text, language: a[:language], script: a[:script], format: a[:format]
234
+ )
235
+ end
236
+ end
237
+
238
+ def fetch_copyright(item)
239
+ cp = item.at("./copyright") || return
240
+ org = cp&.at("owner/organization")
241
+ name = org&.at("name").text
242
+ abbr = org&.at("abbreviation")&.text
243
+ url = org&.at("uri")&.text
244
+ entity = Organization.new(name: name, abbreviation: abbr, url: url)
245
+ from = cp.at("from")&.text
246
+ to = cp.at("to")&.text
247
+ owner = ContributionInfo.new entity: entity
248
+ CopyrightAssociation.new(owner: owner, from: from, to: to)
249
+ end
250
+
251
+ def fetch_link(item)
252
+ item.xpath("./uri").map do |l|
253
+ TypedUri.new type: l[:type], content: l.text
254
+ end
255
+ end
256
+
257
+ def fetch_relations(item)
258
+ item.xpath("./relation").map do |rel|
259
+ localities = rel.xpath("./locality").map do |l|
260
+ ref_to = (rt = l.at("./referenceTo")) ? LocalizedString.new(rt.text) : nil
261
+ BibItemLocality.new(
262
+ l[:type],
263
+ LocalizedString.new(l.at("./referenceFrom").text),
264
+ ref_to
265
+ )
266
+ end
267
+ bibitem = rel&.at "./bibitem"
268
+ DocumentRelation.new(
269
+ type: rel[:type],
270
+ bibitem: BibliographicItem.new(
271
+ type: bibitem[:type], formattedref: fref(bibitem), docstatus: fetch_status(bibitem)
272
+ ),
273
+ bib_locality: localities,
274
+ )
275
+ end
276
+ end
277
+
278
+ def fref(item)
279
+ ident = item&.at("./formattedref | ./docidentifier")
280
+ return unless ident
281
+
282
+ FormattedRef.new(
283
+ content: ident&.text, format: ident[:format],
284
+ language: ident[:language], script: ident[:script]
285
+ )
286
+ end
287
+ end
288
+ end
289
+ end
@@ -0,0 +1,34 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "relaton_bib/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "relaton-bib"
7
+ spec.version = RelatonBib::VERSION
8
+ spec.authors = ["Ribose Inc."]
9
+ spec.email = ["open.source@ribose.com"]
10
+
11
+ spec.summary = "RelatonBib: Ruby XMLDOC impementation."
12
+ spec.description = "RelatonBib: Ruby XMLDOC impementation."
13
+ spec.homepage = "https://github.com/metanorma/relaton-item"
14
+ spec.license = "BSD-2-Clause"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 2.0"
26
+ spec.add_development_dependency "debase"
27
+ spec.add_development_dependency "equivalent-xml", "~> 0.6"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ spec.add_development_dependency "ruby-debug-ide"
31
+ spec.add_development_dependency "simplecov"
32
+
33
+ spec.add_dependency "nokogiri", "~> 1.8.4"
34
+ end
metadata ADDED
@@ -0,0 +1,192 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: relaton-bib
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ribose Inc.
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-05-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: debase
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: equivalent-xml
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: ruby-debug-ide
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: nokogiri
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 1.8.4
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 1.8.4
125
+ description: 'RelatonBib: Ruby XMLDOC impementation.'
126
+ email:
127
+ - open.source@ribose.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".rubocop.yml"
135
+ - ".travis.yml"
136
+ - Gemfile
137
+ - Gemfile.lock
138
+ - LICENSE.txt
139
+ - README.adoc
140
+ - Rakefile
141
+ - bin/console
142
+ - bin/setup
143
+ - lib/relaton_bib.rb
144
+ - lib/relaton_bib/bib_item_locality.rb
145
+ - lib/relaton_bib/bibliographic_date.rb
146
+ - lib/relaton_bib/bibliographic_item.rb
147
+ - lib/relaton_bib/classification.rb
148
+ - lib/relaton_bib/contribution_info.rb
149
+ - lib/relaton_bib/contributor.rb
150
+ - lib/relaton_bib/copyright_association.rb
151
+ - lib/relaton_bib/document_identifier.rb
152
+ - lib/relaton_bib/document_relation.rb
153
+ - lib/relaton_bib/document_relation_collection.rb
154
+ - lib/relaton_bib/document_status.rb
155
+ - lib/relaton_bib/formatted_ref.rb
156
+ - lib/relaton_bib/formatted_string.rb
157
+ - lib/relaton_bib/localized_string.rb
158
+ - lib/relaton_bib/medium.rb
159
+ - lib/relaton_bib/organization.rb
160
+ - lib/relaton_bib/person.rb
161
+ - lib/relaton_bib/series.rb
162
+ - lib/relaton_bib/typed_title_string.rb
163
+ - lib/relaton_bib/typed_uri.rb
164
+ - lib/relaton_bib/validity.rb
165
+ - lib/relaton_bib/version.rb
166
+ - lib/relaton_bib/xml_parser.rb
167
+ - relaton-bib.gemspec
168
+ homepage: https://github.com/metanorma/relaton-item
169
+ licenses:
170
+ - BSD-2-Clause
171
+ metadata: {}
172
+ post_install_message:
173
+ rdoc_options: []
174
+ require_paths:
175
+ - lib
176
+ required_ruby_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ required_rubygems_version: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - ">="
184
+ - !ruby/object:Gem::Version
185
+ version: '0'
186
+ requirements: []
187
+ rubyforge_project:
188
+ rubygems_version: 2.6.12
189
+ signing_key:
190
+ specification_version: 4
191
+ summary: 'RelatonBib: Ruby XMLDOC impementation.'
192
+ test_files: []