Class: Factorio::Mod
- Inherits:
-
Object
- Object
- Factorio::Mod
- 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
-
#name ⇒ Object
(also: #id, #string_id)
readonly
Get the mod name.
Class Method Summary collapse
-
.bind_td_to_th(table_head, line) ⇒ Object
rubocop:disable MethodLength.
-
.login_cookie(username, password) ⇒ String
Get logged in cookie.
Instance Method Summary collapse
-
#author ⇒ String
Get author.
-
#download_list ⇒ Array<Hash>
Get all version info.
-
#download_times ⇒ Integer
Get download times.
-
#git ⇒ String
Get git repo uri.
-
#github ⇒ String
Get github uri.
-
#initialize(name, cookie = '') ⇒ Mod
constructor
A new instance of Mod.
-
#latest_download(version = '') ⇒ Hash
Ge latest download.
-
#license ⇒ String
Get license.
-
#license_uri ⇒ String
Get license uri.
-
#summary ⇒ String
Get the summary of MOD.
-
#title ⇒ String
(also: #display_name)
Get the title (or display name) of MOD.
Constructor Details
#initialize(name, cookie = '') ⇒ Mod
Returns a new instance of Mod.
21 22 23 24 |
# File 'lib/factorio/mod/mod.rb', line 21 def initialize(name, = '') @name = name @mod = Cache.new "#{MOD_URI}/mod/#{ERB::Util.url_encode @name}", end |
Instance Attribute Details
#name ⇒ Object (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 |
.login_cookie(username, password) ⇒ String
Get logged in 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, = data = URI.encode_www_form( 'csrf_token' => csrf_token, 'username' => username, 'password' => password ) resp = Net::HTTP.post(URI(MOD_URI + '/login'), data, 'Cookie' => ) resp.response['set-cookie'].split('; ')[0] end |
Instance Method Details
#author ⇒ String
Get author
119 120 121 |
# File 'lib/factorio/mod/mod.rb', line 119 def data_table('Owner').xpath('a').text end |
#download_list ⇒ Array<Hash>
Get all version info
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_times ⇒ Integer
Get 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 |
#git ⇒ String
Get git repo uri
100 101 102 103 |
# File 'lib/factorio/mod/mod.rb', line 100 def git return if github.nil? github + '.git' end |
#github ⇒ String
Get 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
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 |
#license ⇒ String
Get license
107 108 109 |
# File 'lib/factorio/mod/mod.rb', line 107 def license data_table('License').xpath('a').text end |
#license_uri ⇒ String
Get 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 |
#summary ⇒ String
Get the summary of MOD
86 87 88 |
# File 'lib/factorio/mod/mod.rb', line 86 def summary @mod.any.css('.mod-card-summary').text end |
#title ⇒ String Also known as: display_name
Get the title (or display name) of MOD
79 80 81 |
# File 'lib/factorio/mod/mod.rb', line 79 def title @mod.any.css('.mod-card-title *').text end |