Class: TezosClient::EncodeUtils::ArgsEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/tezos_client/encode_utils.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expr) ⇒ ArgsEncoder

Returns a new instance of ArgsEncoder.



9
10
11
12
13
14
15
# File 'lib/tezos_client/encode_utils.rb', line 9

def initialize(expr)
  @expr = expr.gsub(/(?:@[a-z_]+)|(?:#.*$)/m, "")
              .gsub(/\s+/, " ")
              .strip
  initialize_statuses
  initialize_ret
end

Instance Attribute Details

#escapedObject

Returns the value of attribute escaped.



6
7
8
# File 'lib/tezos_client/encode_utils.rb', line 6

def escaped
  @escaped
end

#exprObject

Returns the value of attribute expr.



6
7
8
# File 'lib/tezos_client/encode_utils.rb', line 6

def expr
  @expr
end

#plObject

Returns the value of attribute pl.



6
7
8
# File 'lib/tezos_client/encode_utils.rb', line 6

def pl
  @pl
end

#popenObject

Returns the value of attribute popen.



6
7
8
# File 'lib/tezos_client/encode_utils.rb', line 6

def popen
  @popen
end

#retObject

Returns the value of attribute ret.



6
7
8
# File 'lib/tezos_client/encode_utils.rb', line 6

def ret
  @ret
end

#sopenObject

Returns the value of attribute sopen.



6
7
8
# File 'lib/tezos_client/encode_utils.rb', line 6

def sopen
  @sopen
end

Instance Method Details

#encodeObject



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/tezos_client/encode_utils.rb', line 121

def encode
  expr.each_char.with_index do |char, i|
    is_last_char = (i == (expr.length - 1))
    treat_char(char, is_last_char)
  end

  if sopen
    raise ArgumentError, "string '#{@val}' has not been closed"
  end

  ret
end

#initialize_retObject



25
26
27
28
29
30
# File 'lib/tezos_client/encode_utils.rb', line 25

def initialize_ret
  @ret = {
    prim: nil,
    args: []
  }
end

#initialize_statusesObject



17
18
19
20
21
22
23
# File 'lib/tezos_client/encode_utils.rb', line 17

def initialize_statuses
  @popen = false
  @sopen = false
  @escaped = false
  @pl = 0
  @val = ""
end

#treat_char(char, is_last_char) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/tezos_client/encode_utils.rb', line 101

def treat_char(char, is_last_char)
  return if treat_escape(char)

  unless popen || sopen
    if is_last_char || char == " "
      @val += char if is_last_char
      treat_val
      return
    end
  end

  unless popen
    return if treat_double_quote(char)
  end

  return if treat_parenthesis(char)

  @val += char
end

#treat_double_quote(char) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tezos_client/encode_utils.rb', line 49

def treat_double_quote(char)
  return false unless char == '"'

  if @sopen
    @sopen = false
    if !ret[:prim]
      @ret = { "string" => @val }
    else
      @ret[:args] << { "string" => @val }
    end
    @val = ""
  else
    @sopen = true
  end
  true
end

#treat_escape(char) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/tezos_client/encode_utils.rb', line 89

def treat_escape(char)
  if escaped
    @val += char
    @escaped = false
    true
  elsif char == "\\"
    @escaped = true
    true
  end
  false
end

#treat_parenthesis(char) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tezos_client/encode_utils.rb', line 66

def treat_parenthesis(char)
  case char
  when "("
    @val += char if @popen
    @popen = true
    @pl += 1
    true
  when ")"
    raise "closing parenthesis while none was opened #{val}" unless popen
    @pl -= 1
    if pl.zero?
      @ret[:args] << ArgsEncoder.new(@val).encode
      @val = ""
      @popen = false
    else
      @val += char
    end
    true
  else
    false
  end
end

#treat_valObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tezos_client/encode_utils.rb', line 32

def treat_val
  unless @val.empty?
    if @val == @val.to_i.to_s
      if !ret[:prim]
        @ret = { "int" => @val }
      else
        @ret[:args] << { "int" => @val }
      end
    elsif ret[:prim]
      @ret[:args] << ArgsEncoder.new(@val).encode
    else
      @ret[:prim] = @val
    end
    @val = ""
  end
end