Class: Factorio::Cache

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

Overview

Cache mod info

Constant Summary collapse

PAGES =
%i[information downloads].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, cookie = '') ⇒ Cache

Returns a new instance of Cache.

Parameters:

  • name (String)

    Mod name



11
12
13
14
15
16
# File 'lib/factorio/mod/cache.rb', line 11

def initialize(name, cookie = '')
  @name = name
  @uri = Addressable::URI.encode("mod/#{@name}")
  @cookie = cookie
  @cache = {}
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



8
9
10
# File 'lib/factorio/mod/cache.rb', line 8

def cache
  @cache
end

Instance Method Details

#any(better = :information) ⇒ Nokogiri::HTML

Get any page that already downloads. Get better(default information) mod page if none of the pages exist.

Parameters:

  • better (Symbol) (defaults to: :information)

    get which page if none of the pages exist.

Returns:

  • (Nokogiri::HTML)


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/factorio/mod/cache.rb', line 34

def any(better = :information)
  unless PAGES.include? better
    raise NoPageError, "page #{better.to_s.capitalize} not exist"
  end

  if @cache.key? better
    @cache[better]
  elsif @cache.any?
    @cache.first.second
  else
    public_method(better).call
  end
end

#downloadsNokogiri::HTML

Get the Downloads page.

Returns:

  • (Nokogiri::HTML)


26
27
28
# File 'lib/factorio/mod/cache.rb', line 26

def downloads
  get :downloads, Addressable::URI.join(Mod::MOD_URI, "#{@uri}/downloads")
end

#informationNokogiri::HTML

Get the Information page.

Returns:

  • (Nokogiri::HTML)


20
21
22
# File 'lib/factorio/mod/cache.rb', line 20

def information
  get :information, Addressable::URI.join(Mod::MOD_URI, @uri)
end