Module: Restiny::Api::Manifest

Includes:
Base
Included in:
Restiny
Defined in:
lib/restiny/api/manifest.rb

Instance Method Summary collapse

Methods included from Base

#api_get, #api_post

Instance Method Details

#download_manifest(locale: 'en', force_download: false) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/restiny/api/manifest.rb', line 16

def download_manifest(locale: 'en', force_download: false)
  result = api_get(endpoint: 'Destiny2/Manifest/')
  raise Restiny::ResponseError, 'Unable to determine manifest details' if result.nil?

  return manifests[locale] if !force_download && manifest_version?(locale, result['version'])

  manifests[locale] = download_manifest_by_url(result.dig('mobileWorldContentPaths', locale), result['version'])
end

#download_manifest_by_url(url, version) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/restiny/api/manifest.rb', line 25

def download_manifest_by_url(url, version)
  raise Restiny::RequestError, 'Unknown locale' if url.nil?

  database_file_path = extract_manifest_from_zip_file(Down.download(BUNGIE_URL + url), version)

  Restiny::Manifest.new(database_file_path, version)
rescue Down::Error => e
  raise Restiny::NetworkError.new('Unable to download the manifest file', e.response.code)
end

#extract_manifest_from_zip_file(source_path, version) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/restiny/api/manifest.rb', line 35

def extract_manifest_from_zip_file(source_path, version)
  Zip::File.open(source_path) do |zip_file|
    File.join(Dir.tmpdir, "#{version}.en.content.db").tap do |path|
      zip_file.first.extract(path) unless File.exist?(path)
    end
  end
rescue Zip::Error => e
  raise Restiny::Error, "Unable to unzip the manifest file (#{e})"
end

#manifest_version?(locale, version) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/restiny/api/manifest.rb', line 49

def manifest_version?(locale, version)
  manifests[locale] && manifests[locale].version == version
end

#manifestsObject



45
46
47
# File 'lib/restiny/api/manifest.rb', line 45

def manifests
  @manifests ||= {}
end