Class: RelatonBipm::DataFetcher
- Inherits:
-
Object
- Object
- RelatonBipm::DataFetcher
- Defined in:
- lib/relaton_bipm/data_fetcher.rb
Instance Attribute Summary collapse
-
#ext ⇒ Object
readonly
Returns the value of attribute ext.
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#index2 ⇒ Object
readonly
Returns the value of attribute index2.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Class Method Summary collapse
-
.fetch(source, output: "data", format: "yaml") ⇒ Object
Initialize fetcher and run fetching.
Instance Method Summary collapse
-
#fetch(source) ⇒ Object
Fetch bipm-data-outcomes or si-brochure.
-
#initialize(output, format) ⇒ DataFetcher
constructor
Initialize fetcher.
- #serialize(item) ⇒ Object
-
#write_file(path, item, warn_duplicate: true) ⇒ Object
Save document to file.
Constructor Details
#initialize(output, format) ⇒ DataFetcher
Initialize fetcher
11 12 13 14 15 16 17 |
# File 'lib/relaton_bipm/data_fetcher.rb', line 11 def initialize(output, format) @output = output @format = format @ext = format.sub(/^bib/, "") @files = [] @index2 = Relaton::Index.find_or_create :bipm, file: "index2.yaml" end |
Instance Attribute Details
#ext ⇒ Object (readonly)
Returns the value of attribute ext.
3 4 5 |
# File 'lib/relaton_bipm/data_fetcher.rb', line 3 def ext @ext end |
#files ⇒ Object (readonly)
Returns the value of attribute files.
3 4 5 |
# File 'lib/relaton_bipm/data_fetcher.rb', line 3 def files @files end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
3 4 5 |
# File 'lib/relaton_bipm/data_fetcher.rb', line 3 def format @format end |
#index2 ⇒ Object (readonly)
Returns the value of attribute index2.
3 4 5 |
# File 'lib/relaton_bipm/data_fetcher.rb', line 3 def index2 @index2 end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
3 4 5 |
# File 'lib/relaton_bipm/data_fetcher.rb', line 3 def output @output end |
Class Method Details
.fetch(source, output: "data", format: "yaml") ⇒ Object
Initialize fetcher and run fetching
26 27 28 29 30 31 32 33 34 |
# File 'lib/relaton_bipm/data_fetcher.rb', line 26 def self.fetch(source, output: "data", format: "yaml") t1 = Time.now puts "Started at: #{t1}" FileUtils.mkdir_p output new(output, format).fetch(source) t2 = Time.now puts "Stopped at: #{t2}" puts "Done in: #{(t2 - t1).round} sec." end |
Instance Method Details
#fetch(source) ⇒ Object
Fetch bipm-data-outcomes or si-brochure
41 42 43 44 45 46 47 48 |
# File 'lib/relaton_bipm/data_fetcher.rb', line 41 def fetch(source) case source when "bipm-data-outcomes" then DataOutcomesParser.parse(self) when "bipm-si-brochure" then BipmSiBrochureParser.parse(self) when "rawdata-bipm-metrologia" then RawdataBipmMetrologia::Fetcher.fetch(self) end index2.save end |
#serialize(item) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/relaton_bipm/data_fetcher.rb', line 67 def serialize(item) case @format when "xml" then item.to_xml bibdata: true when "yaml" then item.to_hash.to_yaml when "bibxml" then item.to_bibxml end end |
#write_file(path, item, warn_duplicate: true) ⇒ Object
Save document to file
57 58 59 60 61 62 63 64 65 |
# File 'lib/relaton_bipm/data_fetcher.rb', line 57 def write_file(path, item, warn_duplicate: true) content = serialize item if @files.include?(path) Util.warn "File #{path} already exists" if warn_duplicate else @files << path end File.write path, content, encoding: "UTF-8" end |