Class: Relaton::Core::DataFetcher

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, format) ⇒ DataFetcher

attr_accessor :docs

Initialize fetcher

Parameters:

  • output (String)

    path to output directory

  • format (String)

    output format (yaml, xml, bibxml)



13
14
15
16
17
18
19
20
# File 'lib/relaton/core/data_fetcher.rb', line 13

def initialize(output, format)
  @output = output
  @format = format
  @ext = format.sub "bibxml", "xml"
  @files = Set.new
  # @docs = []
  @errors = Hash.new(true)
end

Class Method Details

.fetch(source = nil, output: "data", format: "yaml") ⇒ Object

API method for external service

Returns:

  • the value returned by the instance ‘#fetch`, so callers can act on the outcome (e.g. relaton-iso returns whether it rebuilt).



26
27
28
29
30
31
32
33
34
35
# File 'lib/relaton/core/data_fetcher.rb', line 26

def self.fetch(source = nil, output: "data", format: "yaml")
  t1 = Time.now
  puts "Started at: #{t1}"
  FileUtils.mkdir_p output
  result = new(output, format).fetch(source)
  t2 = Time.now
  puts "Stopped at: #{t2}"
  puts "Done in: #{(t2 - t1).round} sec."
  result
end

Instance Method Details

#fetch(source = nil) ⇒ Object

Raises:

  • (NotImplementedError)


37
38
39
# File 'lib/relaton/core/data_fetcher.rb', line 37

def fetch(source = nil)
  raise NotImplementedError, "#{self.class}#fetch method must be implemented"
end

#gh_issueObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/relaton/core/data_fetcher.rb', line 41

def gh_issue
  return @gh_issue if defined? @gh_issue

  channel = gh_issue_channel
  if channel[0]
    @gh_issue = Relaton::Logger::Channels::GhIssue.new(*channel)
    Relaton.logger_pool[:gh_issue] = Relaton::Logger::Log.new(@gh_issue, levels: [:error])
  end
  @gh_issue
end

#gh_issue_channelObject



52
53
54
# File 'lib/relaton/core/data_fetcher.rb', line 52

def gh_issue_channel
  [ENV.fetch("GITHUB_REPOSITORY", nil), "Error fetching documents"]
end

#log_error(_msg) ⇒ Object

Raises:

  • (NoMatchingPatternError)


62
63
64
# File 'lib/relaton/core/data_fetcher.rb', line 62

def log_error(_msg)
  raise NoMatchingPatternError, "#{self.class}#log_error method must be implemented"
end

#output_file(docid) ⇒ String

Returns filename based on PubID identifier.

Parameters:

  • document (String)

    ID

Returns:

  • (String)

    filename based on PubID identifier



68
69
70
71
# File 'lib/relaton/core/data_fetcher.rb', line 68

def output_file(docid)
  id = docid.downcase.gsub(/[.\s\/:()-]+/, "-").delete_suffix("-")
  File.join @output, "#{id}.#{@ext}"
end

#report_errorsObject



56
57
58
59
60
# File 'lib/relaton/core/data_fetcher.rb', line 56

def report_errors
  gh_issue # register the channel before logging
  @errors.select { |_, v| v }.each_key { |k| log_error "Failed to fetch #{k}" }
  @gh_issue&.create_issue
end

#serialize(bib) ⇒ String

Serialize bibliographic item

Parameters:

  • bib (RelatonCcsds::BibliographicItem)

    <description>

Returns:

  • (String)

    serialized bibliographic item



80
81
82
# File 'lib/relaton/core/data_fetcher.rb', line 80

def serialize(bib)
  send "to_#{@format}", bib
end

#to_bibxml(bib) ⇒ Object

Raises:

  • (NotImplementedError)


92
93
94
# File 'lib/relaton/core/data_fetcher.rb', line 92

def to_bibxml(bib)
  raise NotImplementedError, "#{self.class}#to_bibxml method must be implemented"
end

#to_xml(bib) ⇒ Object

Raises:

  • (NotImplementedError)


88
89
90
# File 'lib/relaton/core/data_fetcher.rb', line 88

def to_xml(bib)
  raise NotImplementedError, "#{self.class}#to_xml method must be implemented"
end

#to_yaml(bib) ⇒ Object

Raises:

  • (NotImplementedError)


84
85
86
# File 'lib/relaton/core/data_fetcher.rb', line 84

def to_yaml(bib)
  raise NotImplementedError, "#{self.class}#to_yaml method must be implemented"
end