Class: Monerorequest::Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/monerorequest/encoder.rb

Overview

class to Encode a Monerorequest hash

Defined Under Namespace

Classes: InvalidRequest

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ Encoder

Returns a new instance of Encoder.



18
19
20
21
22
23
# File 'lib/monerorequest/encoder.rb', line 18

def initialize(request)
  @request = request
  @errors = []

  validate!
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



16
17
18
# File 'lib/monerorequest/encoder.rb', line 16

def errors
  @errors
end

#requestObject

Returns the value of attribute request.



15
16
17
# File 'lib/monerorequest/encoder.rb', line 15

def request
  @request
end

Instance Method Details

#encode(version) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/monerorequest/encoder.rb', line 25

def encode(version)
  raise RequestVersionError, "Only Request Version 1 is supported." unless version == 1

  json_str = @request.sort.to_h.to_json.force_encoding("ascii")
  compressed_data = StringIO.new
  gz = Zlib::GzipWriter.new(compressed_data, 9)
  gz.mtime = 0
  gz.write(json_str)
  gz.close
  encoded_str = Base64.encode64(compressed_data.string).gsub("\n", "")
  "monero-request:1:#{encoded_str}"
end