Class: RelatonCalconnect::DataFetcher
- Inherits:
-
Object
- Object
- RelatonCalconnect::DataFetcher
- Defined in:
- lib/relaton_calconnect/data_fetcher.rb
Overview
Relaton-calconnect data fetcher
Constant Summary collapse
- ENDPOINT =
DOMAIN = “standards.calconnect.org/” SCHEME, HOST = DOMAIN.split(%r:?/?/)
"https://standards.calconnect.org/relaton/index.yaml"
Class Method Summary collapse
Instance Method Summary collapse
-
#fetch ⇒ Object
fetch data form server and save it to file.
-
#initialize(output, format) ⇒ DataFetcher
constructor
DATADIR = “data” DATAFILE = File.join DATADIR, “bibliography.yml” ETAGFILE = File.join DATADIR, “etag.txt”.
Constructor Details
#initialize(output, format) ⇒ DataFetcher
DATADIR = “data” DATAFILE = File.join DATADIR, “bibliography.yml” ETAGFILE = File.join DATADIR, “etag.txt”
15 16 17 18 19 |
# File 'lib/relaton_calconnect/data_fetcher.rb', line 15 def initialize(output, format) @output = output @etagfile = File.join output, "etag.txt" @format = format end |
Class Method Details
.fetch(output: "data", format: "yaml") ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/relaton_calconnect/data_fetcher.rb', line 21 def self.fetch(output: "data", format: "yaml") t1 = Time.now puts "Started at: #{t1}" FileUtils.mkdir_p output unless Dir.exist? output new(output, format).fetch t2 = Time.now puts "Stopped at: #{t2}" puts "Done in: #{(t2 - t1).round} sec." end |
Instance Method Details
#fetch ⇒ Object
fetch data form server and save it to file.
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/relaton_calconnect/data_fetcher.rb', line 34 def fetch resp = Faraday.new(ENDPOINT, headers: { "If-None-Match" => etag }).get # return if there aren't any changes since last fetching return unless resp.status == 200 data = YAML.safe_load resp.body all_success = true data["root"]["items"].each do |doc| success = parse_page doc all_success &&= success end self.etag = resp[:etag] if all_success end |