Sha256: 5c626b69b59162e26ddb7f2f115eddba316ad662ae9554573029d35833dcd108

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

# encoding: utf-8
require 'json'

module Apiary
  module Okapi
    class Parser

      attr_reader :data, :resources

      def initialize(spec_path)
        @data = read_file(spec_path)
      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
      end

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

      def parse_data
        @data.each { |res|
          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
        }
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
okapi-0.0.6 lib/okapi/spec_parser.rb
okapi-0.0.5 lib/okapi/spec_parser.rb
okapi-0.0.4 lib/okapi/spec_parser.rb