class Factorio::Cache

Cache mod info

Attributes

cache[R]

Public Class Methods

new(name) click to toggle source

@param name [String] Mod name

# File lib/factorio/mod/cache.rb, line 9
def initialize(name)
  @name = name
  @uri = Mod::API_URI % Addressable::URI.encode(@name)
  @cache = nil
end

Public Instance Methods

get(*key) click to toggle source

Get from cache or download @param key [Symbol] @return [Hash]

# File lib/factorio/mod/cache.rb, line 18
def get(*key)
  @cache ||= URI.open(@uri)
                .then(&Nokogiri.method(:HTML))
                .then { JSON.parse(_1, symbolize_names: true) }
                .then(&method(:deep_freeze)) # rubocop:disable Layout/MultilineMethodCallIndentation
  key.empty? ? @cache : @cache.dig(*key.map(&:to_sym))
rescue OpenURI::HTTPError => e
  raise NoModError.new "MOD #{@name} not found", e.io
end

Private Instance Methods

deep_freeze(sth) click to toggle source
# File lib/factorio/mod/cache.rb, line 30
def deep_freeze(sth)
  case sth
  when Hash then sth.each_value(&method(:deep_freeze))
  when Array then sth.each(&method(:deep_freeze))
  end
  sth.freeze
end