Class: Factorio::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/factorio/mod/cache.rb

Overview

Cache mod info

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Cache

Returns a new instance of Cache.

Parameters:

  • name (String)

    Mod name



9
10
11
12
13
# File 'lib/factorio/mod/cache.rb', line 9

def initialize(name)
  @name = name
  @uri = Mod::API_URI % Addressable::URI.encode(@name)
  @cache = nil
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



6
7
8
# File 'lib/factorio/mod/cache.rb', line 6

def cache
  @cache
end

Instance Method Details

#get(*key) ⇒ Hash

Get from cache or download

Parameters:

  • key (Symbol)

Returns:

  • (Hash)


18
19
20
21
22
23
24
25
26
# 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