Class: Honey::Okapi::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/okapi/spec_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec_path) ⇒ Parser

Returns a new instance of Parser.



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

def initialize(spec_path)
  if not File.exist? spec_path
    raise Exception, "Test spec. file '#{spec_path}' not found"
  end
  @data = read_file(spec_path)
  @proces_all_bp_resources = false
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/okapi/spec_parser.rb', line 7

def data
  @data
end

#global_varsObject (readonly)

Returns the value of attribute global_vars.



7
8
9
# File 'lib/okapi/spec_parser.rb', line 7

def global_vars
  @global_vars
end

#proces_all_bp_resourcesObject (readonly)

Returns the value of attribute proces_all_bp_resources.



7
8
9
# File 'lib/okapi/spec_parser.rb', line 7

def proces_all_bp_resources
  @proces_all_bp_resources
end

#resourcesObject (readonly)

Returns the value of attribute resources.



7
8
9
# File 'lib/okapi/spec_parser.rb', line 7

def resources
  @resources
end

Instance Method Details

#parse_dataObject



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
# File 'lib/okapi/spec_parser.rb', line 40

def parse_data
  global_vars = {}
  resources = []
  @data.each { |res|
    if res.index('CONTINUE') == 0
      @proces_all_bp_resources = true
      next
    end

    if res.index('VARS') == 0
      splited = res.split(' ',2)
      begin
        global_vars = JSON.parse splited[1] if splited[1] and splited[1] != ''
      rescue Exception => e
        raise Exception, "can not parse global parameters (#{e})"
      end
      next
    end

    splited = res.split(' ',3)

    begin
      splited[2] = JSON.parse splited[2] if splited[2] and splited[2] != ''
    rescue Exception => e
      raise Exception, 'can not parse parameters for resource:' + res + "(#{e})"
    end
    
    if splited[1] and splited[1] != '' and splited[0] and splited[0] != ''
      out = {
        'resource' => splited[1],
        'method' => splited[0],
        'params' => substituite_vars(splited[2] || {}, global_vars)
        }
      resources << out
    end
  }

  @global_vars = global_vars
  resources
end

#read_file(path) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/okapi/spec_parser.rb', line 21

def read_file(path)
  @data = []
    File.open(path).each do |line|
      @data << line if line.strip != ""
    end
  @data
end

#substituite_vars(local, global) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/okapi/spec_parser.rb', line 29

def substituite_vars(local, global)
  tmp = {}
  global.each {|k,v|
    tmp[k] = v
  }
  local.each {|k,v|
    tmp[k] = v
  }
  tmp
end