Module: Datajud::KeyFetcher

Defined in:
lib/datajud/key_fetcher.rb

Constant Summary collapse

ACCESS_URL =
"https://datajud-wiki.cnj.jus.br/api-publica/acesso/"
CACHE_PATH =
File.join(Dir.home, ".datajud_api_key")

Class Method Summary collapse

Class Method Details

.clear_cacheObject



29
30
31
# File 'lib/datajud/key_fetcher.rb', line 29

def self.clear_cache
  FileUtils.rm_f(CACHE_PATH)
end

.fetch_api_keyObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/datajud/key_fetcher.rb', line 12

def self.fetch_api_key
  # Tenta carregar do cache
  if File.exist?(CACHE_PATH)
    key = File.read(CACHE_PATH).strip
    return key unless key.empty?
  end

  html = Net::HTTP.get(URI.parse(ACCESS_URL))
  doc = Nokogiri::HTML.parse(html)
  key = doc.text[/Authorization:\s*APIKey\s*([A-Za-z0-9+\/]+=+)/, 1]
  raise "API Key not found on Datajud-Wiki page!" unless key

  # Salva em cache
  File.write(CACHE_PATH, key)
  key
end