Class: RelatonBipm::DataFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton_bipm/data_fetcher.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, format) ⇒ DataFetcher

Initialize fetcher

Parameters:

  • output (String)

    output directory to save files

  • format (String)

    format of output files (xml, yaml, bibxml)



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

#extObject (readonly)

Returns the value of attribute ext.



3
4
5
# File 'lib/relaton_bipm/data_fetcher.rb', line 3

def ext
  @ext
end

#filesObject (readonly)

Returns the value of attribute files.



3
4
5
# File 'lib/relaton_bipm/data_fetcher.rb', line 3

def files
  @files
end

#formatObject (readonly)

Returns the value of attribute format.



3
4
5
# File 'lib/relaton_bipm/data_fetcher.rb', line 3

def format
  @format
end

#index2Object (readonly)

Returns the value of attribute index2.



3
4
5
# File 'lib/relaton_bipm/data_fetcher.rb', line 3

def index2
  @index2
end

#outputObject (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

Parameters:

  • source (String)

    Source name

  • output (Strin) (defaults to: "data")

    directory to save files, default: “data”

  • format (Strin) (defaults to: "yaml")

    format of output files (xml, yaml, bibxml), default: yaml



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

Parameters:

  • source (String)

    Source name



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

Parameters:

  • path (String)

    Path to file

  • item (RelatonBipm::BipmBibliographicItem)

    document to save

  • warn_duplicate (Boolean, nil) (defaults to: true)

    Warn if document already exists



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