lib/okapi/spec_parser.rb in okapi-0.0.6 vs lib/okapi/spec_parser.rb in okapi-0.0.7

- old
+ new

@@ -3,51 +3,80 @@ module Apiary module Okapi class Parser - attr_reader :data, :resources + attr_reader :data, :resources, :proces_all_bp_resources, :global_vars 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 def resources - @resources ||= begin - parse_data { |res| - raise Exception, 'resource not defined' unless res["resource"] - raise Exception, 'method not defined' unless res["method"] - - (@resources ||= []) << Apiary::Okapi::Resource.new(res["resource"], res["method"], res["params"]) - } - @resources - end + @resources ||= parse_data end def read_file(path) @data = [] - File.open(path).each do |line| - @data << line if line.strip != "" - end + File.open(path).each do |line| + @data << line if line.strip != "" + end @data end + def substituite_vars(local, global) + tmp = {} + global.each {|k,v| + tmp[k] = v + } + local.each {|k,v| + tmp[k] = v + } + tmp + end + 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 - out = { - 'resource' => splited[1], - 'method' => splited[0], - 'params' => splited[2] || {} - } - yield out + + 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 end end end