Sha256: 74e9366ed4b05c64e5a289963122c7259e4c2ab53cd077e0dcaf417a890ef5e0

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

class Calamum::DefinitionParser
  attr_accessor :definition, :resources

  def initialize(definition_yaml)
    @definition = definition_yaml
    @resources = Hash.new
  end

  def get_resources
    @definition['resources']
  end

  def get_name
    @definition['name']
  end

  def get_url
    @definition['url']
  end

  def get_description
    @definition['description']
  end

  def get_resources_names
    @definition['resources'].inject([]){|resources, (key, content)| resources << key}
  end

  def load_requests
    self.get_resources.each do |resource, requests|
      @resources[resource] = []
      index = 0
      requests.each do |request|
        req = initialize_request(request)
        unless req.nil?
          @resources[resource][index]= req
          index += 1
        end
      end
    end
    @resources
  end

  def initialize_request(request)
    req = Calamum::Request.new({
                                 :uri =>  request['uri'],
                                 :action => request['action'],
                                 :params => request['params'],
                                 :description => request['description'],
                                 :headers => request['headers'],
                                 :response => request['response'],
                               })
    if req.errors.empty?
      puts_info "Request #{request['action']}: #{request['uri']} loading success"
      req
    else
      puts_error "Request #{request['action']}: #{request['uri']} not valid: #{req.errors}"
      nil
    end
  end

  def get_resource_uris(name)
    @resources[name]
  end

  def find_resource_uri_by_label(resource, label)
    @resources.select{|r| r['label']==label}.first['uri']
  end

  def find_resource_action_by_label(resource, label)
    @resources.select{|r| r['label']==label}.first['action']
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
calamum-1.0.1 lib/calamum/definition_parser.rb