Class: Factorio::Mod

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

Overview

Main class

Constant Summary collapse

MOD_URI =
'https://mods.factorio.com'.freeze
VERSION =
'0.2.2'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Mod.

Parameters:

  • name (String)

    name of mod

  • cookie (String) (defaults to: '')

    logged in cookie



21
22
23
24
# File 'lib/factorio/mod/mod.rb', line 21

def initialize(name, cookie = '')
  @name = name
  @mod = Cache.new "#{MOD_URI}/mod/#{ERB::Util.url_encode @name}", cookie
end

Instance Attribute Details

#nameObject (readonly) Also known as: id, string_id

Get the mod name



15
16
17
# File 'lib/factorio/mod/mod.rb', line 15

def name
  @name
end

Class Method Details

.bind_td_to_th(table_head, line) ⇒ Object

rubocop:disable MethodLength



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/factorio/mod/mod.rb', line 47

def self.bind_td_to_th(table_head, line)
  line.css('td').each_with_index.each_with_object({}) \
    do |(item, index), obj|
    key = table_head[index]
    if key == 'Download'
      href = item.xpath('a/@href').to_s
      raise NotLoginError if href =~ %r{^/login}
      obj[:uri] = MOD_URI + href
    else
      obj[TABLE_HEAD_TO_GOOD_NAME[key]] = item.text
    end
  end
end

Get logged in cookie

Parameters:

  • username (String)
  • password (String)

Returns:

  • (String)

    cookie



66
67
68
69
70
71
72
73
74
75
# File 'lib/factorio/mod/mod.rb', line 66

def self.(username, password)
  csrf_token, cookie = csrf_and_cookie
  data = URI.encode_www_form(
    'csrf_token' => csrf_token,
    'username'   => username,
    'password'   => password
  )
  resp = Net::HTTP.post(URI(MOD_URI + '/login'), data, 'Cookie' => cookie)
  resp.response['set-cookie'].split('; ')[0]
end

Instance Method Details

#authorString

Get author

Returns:

  • (String)

    author



119
120
121
# File 'lib/factorio/mod/mod.rb', line 119

def author
  data_table('Owner').xpath('a').text
end

#download_listArray<Hash>

Get all version info

Returns:

  • (Array<Hash>)

    with version game_version date download_times uri



143
144
145
146
147
148
149
# File 'lib/factorio/mod/mod.rb', line 143

def download_list
  table = @mod.download.css 'table.mod-page-downloads-table'
  head = table.css('thead tr th').map(&:text)
  table.css('tbody tr').map do |line|
    Mod.bind_td_to_th head, line
  end
end

#download_timesInteger

Get download times

Returns:

  • (Integer)

    download times



125
126
127
128
# File 'lib/factorio/mod/mod.rb', line 125

def download_times
  xpath = "//span[@class='mod-card-info-tag'][@title='Downloads']/div"
  @mod.any.xpath(xpath).text.to_i
end

#gitString

Get git repo uri

Returns:

  • (String)

    git uri



100
101
102
103
# File 'lib/factorio/mod/mod.rb', line 100

def git
  return if github.nil?
  github + '.git'
end

#githubString

Get github uri

Returns:

  • (String)

    github uri



92
93
94
95
96
# File 'lib/factorio/mod/mod.rb', line 92

def github
  uri = data_table('Source').xpath('a/@href').to_s
  return if uri.empty?
  uri
end

#latest_download(version = '') ⇒ Hash

Ge latest download

Parameters:

  • version (String) (defaults to: '')

    factorio version

Returns:

  • (Hash)

    with version game_version date download_times uri

Raises:



133
134
135
136
137
138
139
# File 'lib/factorio/mod/mod.rb', line 133

def latest_download(version = '')
  return download_list.last if version == ''
  download_list.each do |download|
    return download if download[:game_version] == version.to_s
  end
  raise NoDownloadError, "Can't found #{name} for factorio #{version}"
end

#licenseString

Get license

Returns:

  • (String)

    license name



107
108
109
# File 'lib/factorio/mod/mod.rb', line 107

def license
  data_table('License').xpath('a').text
end

#license_uriString

Get license uri

Returns:

  • (String)

    license uri



113
114
115
# File 'lib/factorio/mod/mod.rb', line 113

def license_uri
  data_table('License').xpath('a/@href').to_s
end

#summaryString

Get the summary of MOD

Returns:

  • (String)

    mod summary



86
87
88
# File 'lib/factorio/mod/mod.rb', line 86

def summary
  @mod.any.css('.mod-card-summary').text
end

#titleString Also known as: display_name

Get the title (or display name) of MOD

Returns:

  • (String)

    mod title



79
80
81
# File 'lib/factorio/mod/mod.rb', line 79

def title
  @mod.any.css('.mod-card-title *').text
end