Class: Factorio::Cache
- Inherits:
-
Object
- Object
- Factorio::Cache
- Defined in:
- lib/factorio/mod/cache.rb
Overview
Cache mod info
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
Instance Method Summary collapse
-
#get(*key) ⇒ Hash
Get from cache or download.
-
#initialize(name) ⇒ Cache
constructor
A new instance of Cache.
Constructor Details
#initialize(name) ⇒ Cache
Returns a new instance of Cache.
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
#cache ⇒ Object (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
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 |