Class: Etheruby::ResponseParser

Inherits:
Object
  • Object
show all
Defined in:
lib/etheruby/response_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_returns, _response) ⇒ ResponseParser

Returns a new instance of ResponseParser.



20
21
22
23
24
25
26
27
# File 'lib/etheruby/response_parser.rb', line 20

def initialize(_returns, _response)
  @returns = if _returns.is_a? ::String or _returns.is_a? ::Symbol
    [_returns]
  else
    _returns
  end
  @response = _response[2.._response.length]
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



18
19
20
# File 'lib/etheruby/response_parser.rb', line 18

def response
  @response
end

#returnsObject (readonly)

Returns the value of attribute returns.



18
19
20
# File 'lib/etheruby/response_parser.rb', line 18

def returns
  @returns
end

Instance Method Details

#parseObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/etheruby/response_parser.rb', line 29

def parse
  return unless @returns
  real_response = @response
  responses = if @returns.is_a? Array
    []
  else
    {}
  end
  i = 0

  loop do
    if @returns.is_a? ::Hash
      return_name, return_type = returns.keys[i], returns.values[i]
    else
      return_type = returns[i]
    end

    # Treat element
    if Etheruby::is_static_type? return_type
      v, s = Etheruby::treat_variable return_type, real_response, :decode
    else
      value_position, s = Etheruby::Encoders::Uint.new(real_response).decode
      v, take_size = Etheruby::treat_variable(
        return_type,
        @response[(value_position*2)..response.length],
        :decode
      )
    end

    # Treat arrays
    if v.is_a? Etheruby::Encoders::DynamicArray or
       v.is_a? Etheruby::Encoders::StaticArray
      v, s = v.decode
    end

    # Add response to responses array
    if returns.is_a? ::Hash
      responses[return_name] = v
    else
      responses << v
    end

    # Continues until it has the good number of response
    break if responses.count == returns.count
    real_response = real_response[s*2..real_response.length]
    i += 1
  end

  # Returning response in the appropriate format
  if @returns.is_a? ::Hash
    ResponseHolder.new responses
  else
    if @returns.count == 1
      responses[0]
    else
      responses
    end
  end

end