Sha256: 37a1500af89ce2312388eb252b34b0c666160a7fa9cfe8915bf25e87f28293f4

Contents?: true

Size: 1.89 KB

Versions: 2

Compression:

Stored size: 1.89 KB

Contents

require 'yaml'

module CloudCheckr
  module API
    module Controllers

      # Returns a [Hash] with controller name and class
      def self.build_controller_classes!
        endpoints_path    = File.join(File.dirname(__FILE__), 'endpoints.yml')
        endpoints_schemas = YAML.load_file(endpoints_path)
        controllers       = {}

        # Define a controller class for each controller name
        endpoints_schemas.each do |schema|
          controller_name       = schema['controller_name'].to_sym
          controller_class_name = schema['controller_name'].split('_').collect(&:capitalize).join + 'Controller'

          controller_class = Class.new(::CloudCheckr::API::Controller) do
            method_names = []

            # Define a method for each API call
            schema['api_calls'].each do |api_call|
              method_name     = api_call['method_name'].to_sym
              required_params = api_call['param_names'].lazy.select{|name| name.include?('(required)')}.map{|name| name.gsub(/\([^\)]+\)/, '').to_sym}.to_a

              if api_call['method_name'].start_with?('get_')
                define_method(method_name) do |params = {}, headers = nil, &request_builder|
                  @client.get(controller_name, method_name, params, headers, &request_builder)
                end
              else
                define_method(method_name) do |params = {}, headers = nil, &request_builder|
                  @client.post(controller_name, method_name, params, headers, &request_builder)
                end
              end

              method_names << method_name
            end

            define_method(:api_calls){ method_names }
          end

          # Register the class within the Controllers module
          self.const_set(controller_class_name, controller_class)

          controllers[controller_name] = controller_class
        end

        controllers
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cloudcheckr-0.5.3 lib/cloud_checkr/api/controllers.rb
cloudcheckr-0.5.2 lib/cloud_checkr/api/controllers.rb