Class: Factorix::Blueprint

Inherits:
Object
  • Object
show all
Defined in:
lib/factorix/blueprint.rb

Overview

Represents a Factorio blueprint

A blueprint string has the format: version_byte + Base64(zlib(JSON)) Only version byte ‘0’ is supported.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Blueprint

Returns a new instance of Blueprint.

Parameters:

  • data (Hash)

    The blueprint data



42
43
44
# File 'lib/factorix/blueprint.rb', line 42

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataHash (readonly)

Returns The blueprint data.

Returns:

  • (Hash)

    The blueprint data



18
19
20
# File 'lib/factorix/blueprint.rb', line 18

def data
  @data
end

Class Method Details

.decode(string) ⇒ Blueprint

Decode a blueprint string into a Blueprint

Parameters:

  • string (String)

    The blueprint string

Returns:

Raises:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/factorix/blueprint.rb', line 26

def self.decode(string)
  version = string[0]
  raise UnsupportedBlueprintVersionError, "Unsupported blueprint version: #{version.inspect}" unless version == SUPPORTED_VERSION

  compressed = Base64.strict_decode64(string[1..])
  json_string = Zlib::Inflate.inflate(compressed)
  new(JSON.parse(json_string))
rescue ArgumentError => e
  raise BlueprintFormatError, "Invalid Base64 encoding: #{e.message}"
rescue Zlib::Error => e
  raise BlueprintFormatError, "Invalid zlib data: #{e.message}"
rescue JSON::ParserError => e
  raise BlueprintFormatError, "Invalid JSON: #{e.message}"
end

Instance Method Details

#encodeString

Encode this blueprint to a blueprint string

Returns:

  • (String)


49
50
51
52
53
# File 'lib/factorix/blueprint.rb', line 49

def encode
  json_string = JSON.generate(@data)
  compressed = Zlib::Deflate.deflate(json_string, Zlib::BEST_COMPRESSION)
  SUPPORTED_VERSION + Base64.strict_encode64(compressed)
end

#to_jsonString

Serialize this blueprint to pretty-printed JSON

Returns:

  • (String)


58
59
60
# File 'lib/factorix/blueprint.rb', line 58

def to_json(*)
  JSON.pretty_generate(@data)
end