class Factorio::Mod
Info interface
example:
“` mod = Factorio::Mod.new
'LTN-easier' puts “The author of #{mod.title} is #{mod.author}” puts “The #{mod.title} is under #{mod.license}” puts “It latest version can download at #{mod.latest_download}” puts “It latest version for factorio 0.16 can download at ” \
"#{mod.latest_download('0.16')[:uri]}"
“`
Constants
- API_URI
- Download
store the entry of the download
- MOD_URI
factorio
Mod
portal website URI- VERSION
Attributes
Public Class Methods
Get mod if exist.
@param name [String] name of the mod @return [Bool] is exist
# File lib/factorio/mod/mod.rb, line 81 def self.exist?(name) name = Addressable::URI.encode(name) URI.open(API_URI % name) true rescue OpenURI::HTTPError false end
Extend relative links to absolute links @param rel [String] relative link @return [String] absolute link @raise [NotLoginError]
# File lib/factorio/mod/mod.rb, line 93 def self.extend_uri(rel) Addressable::URI.join(MOD_URI, rel) end
Get the log in token by username and password.
@example
print 'username: ' user = gets.chomp print 'password: ' pass = gets.chomp token = Factorio::Mod.login user, pass
@param username [String] @param password [String] @return [String] The log in token
# File lib/factorio/mod/mod.rb, line 42 def self.login(username, password) data = Addressable::URI.form_encode( 'username' => username, 'password' => password, ) result = Net::HTTP.post('https://auth.factorio.com/api-login', data) .then(JSON.method(:parse)) result.first rescue OpenURI::HTTPError raise result['message'] end
Get mod name from mod card.
@param card [Nokogiri::HTML] @return [String] The name in
# File lib/factorio/mod/mod.rb, line 58 def self.name_from_card(card) card.xpath('.//h2[@class="mod-card-title"]/a/@href').text \ .split('/').last.then(&Addressable::URI.method(:unencode)) end
@param name [String] name of the Mod
# File lib/factorio/mod/mod.rb, line 26 def initialize(name) @name = name @mod = Cache.new name end
Search mod.
@param keyword [String] search keyword @param version [String] factorio version, `any` for any version @return [Array<Mod>] all mod searched on the first page with card cache
# File lib/factorio/mod/mod.rb, line 68 def self.search(keyword, version: 'any') uri = Addressable::URI.join( MOD_URI, "/query/#{Addressable::URI.encode keyword}?version=#{version}" ) Nokogiri::HTML(URI.open(uri)).css('.mod-card').map do |card| Mod.new(name_from_card(card)) end end
Public Instance Methods
Whether Mod
is deprecated @return [Boolean] is deprecated
# File lib/factorio/mod/mod.rb, line 146 def deprecated? @mod.get[:deprecated] || false end
Get the download for all of version. @return [Array<Download>]
# File lib/factorio/mod/mod.rb, line 167 def download_list @mod.get[:releases].dup.map(&Download.method(:create)) end
Get the totle number of downloads of Mod
. @return [Integer] number of downloads
# File lib/factorio/mod/mod.rb, line 152 def download_times @mod.get[:downloads_count] end
Get the git repo URI that can be cloned. @return [Addressable::URI] git URI
# File lib/factorio/mod/mod.rb, line 120 def git github&.to_s&.+('.git')&.then(&Addressable::URI.method(:parse)) end
Get the GitHub URI. @return [Addressable::URI] GitHub URI
# File lib/factorio/mod/mod.rb, line 112 def github @mod.get[:github_path].presence&.then do Addressable::URI.join('https://github.com', _1) end end
Get the latest download of the Mod
. @param version [String] factorio version @return [Download]
# File lib/factorio/mod/mod.rb, line 160 def latest_download(version = nil, ifnone = nil) version.blank? && download_list.last || download_list.reverse.find(ifnone) { _1.game_version == version.to_s } end
Get latest download uri by download button in card @return [Addressable::URI] download uri
# File lib/factorio/mod/mod.rb, line 173 def latest_download_uri Mod.extend_uri(latest_download[:download_url]) end
Get the license that the Mod
use. @return [String] license name
# File lib/factorio/mod/mod.rb, line 126 def license @mod.get(:license, :name) end
Get the license URI. @return [Addressable::URI] license URI
# File lib/factorio/mod/mod.rb, line 132 def license_uri Addressable::URI.parse(@mod.get(:license, :url)) end
Get the summary of the Mod
@return [String] summary
# File lib/factorio/mod/mod.rb, line 106 def summary @mod.get[:summary] end
Get the title (also known as _display name_) of the Mod
@return [String] title
# File lib/factorio/mod/mod.rb, line 99 def title @mod.get[:title] end