Class: Factorio::Mod
- Inherits:
-
Object
- Object
- Factorio::Mod
- Defined in:
- lib/factorio/mod/mod.rb,
lib/factorio/mod/version.rb,
lib/factorio/mod/download.rb
Overview
Info interface
example:
mod = Factorio::Mod.new 'LTN-easier'
puts "The author of #{mod.title} is #{mod.}"
puts "The #{mod.title} is under #{mod.license}"
puts "It latest version can download at #{mod.latest_download[:uri]}"
puts "It latest version for factorio 0.16 can download at " \
"#{mod.latest_download('0.16')[:uri]}"
Defined Under Namespace
Classes: Download
Constant Summary collapse
- MOD_URI =
factorio Mod portal website URI
'https://mods.factorio.com'- VERSION =
'0.5.0'
Instance Attribute Summary collapse
-
#name ⇒ Object
(also: #id, #string_id)
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.bind_td_to_th(table_header, line) ⇒ Download
Map each value with table header.
-
.extend_uri(rel) ⇒ String
Extend relative links to absolute links.
-
.get_href(item) ⇒ String
Get href in Nokogiri node.
-
.login_cookie(username, password) ⇒ String
Get the logged in cookie by username and password.
-
.login_cookie_webdirver(driver, autoclose = true) ⇒ String
Get the logged in cookie by webdriver.
Instance Method Summary collapse
-
#author ⇒ String
Get the author of the Mod.
-
#deprecated? ⇒ Bool
Whether Mod is deprecated.
-
#download_list ⇒ Array<Download>
Get the download for all of version.
-
#download_times ⇒ Integer
Get the totle number of downloads of Mod.
-
#git ⇒ String
Get the git repo URI that can be cloned.
-
#github ⇒ String
Get the GitHub URI.
-
#initialize(name, cookie = '') ⇒ Mod
constructor
A new instance of Mod.
-
#latest_download(version = nil) ⇒ Download
Get the latest download of the Mod.
-
#latest_download_uri ⇒ String
(also: #latest_download_link)
Get latest download uri by download button in card.
-
#license ⇒ String
Get the license that the Mod use.
-
#license_uri ⇒ String
(also: #license_link)
Get the license URI.
-
#summary ⇒ String
Get the summary of the Mod.
-
#title ⇒ String
(also: #display_name)
Get the title (also known as display name) of the Mod.
Constructor Details
Instance Attribute Details
#name ⇒ Object (readonly) Also known as: id, string_id
Returns the value of attribute name.
20 21 22 |
# File 'lib/factorio/mod/mod.rb', line 20 def name @name end |
Class Method Details
.bind_td_to_th(table_header, line) ⇒ Download
Map each value with table header.
48 49 50 51 52 53 54 |
# File 'lib/factorio/mod/mod.rb', line 48 def self.bind_td_to_th(table_header, line) line.css('td').each_with_index.each_with_object(Download.new) \ do |(item, index), obj| key = table_header[index] obj.set(key, item) end end |
.extend_uri(rel) ⇒ String
Extend relative links to absolute links
118 119 120 121 122 |
# File 'lib/factorio/mod/mod.rb', line 118 def self.extend_uri(rel) raise NotLoginError if rel =~ %r{^/login} MOD_URI + rel end |
.get_href(item) ⇒ String
Get href in Nokogiri node
127 128 129 |
# File 'lib/factorio/mod/mod.rb', line 127 def self.get_href(item) extend_uri(item.xpath('a/@href').text) end |
.login_cookie(username, password) ⇒ String
Get the logged in cookie by username and password.
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/factorio/mod/mod.rb', line 103 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('; ').first end |
.login_cookie_webdirver(driver, autoclose = true) ⇒ String
Get the logged in cookie by webdriver.
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/factorio/mod/mod.rb', line 69 def self.(driver, autoclose = true) begin driver.navigate.to 'https://mods.factorio.com/login' rescue Net::ReadTimeout retry end sleep(1) while driver.current_url == 'https://mods.factorio.com/login' session = driver.manage.('session')[:value] driver.quit if autoclose "session=#{session}" end |
Instance Method Details
#author ⇒ String
Get the author of the Mod.
171 172 173 |
# File 'lib/factorio/mod/mod.rb', line 171 def @mod.any.xpath('//div[@class="mod-card-author"]/a').text end |
#deprecated? ⇒ Bool
Whether Mod is deprecated
177 178 179 |
# File 'lib/factorio/mod/mod.rb', line 177 def deprecated? !@mod.any.xpath('//span[@class="mod-card-title-deprecated"]').empty? end |
#download_list ⇒ Array<Download>
Get the download for all of version.
202 203 204 205 206 207 208 |
# File 'lib/factorio/mod/mod.rb', line 202 def download_list table = @mod.downloads.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 the totle number of downloads of Mod.
183 184 185 186 |
# File 'lib/factorio/mod/mod.rb', line 183 def download_times xpath = "//span[@class='mod-card-info-tag'][@title='Downloads']/div" @mod.any.xpath(xpath).text.to_i end |
#git ⇒ String
Get the git repo URI that can be cloned.
152 153 154 |
# File 'lib/factorio/mod/mod.rb', line 152 def git github &.+ '.git' end |
#github ⇒ String
Get the GitHub URI.
146 147 148 |
# File 'lib/factorio/mod/mod.rb', line 146 def github @mod.any.xpath('//a[@class="source-code-link"]/@href').text.presence end |
#latest_download(version = nil) ⇒ Download
Get the latest download of the Mod.
191 192 193 194 195 196 197 198 |
# File 'lib/factorio/mod/mod.rb', line 191 def latest_download(version = nil) return download_list.last if version.blank? 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 |
#latest_download_uri ⇒ String Also known as: latest_download_link
Get latest download uri by download button in card
212 213 214 |
# File 'lib/factorio/mod/mod.rb', line 212 def latest_download_uri Mod.get_href(@mod.any.css('.mod-download-button')) end |
#license ⇒ String
Get the license that the Mod use.
158 159 160 |
# File 'lib/factorio/mod/mod.rb', line 158 def license data_table('License').xpath('a').text end |
#license_uri ⇒ String Also known as: license_link
Get the license URI.
164 165 166 |
# File 'lib/factorio/mod/mod.rb', line 164 def license_uri data_table('License').xpath('a/@href').to_s end |
#summary ⇒ String
Get the summary of the Mod
140 141 142 |
# File 'lib/factorio/mod/mod.rb', line 140 def summary @mod.any.css('.mod-card-summary').text end |
#title ⇒ String Also known as: display_name
Get the title (also known as display name) of the Mod
133 134 135 |
# File 'lib/factorio/mod/mod.rb', line 133 def title @mod.any.css('.mod-card-title *').text end |