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.7.2'
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.
-
.exist?(name) ⇒ Bool
Get mod if exist.
-
.extend_uri(rel) ⇒ String
Extend relative links to absolute links.
-
.get_href(item) ⇒ Addressable::URI
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.
-
.name_from_card(card) ⇒ String
Get mod name from mod card.
-
.search(keyword, cookie = '', version: 'any') ⇒ Array<Mod>
Search mod.
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 ⇒ Addressable::URI
Get the git repo URI that can be cloned.
-
#github ⇒ Addressable::URI
Get the GitHub URI.
-
#initialize(name, cookie = '') ⇒ Mod
constructor
A new instance of Mod.
-
#latest_download(version = nil, ifnone = nil) ⇒ Download
Get the latest download of the Mod.
-
#latest_download_uri ⇒ Addressable::URI
(also: #latest_download_link)
Get latest download uri by download button in card.
-
#license ⇒ String
Get the license that the Mod use.
-
#license_uri ⇒ Addressable::URI
(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.
47 48 49 50 51 |
# File 'lib/factorio/mod/mod.rb', line 47 def self.bind_td_to_th(table_header, line) table_header.zip(line.css('td')).inject(Download.new) do |obj, pair| obj.set(*pair) end end |
.exist?(name) ⇒ Bool
Get mod if exist.
142 143 144 145 146 147 148 |
# File 'lib/factorio/mod/mod.rb', line 142 def self.exist?(name) uri = Addressable::URI.encode("mod/#{name}") URI.open(Addressable::URI.join(MOD_URI, uri)) true rescue OpenURI::HTTPError false end |
.extend_uri(rel) ⇒ String
Extend relative links to absolute links
154 155 156 157 158 |
# File 'lib/factorio/mod/mod.rb', line 154 def self.extend_uri(rel) raise NotLoginError if rel =~ %r{^/login} Addressable::URI.join(MOD_URI, rel) end |
.get_href(item) ⇒ Addressable::URI
Get href in Nokogiri node
163 164 165 |
# File 'lib/factorio/mod/mod.rb', line 163 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.
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/factorio/mod/mod.rb', line 99 def self.(username, password) csrf_token, = data = Addressable::URI.form_encode( 'csrf_token' => csrf_token, 'username' => username, 'password' => password ) Net::HTTP.post(Addressable::URI.join(MOD_URI, '/login'), data, 'Cookie' => ) \ .response['set-cookie'].split('; ').first end |
.login_cookie_webdirver(driver, autoclose = true) ⇒ String
Get the logged in cookie by webdriver.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/factorio/mod/mod.rb', line 66 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]}" \ .tap { driver.quit if autoclose } end |
.name_from_card(card) ⇒ String
Get mod name from mod card.
115 116 117 118 |
# File 'lib/factorio/mod/mod.rb', line 115 def self.name_from_card(card) card.xpath('.//h2[@class="mod-card-title"]/a/@href').text \ .split('/').last.then(&Addressable::URI.method(:unencode)) end |
.search(keyword, cookie = '', version: 'any') ⇒ Array<Mod>
Search mod.
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/factorio/mod/mod.rb', line 126 def self.search(keyword, = '', version: 'any') uri = Addressable::URI.join( MOD_URI, "/query/#{Addressable::URI.encode keyword}?version=#{version}" ) Nokogiri::HTML(URI.open(uri, 'Cookie' => )) \ .css('.mod-card').map do |card| Mod.new(name_from_card(card), ).tap do |mod| mod.instance_eval { @mod.cache[:card] = card.clone } end end end |
Instance Method Details
#author ⇒ String
Get the author of the Mod.
208 209 210 |
# File 'lib/factorio/mod/mod.rb', line 208 def @mod.any.xpath('.//div[@class="mod-card-author"]/a').text end |
#deprecated? ⇒ Bool
Whether Mod is deprecated
214 215 216 |
# File 'lib/factorio/mod/mod.rb', line 214 def deprecated? @mod.any.xpath('.//span[@class="mod-card-title-deprecated"]').any? end |
#download_list ⇒ Array<Download>
Get the download for all of version.
235 236 237 238 239 |
# File 'lib/factorio/mod/mod.rb', line 235 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 { |line| Mod.bind_td_to_th(head, line) } end |
#download_times ⇒ Integer
Get the totle number of downloads of Mod.
220 221 222 223 |
# File 'lib/factorio/mod/mod.rb', line 220 def download_times xpath = ".//span[@class='mod-card-info-tag'][@title='Downloads']/div" @mod.any.xpath(xpath).text.to_i end |
#git ⇒ Addressable::URI
Get the git repo URI that can be cloned.
189 190 191 |
# File 'lib/factorio/mod/mod.rb', line 189 def git github&.to_s&.+('.git')&.then(&Addressable::URI.method(:parse)) end |
#github ⇒ Addressable::URI
Get the GitHub URI.
182 183 184 185 |
# File 'lib/factorio/mod/mod.rb', line 182 def github @mod.any.xpath('.//a[@class="source-code-link"]/@href').text.presence \ &.then(&Addressable::URI.method(:parse)) end |
#latest_download(version = nil, ifnone = nil) ⇒ Download
Get the latest download of the Mod.
228 229 230 231 |
# File 'lib/factorio/mod/mod.rb', line 228 def latest_download(version = nil, ifnone = nil) version.blank? && download_list.first || download_list.find(ifnone) { |d| d.game_version == version.to_s } end |
#latest_download_uri ⇒ Addressable::URI Also known as: latest_download_link
Get latest download uri by download button in card
243 244 245 |
# File 'lib/factorio/mod/mod.rb', line 243 def latest_download_uri Mod.get_href(@mod.any.css('.mod-download-button')) end |
#license ⇒ String
Get the license that the Mod use.
195 196 197 |
# File 'lib/factorio/mod/mod.rb', line 195 def license data_table('License').xpath('a').text end |
#license_uri ⇒ Addressable::URI Also known as: license_link
Get the license URI.
201 202 203 |
# File 'lib/factorio/mod/mod.rb', line 201 def license_uri Addressable::URI.parse(data_table('License').xpath('a/@href').text) end |
#summary ⇒ String
Get the summary of the Mod
176 177 178 |
# File 'lib/factorio/mod/mod.rb', line 176 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
169 170 171 |
# File 'lib/factorio/mod/mod.rb', line 169 def title @mod.any.css('.mod-card-title *').text end |