Class: Relaton::Calconnect::DataFetcher

Inherits:
Relaton::Core::DataFetcher
  • Object
show all
Defined in:
lib/relaton/calconnect/data_fetcher.rb

Overview

Relaton-calconnect data fetcher

Constant Summary collapse

ENDPOINT =
"https://standards.calconnect.org/cc/index.json"

Instance Method Summary collapse

Instance Method Details

#agentObject



29
30
31
# File 'lib/relaton/calconnect/data_fetcher.rb', line 29

def agent
  @agent ||= Mechanize.new
end

#etagfileObject



17
18
19
# File 'lib/relaton/calconnect/data_fetcher.rb', line 17

def etagfile
  @etagfile ||= File.join @output, "etag.txt"
end

#fetch(_source = nil) ⇒ Object

fetch data form server and save it to file.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/relaton/calconnect/data_fetcher.rb', line 36

def fetch(_source = nil) # rubocop:disable Metrics/AbcSize
  agent.request_headers["If-None-Match"] = etag if etag
  resp = agent.get(ENDPOINT)
  # 304 Not Modified — nothing changed since the last fetch
  return if resp.code == "304"

  data = JSON.parse resp.body
  all_success = true
  Array(data["documents"]).each { |doc| all_success &&= parse_page doc }
  self.etag = resp.response["etag"] if all_success
  index.save
  report_errors
end

#indexObject



21
22
23
# File 'lib/relaton/calconnect/data_fetcher.rb', line 21

def index
  @index = Relaton::Index.find_or_create :CC, file: "index-v1.yaml"
end

#log_error(msg) ⇒ Object



25
26
27
# File 'lib/relaton/calconnect/data_fetcher.rb', line 25

def log_error(msg)
  Util.error msg
end