relaton-bib 0.3.3 → 0.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/relaton_bib.rb CHANGED
@@ -1,8 +1,40 @@
1
1
  require "relaton_bib/version"
2
- require "relaton_bib/bibliographic_item.rb"
2
+ require "relaton_bib/bibliographic_item"
3
+ require "relaton_bib/hit_collection"
4
+ require "relaton_bib/hit"
3
5
 
4
6
  module RelatonBib
5
7
  class Error < StandardError; end
6
8
 
7
9
  class RequestError < StandardError; end
10
+
11
+ class << self
12
+ # @param date [String]
13
+ # @return [Date, NilClass]
14
+ def parse_date(sdate)
15
+ if /(?<date>\w+\s\d{4})/ =~ sdate # February 2012
16
+ Date.strptime(date, "%B %Y")
17
+ elsif /(?<date>\w+\s\d{1,2},\s\d{4})/ =~ sdate # February 11, 2012
18
+ Date.strptime(date, "%B %d, %Y")
19
+ elsif /(?<date>\d{4}-\d{2}-\d{2})/ =~ sdate # 2012-02-11
20
+ Date.parse(date)
21
+ elsif /(?<date>\d{4}-\d{2})/ =~ sdate # 2012-02
22
+ Date.strptime date, "%Y-%m"
23
+ elsif /(?<date>\d{4})/ =~ sdate # 2012
24
+ Date.strptime date, "%Y"
25
+ end
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ # @param array [Array]
32
+ # @return [Array<String>, String]
33
+ def single_element_array(array)
34
+ if array.size > 1
35
+ array.map { |e| e.is_a?(String) ? e : e.to_hash }
36
+ else
37
+ array[0].is_a?(String) ? array[0] : array[0].to_hash
38
+ end
39
+ end
8
40
  end
@@ -17,18 +17,18 @@ module RelatonBib
17
17
 
18
18
  # Bibliographic item locality.
19
19
  class BibItemLocality
20
- # @return [RelatonBib::SpecificLocalityType]
20
+ # @return [String]
21
21
  attr_reader :type
22
22
 
23
- # @return [RelatonBib::LocalizedString]
23
+ # @return [String]
24
24
  attr_reader :reference_from
25
25
 
26
- # @return [RelatonBib::LocalizedString]
26
+ # @return [String, NilClass]
27
27
  attr_reader :reference_to
28
28
 
29
29
  # @param type [String]
30
30
  # @param referenceFrom [String]
31
- # @param referenceTo [String]
31
+ # @param referenceTo [String, NilClass]
32
32
  def initialize(type, reference_from, reference_to = nil)
33
33
  @type = type
34
34
  @reference_from = reference_from
@@ -42,5 +42,12 @@ module RelatonBib
42
42
  builder.referenceTo reference_to if reference_to
43
43
  end
44
44
  end
45
+
46
+ # @return [Hash]
47
+ def to_hash
48
+ hash = { "type" => type, "reference_from" => reference_from }
49
+ hash["reference_to"] = reference_to if reference_to
50
+ hash
51
+ end
45
52
  end
46
- end
53
+ end
@@ -1,13 +1,13 @@
1
1
  module RelatonBib
2
2
  class BiblioNote < FormattedString
3
- # @return [String]
3
+ # @return [String, NilClass]
4
4
  attr_reader :type
5
5
 
6
6
  # @param content [String]
7
- # @param type [String]
8
- # @param language [String] language code Iso639
9
- # @param script [String] script code Iso15924
10
- # @param format [String] the content type
7
+ # @param type [String, NilClass]
8
+ # @param language [String, NilClass] language code Iso639
9
+ # @param script [String, NilClass] script code Iso15924
10
+ # @param format [String, NilClass] the content format
11
11
  def initialize(content:, type: nil, language: nil, script: nil, format: nil)
12
12
  @type = type
13
13
  super content: content, language: language, script: script, format: format
@@ -19,5 +19,15 @@ module RelatonBib
19
19
  xml[:type] = type if type
20
20
  xml
21
21
  end
22
+
23
+ # @return [Hash]
24
+ def to_hash
25
+ hash = super
26
+ return hash unless type
27
+
28
+ hash = { "content" => hash } if hash.is_a? String
29
+ hash["type"] = type
30
+ hash
31
+ end
22
32
  end
23
33
  end
@@ -2,13 +2,15 @@ module RelatonBib
2
2
  # Version
3
3
  class BibliographicItem
4
4
  class Version
5
- # @return [String]
5
+ include RelatonBib
6
+
7
+ # @return [String, NilClass]
6
8
  attr_reader :revision_date
7
9
 
8
10
  # @return [Array<String>]
9
11
  attr_reader :draft
10
12
 
11
- # @param revision_date [String]
13
+ # @param revision_date [String, NilClass]
12
14
  # @param draft [Array<String>]
13
15
  def initialize(revision_date = nil, draft = [])
14
16
  @revision_date = revision_date
@@ -22,6 +24,14 @@ module RelatonBib
22
24
  draft.each { |d| builder.draft d }
23
25
  end
24
26
  end
27
+
28
+ # @return [Hash]
29
+ def to_hash
30
+ hash = {}
31
+ hash["revision_date"] = revision_date if revision_date
32
+ hash["draft"] = single_element_array(draft) if draft&.any?
33
+ hash
34
+ end
25
35
  end
26
36
  end
27
37
  end
@@ -6,34 +6,33 @@ module RelatonBib
6
6
  # Bibliographic date.
7
7
  class BibliographicDate
8
8
  TYPES = %w[published accessed created implemented obsoleted confirmed
9
- updated issued transmitted copied unchanged circulated
10
- ].freeze
9
+ updated issued transmitted copied unchanged circulated adapted].freeze
11
10
 
12
11
  # @return [String]
13
12
  attr_reader :type
14
13
 
15
- # @return [Time]
14
+ # @return [Date]
16
15
  attr_reader :from
17
16
 
18
- # @return [Time]
17
+ # @return [Date]
19
18
  attr_reader :to
20
19
 
21
- # @return [Time]
20
+ # @return [Date]
22
21
  attr_reader :on
23
22
 
24
23
  # @param type [String] "published", "accessed", "created", "activated"
24
+ # @param on [String]
25
25
  # @param from [String]
26
26
  # @param to [String]
27
27
  def initialize(type:, on: nil, from: nil, to: nil)
28
28
  raise ArgumentError, "expected :on or :from argument" unless on || from
29
29
 
30
- TYPES.include?(type) or
31
- raise ArgumentError, %{Type "#{type}" is invalid.} unless
30
+ # raise ArgumentError, "invalid type: #{type}" unless TYPES.include? type
32
31
 
33
32
  @type = type
34
- @on = parse_date on
35
- @from = parse_date from
36
- @to = parse_date to
33
+ @on = RelatonBib.parse_date on
34
+ @from = RelatonBib.parse_date from
35
+ @to = RelatonBib.parse_date to
37
36
  end
38
37
 
39
38
  # rubocop:disable Metrics/AbcSize
@@ -52,6 +51,15 @@ module RelatonBib
52
51
  end
53
52
  # rubocop:enable Metrics/AbcSize
54
53
 
54
+ # @return [Hash]
55
+ def to_hash
56
+ hash = { "type" => type }
57
+ hash["value"] = on.to_s if on
58
+ hash["from"] = from.to_s if from
59
+ hash["to"] = to.to_s if to
60
+ hash
61
+ end
62
+
55
63
  private
56
64
 
57
65
  # Formats date
@@ -65,19 +73,5 @@ module RelatonBib
65
73
  else date.year
66
74
  end
67
75
  end
68
-
69
- # @params date [String] 'yyyy', 'yyyy-mm', 'yyyy-mm-dd
70
- # @return [Time]
71
- def parse_date(date)
72
- return unless date
73
-
74
- if date =~ /^\d{4}$/
75
- Time.strptime date, "%Y"
76
- elsif date =~ /^\d{4}-\d{2}$/
77
- Time.strptime date, "%Y-%m"
78
- elsif date =~ /\d{4}-\d{2}-\d{2}$/
79
- Time.strptime date, "%Y-%m-%d"
80
- end
81
- end
82
76
  end
83
77
  end
@@ -26,13 +26,14 @@ require "relaton_bib/hash_converter"
26
26
  module RelatonBib
27
27
  # Bibliographic item
28
28
  class BibliographicItem
29
+ include RelatonBib
30
+
29
31
  TYPES = %W[article book booklet conference manual proceedings presentation
30
32
  thesis techreport standard unpublished map electronic\sresource
31
33
  audiovisual film video broadcast graphic_work music patent
32
- inbook incollection inproceedings journal
33
- ].freeze
34
+ inbook incollection inproceedings journal].freeze
34
35
 
35
- # @return [String]
36
+ # @return [String, NilClass]
36
37
  attr_reader :id
37
38
 
38
39
  # @return [Array<RelatonBib::TypedTitleString>]
@@ -41,13 +42,13 @@ module RelatonBib
41
42
  # @return [Array<RelatonBib::TypedUri>]
42
43
  attr_reader :link
43
44
 
44
- # @return [String]
45
+ # @return [String, NilClass]
45
46
  attr_reader :type
46
47
 
47
48
  # @return [Array<RelatonBib::DocumentIdentifier>]
48
49
  attr_reader :docidentifier
49
50
 
50
- # @return [String] docnumber
51
+ # @return [String, NilClass] docnumber
51
52
  attr_reader :docnumber
52
53
 
53
54
  # @return [Array<RelatonBib::BibliographicDate>]
@@ -59,10 +60,10 @@ module RelatonBib
59
60
  # @return [String, NillClass]
60
61
  attr_reader :edition
61
62
 
62
- # @return [RelatonBib::BibliongraphicItem::Version]
63
+ # @return [RelatonBib::BibliongraphicItem::Version, NilClass]
63
64
  attr_reader :version
64
65
 
65
- # @return [Array<RelatonBib::BiblioNote>, NilClass]
66
+ # @return [Array<RelatonBib::BiblioNote>]
66
67
  attr_reader :biblionote
67
68
 
68
69
  # @return [Array<String>] language Iso639 code
@@ -71,7 +72,7 @@ module RelatonBib
71
72
  # @return [Array<String>] script Iso15924 code
72
73
  attr_reader :script
73
74
 
74
- # @return [RelatonBib::FormattedRef]
75
+ # @return [RelatonBib::FormattedRef, NilClass]
75
76
  attr_reader :formattedref
76
77
 
77
78
  # @!attribute [r] abstract
@@ -80,7 +81,7 @@ module RelatonBib
80
81
  # @return [RelatonBib::DocumentStatus, NilClass]
81
82
  attr_reader :status
82
83
 
83
- # @return [RelatonBib::CopyrightAssociation]
84
+ # @return [RelatonBib::CopyrightAssociation, NilClass]
84
85
  attr_reader :copyright
85
86
 
86
87
  # @return [RelatonBib::DocRelationCollection]
@@ -134,6 +135,11 @@ module RelatonBib
134
135
  # @param validity [RelatonBib:Validity, NilClass]
135
136
  # @param fetched [Date, NilClass] default nil
136
137
  #
138
+ # @param copyright [Hash, RelatonBib::CopyrightAssociation, NilClass]
139
+ # @option copyright [Hash, RelatonBib::ContributionInfo] :owner
140
+ # @option copyright [String] :form
141
+ # @option copyright [String, NilClass] :to
142
+ #
137
143
  # @param date [Array<Hash>]
138
144
  # @option date [String] :type
139
145
  # @option date [String] :from
@@ -163,7 +169,7 @@ module RelatonBib
163
169
  # @option link [String] :content
164
170
  def initialize(**args)
165
171
  if args[:type] && !TYPES.include?(args[:type])
166
- raise ArgumentError, %{Type "#{args[:type]}" is invalid.}
172
+ warn %{Document type "#{args[:type]}" is invalid.}
167
173
  end
168
174
 
169
175
  @title = (args[:title] || []).map do |t|
@@ -204,7 +210,7 @@ module RelatonBib
204
210
  @language = args.fetch :language, []
205
211
  @script = args.fetch :script, []
206
212
  @status = args[:docstatus]
207
- @relation = DocRelationCollection.new(args[:relation] || [])
213
+ @relation = DocRelationCollection.new(args[:relation] || [])
208
214
  @link = args.fetch(:link, []).map { |s| s.is_a?(Hash) ? TypedUri.new(s) : s }
209
215
  @series = args.fetch :series, []
210
216
  @medium = args[:medium]
@@ -265,6 +271,38 @@ module RelatonBib
265
271
  end
266
272
  end
267
273
 
274
+ # @return [Hash]
275
+ def to_hash
276
+ hash = {}
277
+ hash["id"] = id if id
278
+ hash["title"] = single_element_array(title) if title&.any?
279
+ hash["link"] = single_element_array(link) if link&.any?
280
+ hash["type"] = type if type
281
+ hash["docid"] = single_element_array(docidentifier) if docidentifier&.any?
282
+ hash["docnumber"] = docnumber if docnumber
283
+ hash["date"] = single_element_array(date) if date&.any?
284
+ hash["contributor"] = single_element_array(contributor) if contributor&.any?
285
+ hash["edition"] = edition if edition
286
+ hash["version"] = version.to_hash if version
287
+ hash["biblionote"] = single_element_array(biblionote) if biblionote&.any?
288
+ hash["language"] = single_element_array(language) if language&.any?
289
+ hash["script"] = single_element_array(script) if script&.any?
290
+ hash["formattedref"] = formattedref.to_hash if formattedref
291
+ hash["abstract"] = single_element_array(abstract) if abstract&.any?
292
+ hash["docstatus"] = status.to_hash if status
293
+ hash["copyright"] = copyright.to_hash if copyright
294
+ hash["relation"] = single_element_array(relation) if relation&.any?
295
+ hash["series"] = single_element_array(series) if series&.any?
296
+ hash["medium"] = medium.to_hash if medium
297
+ hash["place"] = single_element_array(place) if place&.any?
298
+ hash["extent"] = single_element_array(extent) if extent&.any?
299
+ hash["accesslocation"] = single_element_array(accesslocation) if accesslocation&.any?
300
+ hash["classification"] = classification.to_hash if classification
301
+ hash["validity"] = validity.to_hash if validity
302
+ hash["fetched"] = fetched.to_s if fetched
303
+ hash
304
+ end
305
+
268
306
  private
269
307
 
270
308
  # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
@@ -18,5 +18,12 @@ module RelatonBib
18
18
  xml = builder.classification value
19
19
  xml[:type] = type if type
20
20
  end
21
+
22
+ # @return [Hash]
23
+ def to_hash
24
+ hash = { "value" => value }
25
+ hash["type"] = type if type
26
+ hash
27
+ end
21
28
  end
22
29
  end
@@ -4,17 +4,17 @@ require "relaton_bib/person"
4
4
 
5
5
  # RelatonBib module
6
6
  module RelatonBib
7
-
8
7
  # Contributor's role.
9
8
  class ContributorRole
9
+ include RelatonBib
10
+
10
11
  TYPES = %w[author performer publisher editor adapter translator
11
- distributor
12
- ].freeze
12
+ distributor].freeze
13
13
 
14
14
  # @return [Array<RelatonBib::FormattedString>]
15
15
  attr_reader :description
16
16
 
17
- # @return [ContributorRoleType]
17
+ # @return [Strig]
18
18
  attr_reader :type
19
19
 
20
20
  # @param type [String] allowed types "author", "editor",
@@ -29,6 +29,7 @@ module RelatonBib
29
29
  @description = args.fetch(:description, []).map { |d| FormattedString.new content: d, format: nil }
30
30
  end
31
31
 
32
+ # @param builder [Nokogiri::XML::Builder]
32
33
  def to_xml(builder)
33
34
  builder.role(type: type) do
34
35
  description.each do |d|
@@ -36,10 +37,23 @@ module RelatonBib
36
37
  end
37
38
  end
38
39
  end
40
+
41
+ # @return [Hash, String]
42
+ def to_hash
43
+ if description&.any?
44
+ hash = { "description" => single_element_array(description) }
45
+ hash["type"] = type if type
46
+ hash
47
+ elsif type
48
+ type
49
+ end
50
+ end
39
51
  end
40
52
 
41
53
  # Contribution info.
42
54
  class ContributionInfo
55
+ include RelatonBib
56
+
43
57
  # @return [Array<RelatonBib::ContributorRole>]
44
58
  attr_reader :role
45
59
 
@@ -54,8 +68,16 @@ module RelatonBib
54
68
  @role = role.map { |r| ContributorRole.new(**r) }
55
69
  end
56
70
 
71
+ # @param builder [Nokogiri::XML::Builder]
57
72
  def to_xml(builder)
58
73
  entity.to_xml builder
59
74
  end
75
+
76
+ # @return [Hash]
77
+ def to_hash
78
+ hash = entity.to_hash
79
+ hash["role"] = single_element_array(role) if role&.any?
80
+ hash
81
+ end
60
82
  end
61
83
  end
@@ -43,6 +43,17 @@ module RelatonBib
43
43
  doc.postcode postcode if postcode
44
44
  end
45
45
  end
46
+
47
+ # @return [Hash]
48
+ def to_hash
49
+ hash = {}
50
+ hash["street"] = street if street&.any?
51
+ hash["city"] = city
52
+ hash["state"] = state if state
53
+ hash["country"] = country
54
+ hash["postcode"] = postcode if postcode
55
+ hash
56
+ end
46
57
  end
47
58
 
48
59
  # Contact class.
@@ -63,11 +74,18 @@ module RelatonBib
63
74
  def to_xml(doc)
64
75
  doc.send type, value
65
76
  end
77
+
78
+ # @return [Hash]
79
+ def to_hash
80
+ { "type" => type, "value" => value }
81
+ end
66
82
  end
67
83
 
68
84
  # Affilation.
69
85
  class Affilation
70
- # @return [RelatonBib::LocalizedString]
86
+ include RelatonBib
87
+
88
+ # @return [RelatonBib::LocalizedString, NilClass]
71
89
  attr_reader :name
72
90
 
73
91
  # @return [Array<RelatonBib::FormattedString>]
@@ -77,8 +95,10 @@ module RelatonBib
77
95
  attr_reader :organization
78
96
 
79
97
  # @param organization [RelatonBib::Organization]
98
+ # @param name [RelatonBib::LocalizedString, NilClass]
80
99
  # @param description [Array<RelatonBib::FormattedString>]
81
- def initialize(organization, description = [])
100
+ def initialize(organization:, name: nil, description: [])
101
+ @name = name
82
102
  @organization = organization
83
103
  @description = description
84
104
  end
@@ -91,18 +111,28 @@ module RelatonBib
91
111
  organization.to_xml builder
92
112
  end
93
113
  end
114
+
115
+ # @return [Hash]
116
+ def to_hash
117
+ hash = organization.to_hash
118
+ hash["name"] = name.to_hash if name
119
+ hash["description"] = single_element_array(description) if description&.any?
120
+ hash
121
+ end
94
122
  end
95
123
 
96
124
  # Contributor.
97
125
  class Contributor
126
+ include RelatonBib
127
+
98
128
  # @return [URI]
99
129
  attr_reader :uri
100
130
 
101
- # @return [Array<RelatonBib::Address, RelatonBib::Phone>]
131
+ # @return [Array<RelatonBib::Address, RelatonBib::Contact>]
102
132
  attr_reader :contact
103
133
 
104
134
  # @param url [String]
105
- # @param contact [Array<RelatonBib::Address, RelatonBib::Phone>]
135
+ # @param contact [Array<RelatonBib::Address, RelatonBib::Contact>]
106
136
  def initialize(url: nil, contact: [])
107
137
  @uri = URI url if url
108
138
  @contact = contact
@@ -118,5 +148,13 @@ module RelatonBib
118
148
  def to_xml(builder)
119
149
  contact.each { |contact| contact.to_xml builder }
120
150
  end
151
+
152
+ # @return [Hash]
153
+ def to_hash
154
+ hash = {}
155
+ hash["url"] = uri.to_s if uri
156
+ hash["contact"] = single_element_array(contact) if contact&.any?
157
+ hash
158
+ end
121
159
  end
122
160
  end