# frozen_string_literal: true require_relative 'nuva/version' require_relative 'nuva/NuvaDatabase_pb' require_relative 'nuva/repository' require_relative 'nuva/queries' require 'net/http' require 'json' require 'ostruct' module Nuva class Nuva attr_reader :repositories attr_reader :queries def initialize(db) @db = db @repositories = OpenStruct.new @repositories.vaccines = VaccineRepository.new db.vaccines @repositories.valences = ValenceRepository.new db.valences @repositories.diseases = DiseaseRepository.new db.diseases @repositories.freeze @queries = OpenStruct.new @queries.valences_by_vaccine = ::Nuva::Queries::ValencesByVaccine.new @repositories @queries.vaccines_by_valence = ::Nuva::Queries::VaccinesByValence.new @repositories @queries.vaccines_by_disease = ::Nuva::Queries::VaccinesByDisease.new @repositories @queries.valences_by_disease = ::Nuva::Queries::ValencesByDisease.new @repositories @queries.diseases_by_vaccine = ::Nuva::Queries::DiseasesByVaccine.new @repositories @queries.diseases_by_valence = ::Nuva::Queries::DiseasesByValence.new @repositories @queries.vaccine_fuzzy_search = ::Nuva::Queries::VaccineFuzzySearch.new @repositories @queries.lookup_vaccine_by_code = ::Nuva::Queries::LookupVaccineByCode.new @repositories @queries.lookup_equivalent_vaccines = ::Nuva::Queries::LookupEquivalentVaccines.new @repositories @queries.lookup_generalized_vaccines = ::Nuva::Queries::LookupGeneralizedVaccines.new @repositories, @queries.valences_by_vaccine @queries.all_nomenclatures = ::Nuva::Queries::AllNomenclatures.new @repositories @queries.all_code_by_nomenclature = ::Nuva::Queries::AllCodeByNomenclature.new @repositories @queries.freeze end def version @db.version end def self.load(lang = "en") latest_version_uri = URI "https://cdnnuva.mesvaccins.net/versions/last.json" ::Net::HTTP.start(latest_version_uri.host, latest_version_uri.port, use_ssl: latest_version_uri.scheme == 'https') do |http| # Fetch version manifest req = ::Net::HTTP::Get.new latest_version_uri res = http.request req res.value manifest = JSON.parse(res.body) dump_hash = manifest['dump_hash'] # Fetch latest database dump_uri = latest_version_uri.dup dump_uri.path = "/proto/#{dump_hash}_#{lang}.db" req = ::Net::HTTP::Get.new dump_uri res = http.request req res.value # The return value of the block is returned by the start method ::Nuva::Nuva.new(::Nuva::NuvaDatabase.decode(res.body)) end end def self.load_from_file(path) Nuva.new(NuvaDatabase.decode(File.read(path))) end end end