Class: Relaton::Core::DataFetcher
- Inherits:
-
Object
- Object
- Relaton::Core::DataFetcher
- Defined in:
- lib/relaton/core/data_fetcher.rb
Class Method Summary collapse
-
.fetch(source = nil, output: "data", format: "yaml") ⇒ Object
API method for external service.
Instance Method Summary collapse
- #fetch(source = nil) ⇒ Object
- #gh_issue ⇒ Object
- #gh_issue_channel ⇒ Object
-
#initialize(output, format) ⇒ DataFetcher
constructor
attr_accessor :docs.
- #log_error(_msg) ⇒ Object
-
#output_file(docid) ⇒ String
Filename based on PubID identifier.
- #report_errors ⇒ Object
-
#serialize(bib) ⇒ String
Serialize bibliographic item.
- #to_bibxml(bib) ⇒ Object
- #to_xml(bib) ⇒ Object
- #to_yaml(bib) ⇒ Object
Constructor Details
#initialize(output, format) ⇒ DataFetcher
attr_accessor :docs
Initialize fetcher
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
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
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_issue ⇒ Object
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_channel ⇒ Object
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
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.
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_errors ⇒ Object
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
80 81 82 |
# File 'lib/relaton/core/data_fetcher.rb', line 80 def serialize(bib) send "to_#{@format}", bib end |
#to_bibxml(bib) ⇒ Object
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
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
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 |