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(uri, cookie = '') ⇒ Cache

Returns a new instance of Cache.

Parameters:

  • uri (String)

    Mod URI like https://mods.factorio.com/mod/LTN-easier



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

def initialize(uri, cookie = '')
  @uri = uri
  @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)


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

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)


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

def downloads
  get :downloads, "#{@uri}/downloads"
end

#informationNokogiri::HTML

Get the Information page.

Returns:

  • (Nokogiri::HTML)


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

def information
  get :information, @uri
end