relaton-ogc 1.9.1 → 1.10.1

Sign up to get free protection for your applications and to get access to all the features.
data/grammars/ogc.rng CHANGED
@@ -62,6 +62,19 @@
62
62
  </optional>
63
63
  </element>
64
64
  </define>
65
+ <define name="DocumentSubtype">
66
+ <choice>
67
+ <value>conceptual-model</value>
68
+ <value>conceptual-model-and-encoding</value>
69
+ <value>conceptual-model-and-implementation</value>
70
+ <value>encoding</value>
71
+ <value>extension</value>
72
+ <value>implementation</value>
73
+ <value>profile</value>
74
+ <value>profile-with-extension</value>
75
+ <value>general</value>
76
+ </choice>
77
+ </define>
65
78
  </include>
66
79
  <define name="TextElement" combine="choice">
67
80
  <ref name="hi"/>
@@ -79,19 +92,6 @@
79
92
  </zeroOrMore>
80
93
  </element>
81
94
  </define>
82
- <define name="DocumentSubtype">
83
- <choice>
84
- <value>conceptual-model</value>
85
- <value>conceptual-model-and-encoding</value>
86
- <value>conceptual-model-and-implementation</value>
87
- <value>encoding</value>
88
- <value>extension</value>
89
- <value>implementation</value>
90
- <value>profile</value>
91
- <value>profile-with-extension</value>
92
- <value>general</value>
93
- </choice>
94
- </define>
95
95
  <define name="submitters">
96
96
  <element name="submitters">
97
97
  <ref name="Basic-Section"/>
data/grammars/reqt.rng CHANGED
@@ -64,9 +64,9 @@
64
64
  <optional>
65
65
  <ref name="label"/>
66
66
  </optional>
67
- <optional>
67
+ <zeroOrMore>
68
68
  <ref name="subject"/>
69
- </optional>
69
+ </zeroOrMore>
70
70
  <zeroOrMore>
71
71
  <ref name="reqinherit"/>
72
72
  </zeroOrMore>
@@ -80,6 +80,7 @@
80
80
  <ref name="verification"/>
81
81
  <ref name="import"/>
82
82
  <ref name="description"/>
83
+ <ref name="component"/>
83
84
  </choice>
84
85
  </zeroOrMore>
85
86
  <optional>
@@ -105,12 +106,16 @@
105
106
  </define>
106
107
  <define name="subject">
107
108
  <element name="subject">
108
- <text/>
109
+ <oneOrMore>
110
+ <ref name="TextElement"/>
111
+ </oneOrMore>
109
112
  </element>
110
113
  </define>
111
114
  <define name="reqinherit">
112
115
  <element name="inherit">
113
- <text/>
116
+ <oneOrMore>
117
+ <ref name="TextElement"/>
118
+ </oneOrMore>
114
119
  </element>
115
120
  </define>
116
121
  <define name="measurementtarget">
@@ -138,6 +143,12 @@
138
143
  <ref name="RequirementSubpart"/>
139
144
  </element>
140
145
  </define>
146
+ <define name="component">
147
+ <element name="component">
148
+ <attribute name="class"/>
149
+ <ref name="RequirementSubpart"/>
150
+ </element>
151
+ </define>
141
152
  <define name="reqt_references">
142
153
  <element name="references">
143
154
  <oneOrMore>
@@ -0,0 +1,98 @@
1
+ module RelatonOgc
2
+ class DataFetcher
3
+ module Utils
4
+ ENDPOINT = "https://raw.githubusercontent.com/opengeospatial/"\
5
+ "NamingAuthority/master/incubation/bibliography/"\
6
+ "bibliography.json".freeze
7
+
8
+ def get_data # rubocop:disable Metrics/AbcSize
9
+ h = {}
10
+ h["If-None-Match"] = etag if etag
11
+ resp = Faraday.new(ENDPOINT, headers: h).get
12
+ case resp.status
13
+ when 200
14
+ json = JSON.parse(resp.body)
15
+ block_given? ? yield(resp[:etag], json) : json
16
+ when 304 then [] # there aren't any changes since last fetching
17
+ else raise RelatonBib::RequestError, "Could not access #{ENDPOINT}"
18
+ end
19
+ end
20
+
21
+ #
22
+ # Read ETag form file
23
+ #
24
+ # @return [String, NilClass]
25
+ def etag
26
+ @etag ||= if File.exist? @etagfile
27
+ File.read @etagfile, encoding: "UTF-8"
28
+ end
29
+ end
30
+
31
+ #
32
+ # Save ETag to file
33
+ #
34
+ # @param tag [String]
35
+ def etag=(e_tag)
36
+ File.write @etagfile, e_tag, encoding: "UTF-8"
37
+ end
38
+ end
39
+
40
+ include Utils
41
+
42
+ #
43
+ # Create DataFetcher instance
44
+ #
45
+ # @param [String] output directory to save the documents
46
+ # @param [String] format output format "yaml" or "xmo"
47
+ #
48
+ def initialize(output, format)
49
+ @output = output
50
+ @etagfile = File.join output, "etag.txt"
51
+ @format = format
52
+ @docids = []
53
+ @dupids = []
54
+ end
55
+
56
+ def self.fetch(output: "data", format: "yaml")
57
+ t1 = Time.now
58
+ puts "Started at: #{t1}"
59
+ FileUtils.mkdir_p output unless Dir.exist? output
60
+ new(output, format).fetch
61
+ t2 = Time.now
62
+ puts "Stopped at: #{t2}"
63
+ puts "Done in: #{(t2 - t1).round} sec."
64
+ end
65
+
66
+ def fetch # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
67
+ get_data do |etag, json|
68
+ no_errors = true
69
+ json.each do |_, hit|
70
+ next if hit["type"] == "CC"
71
+
72
+ bib = Scrapper.parse_page hit
73
+ write_document bib
74
+ rescue StandardError => e
75
+ no_errors = false
76
+ warn "Fetching document: #{hit['identifier']}"
77
+ warn "#{e.class} #{e.message}"
78
+ warn e.backtrace
79
+ end
80
+ warn "[relaton-ogc] WARNING Duplicated documents: #{@dupids.uniq.join(', ')}" if @dupids.any?
81
+ self.etag = etag if no_errors
82
+ end
83
+ end
84
+
85
+ def write_document(bib) # rubocop:disable Metrics/AbcSize
86
+ if @docids.include?(bib.docidentifier[0].id)
87
+ @dupids << bib.docidentifier[0].id
88
+ return
89
+ end
90
+
91
+ @docids << bib.docidentifier[0].id
92
+ name = bib.docidentifier[0].id.upcase.gsub(/[\s:.]/, "_")
93
+ file = "#{@output}/#{name}.#{@format}"
94
+ content = @format == "xml" ? bib.to_xml(bibdata: true) : bib.to_hash.to_yaml
95
+ File.write file, content, encoding: "UTF-8"
96
+ end
97
+ end
98
+ end
@@ -1,9 +1,20 @@
1
1
  module RelatonOgc
2
2
  class Hit < RelatonBib::Hit
3
+ #
4
+ # <Description>
5
+ #
6
+ # @param [RelatonOgc::OgcBibliographicItem] bibitem
7
+ # @param [RelatonOgc::HitCollection, nil] hitcoll
8
+ #
9
+ def initialize(bibitem, hitcoll = nil)
10
+ super({ id: bibitem.docidentifier[0].id}, hitcoll)
11
+ @fetch = bibitem
12
+ end
13
+
3
14
  # Parse page.
4
15
  # @return [RelatonNist::NistBliographicItem]
5
16
  def fetch
6
- @fetch ||= Scrapper.parse_page @hit
17
+ @fetch # ||= Scrapper.parse_page @hit
7
18
  end
8
19
  end
9
20
  end
@@ -4,86 +4,71 @@ require "fileutils"
4
4
 
5
5
  module RelatonOgc
6
6
  class HitCollection < RelatonBib::HitCollection
7
- ENDPOINT = "https://raw.githubusercontent.com/opengeospatial/"\
8
- "NamingAuthority/master/incubation/bibliography/bibliography.json".freeze
9
- DATADIR = File.expand_path ".relaton/ogc/", Dir.home
10
- DATAFILE = File.expand_path "bibliography.json", DATADIR
11
- ETAGFILE = File.expand_path "etag.txt", DATADIR
7
+ # include DataFetcher::Utils
12
8
 
13
- # @param ref [Strig]
9
+ # ENDPOINT = "https://raw.githubusercontent.com/opengeospatial/"\
10
+ # "NamingAuthority/master/incubation/bibliography/"\
11
+ # "bibliography.json".freeze
12
+ ENDPOINT = "https://raw.githubusercontent.com/relaton/relaton-data-ogc/main/data/".freeze
13
+ # DATADIR = File.expand_path ".relaton/ogc/", Dir.home
14
+ # DATAFILE = File.expand_path "bibliography.json", DATADIR
15
+ # ETAGFILE = File.expand_path "etag.txt", DATADIR
16
+
17
+ # @param code [Strig]
14
18
  # @param year [String]
15
19
  # @param opts [Hash]
16
- def initialize(ref, year = nil)
20
+ def initialize(code, year = nil) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
17
21
  super
18
- @array = from_json(ref).sort_by do |hit|
19
- begin
20
- hit.hit["date"] ? Date.parse(hit.hit["date"]) : Date.new
21
- rescue ArgumentError
22
- Date.parse "0000-01-01"
23
- end
24
- end.reverse
22
+ # @etagfile = File.expand_path "etag.txt", DATADIR
23
+ # @array = from_json(ref).sort_by do |hit|
24
+ # hit.hit["date"] ? Date.parse(hit.hit["date"]) : Date.new
25
+ # rescue ArgumentError
26
+ # Date.parse "0000-01-01"
27
+ # end.reverse
28
+ resp = Faraday.get "#{ENDPOINT}#{code.upcase.gsub(/[\s:.]/, '_')}.yaml"
29
+ @array = case resp.status
30
+ when 200
31
+ bib = OgcBibliographicItem.from_hash YAML.safe_load(resp.body)
32
+ [Hit.new(bib, self)]
33
+ else []
34
+ end
25
35
  end
26
36
 
27
- private
37
+ # private
28
38
 
29
39
  #
30
40
  # Fetch data form json
31
41
  #
32
42
  # @param docid [String]
33
- def from_json(docid, **_opts)
34
- ref = docid.sub(/^OGC\s/, "").strip
35
- return [] if ref.empty?
43
+ # def from_json(docid, **_opts)
44
+ # ref = docid.sub(/^OGC\s/, "").strip
45
+ # return [] if ref.empty?
36
46
 
37
- data.select do |_k, doc|
38
- doc["type"] != "CC" && doc["identifier"].include?(ref)
39
- end.map { |_k, h| Hit.new(h, self) }
40
- end
47
+ # data.select do |_k, doc|
48
+ # doc["type"] != "CC" && doc["identifier"].include?(ref)
49
+ # end.map { |_k, h| Hit.new(h, self) }
50
+ # end
41
51
 
42
52
  #
43
53
  # Fetches json data
44
54
  #
45
55
  # @return [Hash]
46
- def data
47
- ctime = File.ctime DATAFILE if File.exist? DATAFILE
48
- fetch_data if !ctime || ctime.to_date < Date.today
49
- @data ||= JSON.parse File.read(DATAFILE, encoding: "UTF-8")
50
- end
56
+ # def data
57
+ # ctime = File.ctime DATAFILE if File.exist? DATAFILE
58
+ # fetch_data if !ctime || ctime.to_date < Date.today
59
+ # @data ||= JSON.parse File.read(DATAFILE, encoding: "UTF-8")
60
+ # end
51
61
 
52
62
  #
53
63
  # fetch data form server and save it to file.
54
64
  #
55
- def fetch_data # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
56
- h = {}
57
- h["If-None-Match"] = etag if etag
58
- resp = Faraday.new(ENDPOINT, headers: h).get
59
- # return if there aren't any changes since last fetching
60
- return if resp.status == 304
61
- unless resp.status == 200
62
- raise RelatonBib::RequestError, "Could not access #{ENDPOINT}"
63
- end
64
-
65
- FileUtils.mkdir_p DATADIR unless Dir.exist? DATADIR
66
- self.etag = resp[:etag]
67
- @data = JSON.parse resp.body
68
- File.write DATAFILE, @data.to_json, encoding: "UTF-8"
69
- end
70
-
71
- #
72
- # Read ETag form file
73
- #
74
- # @return [String, NilClass]
75
- def etag
76
- @etag ||= if File.exist? ETAGFILE
77
- File.read ETAGFILE, encoding: "UTF-8"
78
- end
79
- end
65
+ # def fetch_data
66
+ # json = get_data
67
+ # return unless json
80
68
 
81
- #
82
- # Save ETag to file
83
- #
84
- # @param tag [String]
85
- def etag=(e_tag)
86
- File.write ETAGFILE, e_tag, encoding: "UTF-8"
87
- end
69
+ # FileUtils.mkdir_p DATADIR unless Dir.exist? DATADIR
70
+ # @data = json
71
+ # File.write DATAFILE, @data.to_json, encoding: "UTF-8"
72
+ # end
88
73
  end
89
74
  end
@@ -4,7 +4,8 @@ module RelatonOgc
4
4
  # @param text [String]
5
5
  # @return [RelatonOgc::HitCollection]
6
6
  def search(text, year = nil, _opts = {})
7
- HitCollection.new text, year
7
+ code = text.sub(/^OGC\s/, "")
8
+ HitCollection.new code, year
8
9
  rescue Faraday::ConnectionFailed
9
10
  raise RelatonBib::RequestError, HitCollection::ENDPOINT
10
11
  end
@@ -2,11 +2,12 @@ require "relaton/processor"
2
2
 
3
3
  module RelatonOgc
4
4
  class Processor < Relaton::Processor
5
- def initialize
5
+ def initialize # rubocop:disable Lint/MissingSuper
6
6
  @short = :relaton_ogc
7
7
  @prefix = "OGC"
8
8
  @defaultprefix = %r{^OGC\s}
9
9
  @idtype = "OGC"
10
+ @datasets = %w[ogc-naming-authority]
10
11
  end
11
12
 
12
13
  # @param code [String]
@@ -17,6 +18,18 @@ module RelatonOgc
17
18
  ::RelatonOgc::OgcBibliography.get(code, date, opts)
18
19
  end
19
20
 
21
+ #
22
+ # Fetch all the documents from a source
23
+ #
24
+ # @param [String] _source source name
25
+ # @param [Hash] opts
26
+ # @option opts [String] :output directory to output documents
27
+ # @option opts [String] :format
28
+ #
29
+ def fetch_data(_source, opts)
30
+ DataFetcher.fetch(**opts)
31
+ end
32
+
20
33
  # @param xml [String]
21
34
  # @return [RelatonOgc::OgcBibliographicItem]
22
35
  def from_xml(xml)
@@ -13,7 +13,7 @@ module RelatonOgc
13
13
  "IPR" => { type: "engineering-report" },
14
14
  "IS" => { type: "standard", subtype: "implementation" },
15
15
  "ISC" => { type: "standard", subtype: "implementation" },
16
- "ISx" => { type: "standard", subtype: "extesion" },
16
+ "ISx" => { type: "standard", subtype: "extension" },
17
17
  "Notes" => { type: "other" },
18
18
  "ORM" => { type: "reference-model" },
19
19
  "PC" => { type: "standard", subtype: "profile" },
@@ -70,7 +70,7 @@ module RelatonOgc
70
70
  # @param identifier [String]
71
71
  # @return [Array<RelatonBib::DocumentIdentifier>]
72
72
  def fetch_docid(identifier)
73
- [RelatonBib::DocumentIdentifier.new(id: identifier, type: "OGC")]
73
+ [RelatonBib::DocumentIdentifier.new(id: identifier, type: "OGC", primary: true)]
74
74
  end
75
75
 
76
76
  # @param url [String]
@@ -88,7 +88,7 @@ module RelatonOgc
88
88
  # @param stage [String]
89
89
  # @return [RelatonBib::DocumentStatus, NilClass]
90
90
  def fetch_status(stage)
91
- stage && RelatonBib::DocunentStatus.new(stage: stage)
91
+ stage && RelatonBib::DocumentStatus.new(stage: stage)
92
92
  end
93
93
 
94
94
  # @param identifier [String]
@@ -138,6 +138,8 @@ module RelatonOgc
138
138
  # @param date [String]
139
139
  # @return [Array<RelatonBib::BibliographicDate>]
140
140
  def fetch_date(date)
141
+ return [] unless date
142
+
141
143
  [RelatonBib::BibliographicDate.new(type: "published", on: date)]
142
144
  end
143
145
  end
@@ -1,3 +1,3 @@
1
1
  module RelatonOgc
2
- VERSION = "1.9.1".freeze
2
+ VERSION = "1.10.1".freeze
3
3
  end
data/lib/relaton_ogc.rb CHANGED
@@ -2,6 +2,7 @@ require "relaton_iso_bib"
2
2
  require "relaton_ogc/version"
3
3
  require "relaton_ogc/ogc_bibliographic_item"
4
4
  require "relaton_ogc/ogc_bibliography"
5
+ require "relaton_ogc/data_fetcher"
5
6
  require "relaton_ogc/hit_collection"
6
7
  require "relaton_ogc/scrapper"
7
8
  require "relaton_ogc/xml_parser"
data/relaton_ogc.gemspec CHANGED
@@ -35,5 +35,5 @@ Gem::Specification.new do |spec|
35
35
  spec.add_development_dependency "webmock"
36
36
 
37
37
  spec.add_dependency "faraday", "~> 1.1"
38
- spec.add_dependency "relaton-iso-bib", "~> 1.9.0"
38
+ spec.add_dependency "relaton-iso-bib", "~> 1.10.1"
39
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relaton-ogc
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.1
4
+ version: 1.10.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-09-07 00:00:00.000000000 Z
11
+ date: 2022-02-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: equivalent-xml
@@ -142,14 +142,14 @@ dependencies:
142
142
  requirements:
143
143
  - - "~>"
144
144
  - !ruby/object:Gem::Version
145
- version: 1.9.0
145
+ version: 1.10.1
146
146
  type: :runtime
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
- version: 1.9.0
152
+ version: 1.10.1
153
153
  description: 'RelatonOgc: retrieve OGC Standards for bibliographic use using the OgcBibliographicItem
154
154
  model'
155
155
  email:
@@ -175,6 +175,7 @@ files:
175
175
  - grammars/ogc.rng
176
176
  - grammars/reqt.rng
177
177
  - lib/relaton_ogc.rb
178
+ - lib/relaton_ogc/data_fetcher.rb
178
179
  - lib/relaton_ogc/editorial_group.rb
179
180
  - lib/relaton_ogc/hash_converter.rb
180
181
  - lib/relaton_ogc/hit.rb