Class: Factorio::Mod

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Mod

Returns a new instance of Mod.

Parameters:

  • name (String)

    name of the 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

#nameObject (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.

Parameters:

  • name (String)

    name of the mod

Returns:

  • (Bool)

    is 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

Parameters:

  • rel (String)

    relative link

Returns:

  • (String)

    absolute link

Raises:

  • (NotLoginError)


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.

Examples:

print 'username: '
user = gets.chomp
print 'password: '
pass = gets.chomp
token = Factorio::Mod. user, pass

Parameters:

  • username (String)
  • password (String)

Returns:

  • (String)

    The log in token



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/factorio/mod/mod.rb', line 42

def self.(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.

Parameters:

  • card (Nokogiri::HTML)

Returns:

  • (String)

    The name in



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.

Parameters:

  • keyword (String)

    search keyword

  • version (String) (defaults to: 'any')

    factorio version, any for any version

Returns:

  • (Array<Mod>)

    all mod searched on the first page with card cache



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

#authorString Also known as: owner

Get the author of the Mod.

Returns:

  • (String)

    author



139
140
141
# File 'lib/factorio/mod/mod.rb', line 139

def author
  @mod.get[:owner]
end

#deprecated?Boolean

Whether Mod is deprecated

Returns:

  • (Boolean)

    is deprecated



146
147
148
# File 'lib/factorio/mod/mod.rb', line 146

def deprecated?
  @mod.get[:deprecated] || false
end

#download_listArray<Download>

Get the download for all of version.

Returns:



167
168
169
# File 'lib/factorio/mod/mod.rb', line 167

def download_list
  @mod.get[:releases].dup.map(&Download.method(:create))
end

#download_timesInteger Also known as: downloads_count

Get the totle number of downloads of Mod.

Returns:

  • (Integer)

    number of downloads



152
153
154
# File 'lib/factorio/mod/mod.rb', line 152

def download_times
  @mod.get[:downloads_count]
end

#gitAddressable::URI

Get the git repo URI that can be cloned.

Returns:

  • (Addressable::URI)

    git URI



120
121
122
# File 'lib/factorio/mod/mod.rb', line 120

def git
  github&.to_s&.+('.git')&.then(&Addressable::URI.method(:parse))
end

#githubAddressable::URI

Get the GitHub URI.

Returns:

  • (Addressable::URI)

    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.

Parameters:

  • version (String) (defaults to: nil)

    factorio version

Returns:



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_uriAddressable::URI Also known as: latest_download_link

Get latest download uri by download button in card

Returns:

  • (Addressable::URI)

    download uri



173
174
175
# File 'lib/factorio/mod/mod.rb', line 173

def latest_download_uri
  Mod.extend_uri(latest_download[:download_url])
end

#licenseString

Get the license that the Mod use.

Returns:

  • (String)

    license name



126
127
128
# File 'lib/factorio/mod/mod.rb', line 126

def license
  @mod.get(:license, :name)
end

#license_uriAddressable::URI Also known as: license_link

Get the license URI.

Returns:

  • (Addressable::URI)

    license URI



132
133
134
# File 'lib/factorio/mod/mod.rb', line 132

def license_uri
  Addressable::URI.parse(@mod.get(:license, :url))
end

#summaryString

Get the summary of the Mod

Returns:

  • (String)

    summary



106
107
108
# File 'lib/factorio/mod/mod.rb', line 106

def summary
  @mod.get[:summary]
end

#titleString Also known as: display_name

Get the title (also known as display name) of the Mod

Returns:

  • (String)

    title



99
100
101
# File 'lib/factorio/mod/mod.rb', line 99

def title
  @mod.get[:title]
end