lib/nuva.rb in nuva-0.3.1 vs lib/nuva.rb in nuva-0.4.0

- old
+ new

@@ -53,40 +53,35 @@ def version @db.version end - def self.load(lang = "en") - latest_version_uri = - URI "https://cdnnuva.mesvaccins.net/versions/last.json" + def inspect + "#<Nuva::Nuva version=#{version.major}.#{version.minor}.#{version.patch} diseaseCount=#{repositories.diseases.all.count} vaccineCount=#{repositories.vaccines.all.count} valenceCount=#{repositories.valences.all.count}>" + end + LATEST_VERSION_URI = + URI("https://cdnnuva.mesvaccins.net/versions/last.json") + + def self.load(lang: "en", hash: nil) ::Net::HTTP.start( - latest_version_uri.host, - latest_version_uri.port, - use_ssl: latest_version_uri.scheme == "https" + 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 + # Fetch version manifest if necessary + hash ||= fetch_latest_version_hash(http:) - 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)) + dump_uri = LATEST_VERSION_URI.dup + dump_uri.path = "/proto/#{hash}_en.db" + response = http.get(dump_uri) + ::Nuva::Nuva.new(::Nuva::NuvaDatabase.decode(response.body)) end + end + + def self.fetch_latest_version_hash(http: Net::HTTP) + response = http.get(LATEST_VERSION_URI) + JSON.parse(response.body)["dump_hash"] end def self.load_from_file(path) Nuva.new(NuvaDatabase.decode(File.read(path))) end