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.author}"
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'- API_URI =
'https://mods.factorio.com/api/mods/%s/full'- VERSION =
'0.8.0'
Instance Attribute Summary collapse
-
#name ⇒ Object
(also: #id, #string_id)
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.exist?(name) ⇒ Bool
Get mod if exist.
-
.extend_uri(rel) ⇒ String
Extend relative links to absolute links.
-
.login(username, password) ⇒ String
Get the log in token by username and password.
-
.name_from_card(card) ⇒ String
Get mod name from mod card.
-
.search(keyword, version: 'any') ⇒ Array<Mod>
Search mod.
Instance Method Summary collapse
-
#author ⇒ String
(also: #owner)
Get the author of the Mod.
-
#deprecated? ⇒ Boolean
Whether Mod is deprecated.
-
#download_list ⇒ Array<Download>
Get the download for all of version.
-
#download_times ⇒ Integer
(also: #downloads_count)
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) ⇒ 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
#initialize(name) ⇒ Mod
Returns a new instance of Mod.
26 27 28 29 |
# File 'lib/factorio/mod/mod.rb', line 26 def initialize(name) @name = name @mod = Cache.new name end |
Instance Attribute Details
#name ⇒ Object (readonly) Also known as: id, string_id
Returns the value of attribute name.
21 22 23 |
# File 'lib/factorio/mod/mod.rb', line 21 def name @name end |
Class Method Details
.exist?(name) ⇒ Bool
Get mod if exist.
81 82 83 84 85 86 87 |
# 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_uri(rel) ⇒ String
Extend relative links to absolute links
93 94 95 |
# File 'lib/factorio/mod/mod.rb', line 93 def self.extend_uri(rel) Addressable::URI.join(MOD_URI, rel) end |
.login(username, password) ⇒ String
Get the log in token by username and password.
42 43 44 45 46 47 48 49 50 51 52 |
# 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 |
.name_from_card(card) ⇒ String
Get mod name from mod card.
58 59 60 61 |
# 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 |
.search(keyword, version: 'any') ⇒ Array<Mod>
Search mod.
68 69 70 71 72 73 74 75 |
# 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 |
Instance Method Details
#author ⇒ String Also known as: owner
Get the author of the Mod.
139 140 141 |
# File 'lib/factorio/mod/mod.rb', line 139 def @mod.get[:owner] end |
#deprecated? ⇒ Boolean
Whether Mod is deprecated
146 147 148 |
# File 'lib/factorio/mod/mod.rb', line 146 def deprecated? @mod.get[:deprecated] || false end |
#download_list ⇒ Array<Download>
Get the download for all of version.
167 168 169 |
# File 'lib/factorio/mod/mod.rb', line 167 def download_list @mod.get[:releases].dup.map(&Download.method(:create)) end |
#download_times ⇒ Integer Also known as: downloads_count
Get the totle number of downloads of Mod.
152 153 154 |
# File 'lib/factorio/mod/mod.rb', line 152 def download_times @mod.get[:downloads_count] end |
#git ⇒ Addressable::URI
Get the git repo URI that can be cloned.
120 121 122 |
# File 'lib/factorio/mod/mod.rb', line 120 def git github&.to_s&.+('.git')&.then(&Addressable::URI.method(:parse)) end |
#github ⇒ Addressable::URI
Get the GitHub URI.
112 113 114 115 116 |
# 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 |
#latest_download(version = nil, ifnone = nil) ⇒ Download
Get the latest download of the Mod.
160 161 162 163 |
# 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 |
#latest_download_uri ⇒ Addressable::URI Also known as: latest_download_link
Get latest download uri by download button in card
173 174 175 |
# File 'lib/factorio/mod/mod.rb', line 173 def latest_download_uri Mod.extend_uri(latest_download[:download_url]) end |
#license ⇒ String
Get the license that the Mod use.
126 127 128 |
# File 'lib/factorio/mod/mod.rb', line 126 def license @mod.get(:license, :name) end |
#license_uri ⇒ Addressable::URI Also known as: license_link
Get the license URI.
132 133 134 |
# File 'lib/factorio/mod/mod.rb', line 132 def license_uri Addressable::URI.parse(@mod.get(:license, :url)) end |
#summary ⇒ String
Get the summary of the Mod
106 107 108 |
# File 'lib/factorio/mod/mod.rb', line 106 def summary @mod.get[:summary] end |
#title ⇒ String Also known as: display_name
Get the title (also known as display name) of the Mod
99 100 101 |
# File 'lib/factorio/mod/mod.rb', line 99 def title @mod.get[:title] end |