Module: RelatonOgc::DataFetcher::Utils

Included in:
RelatonOgc::DataFetcher
Defined in:
lib/relaton_ogc/data_fetcher.rb

Constant Summary collapse

ENDPOINT =
"https://raw.githubusercontent.com/opengeospatial/NamingAuthority/master/definitions/docs/docs.json"

Instance Method Summary collapse

Instance Method Details

#etagString, NilClass

Read ETag form file

Returns:

  • (String, NilClass)


25
26
27
28
29
# File 'lib/relaton_ogc/data_fetcher.rb', line 25

def etag
  @etag ||= if File.exist? @etagfile
              File.read @etagfile, encoding: "UTF-8"
            end
end

#etag=(e_tag) ⇒ Object

Save ETag to file

Parameters:

  • tag (String)


35
36
37
# File 'lib/relaton_ogc/data_fetcher.rb', line 35

def etag=(e_tag)
  File.write @etagfile, e_tag, encoding: "UTF-8"
end

#get_dataObject

rubocop:disable Metrics/AbcSize



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/relaton_ogc/data_fetcher.rb', line 8

def get_data # rubocop:disable Metrics/AbcSize
  h = {}
  h["If-None-Match"] = etag if etag
  resp = Faraday.new(ENDPOINT, headers: h).get
  case resp.status
  when 200
    json = JSON.parse(resp.body)
    block_given? ? yield(resp[:etag], json) : json
  when 304 then [] # there aren't any changes since last fetching
  else raise RelatonBib::RequestError, "Could not access #{ENDPOINT}"
  end
end