Class: Factorix::Blueprint
- Inherits:
-
Object
- Object
- Factorix::Blueprint
- 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
-
#data ⇒ Hash
readonly
The blueprint data.
Class Method Summary collapse
-
.decode(string) ⇒ Blueprint
Decode a blueprint string into a Blueprint.
Instance Method Summary collapse
-
#encode ⇒ String
Encode this blueprint to a blueprint string.
-
#initialize(data) ⇒ Blueprint
constructor
A new instance of Blueprint.
-
#to_json ⇒ String
Serialize this blueprint to pretty-printed JSON.
Constructor Details
#initialize(data) ⇒ Blueprint
Returns a new instance of Blueprint.
42 43 44 |
# File 'lib/factorix/blueprint.rb', line 42 def initialize(data) @data = data end |
Instance Attribute Details
#data ⇒ Hash (readonly)
Returns 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
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.}" rescue Zlib::Error => e raise BlueprintFormatError, "Invalid zlib data: #{e.}" rescue JSON::ParserError => e raise BlueprintFormatError, "Invalid JSON: #{e.}" end |
Instance Method Details
#encode ⇒ String
Encode this blueprint to a blueprint 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_json ⇒ String
Serialize this blueprint to pretty-printed JSON
58 59 60 |
# File 'lib/factorix/blueprint.rb', line 58 def to_json(*) JSON.pretty_generate(@data) end |