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:

  • path to output directory

  • output format (yaml, xml, bibxml)



10
11
12
13
14
15
16
17
# File 'lib/relaton/core/data_fetcher.rb', line 10

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



20
21
22
23
24
25
26
27
28
# File 'lib/relaton/core/data_fetcher.rb', line 20

def self.fetch(source = nil, 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 = nil) ⇒ Object

Raises:



30
31
32
# File 'lib/relaton/core/data_fetcher.rb', line 30

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

#gh_issueObject



34
35
36
37
38
39
40
# File 'lib/relaton/core/data_fetcher.rb', line 34

def gh_issue
  return @gh_issue if defined? @gh_issue

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

#gh_issue_channelObject

Raises:



42
43
44
# File 'lib/relaton/core/data_fetcher.rb', line 42

def gh_issue_channel
  raise NotImplementedError, "#{self.class}#gh_issue_channel method must be implemented"
end

#log_error(_msg) ⇒ Object

Raises:



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

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:

  • ID

Returns:

  • filename based on PubID identifier



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

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

#repot_errorsObject



46
47
48
49
# File 'lib/relaton/core/data_fetcher.rb', line 46

def repot_errors
  @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:

  • <description>

Returns:

  • serialized bibliographic item



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

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

#to_bibxml(bib) ⇒ Object

Raises:



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

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

#to_xml(bib) ⇒ Object

Raises:



76
77
78
# File 'lib/relaton/core/data_fetcher.rb', line 76

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

#to_yaml(bib) ⇒ Object

Raises:



72
73
74
# File 'lib/relaton/core/data_fetcher.rb', line 72

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