module CultomePlayer

Constants

VERSION

Public Class Methods

get_player(env=:user) click to toggle source

Get an instance of DefaultPlayer

@param env [Symbol] The environment from which the configirations will be taken. @return [DefaultPlayer] A Cultome player ready to rock.

# File lib/cultome_player.rb, line 122
def get_player(env=:user)
  DefaultPlayer.new(env)
end

Public Instance Methods

create_response(type, data) click to toggle source

Creates a generic response

@param type [Symbol] The response type. @param data [Hash] The information that the response will contain. @return [Response] Response object with information in form of getter methods.

# File lib/cultome_player.rb, line 74
def create_response(type, data)
  data[:response_type] = data.keys.first unless data.has_key?(:response_type)
  return Response.new(type, data)
end
execute(user_input) click to toggle source

Interpret a user input string as it would be typed in the console.

@param user_input [String] The user input. @return [Response] Response object with information about command execution.

# File lib/cultome_player.rb, line 27
def execute(user_input)
  cmds = parse user_input

  seq_success = true # bandera de exito, si un comando de la cadena falla, los siguientes se abortan
  response_seq = cmds.collect do |cmd|
    if seq_success
      # revisamos si es un built in command o un plugin
      action = cmd.action
      plugin_action = "command_#{cmd.action}".to_sym
      action = plugin_action if plugins_respond_to?(cmd.action)

      raise 'invalid command:action unknown' unless respond_to?(action)
      with_connection do
        begin
          emit_event(:before_command, cmd)
          emit_event("before_command_#{action}".to_sym, cmd) if cmd.history?
          r = send(action, cmd)
          emit_event("after_command_#{action}".to_sym, cmd, r) if cmd.history?
          emit_event(:after_command, cmd, r)

          seq_success = false unless r.success?
          r # return response
        rescue Exception => e
          emit_event(:execute_exception, cmd, e)

          if current_env == :test || current_env == :rspec
            display c3("#{e.message}")
            e.backtrace.each{|b| display c3(b) }
          end

          seq_success = false
          s = e.message.split(":")
          failure(message: s[0], details: s[1])
        end
      end
    else # seq_success == false
      nil
    end
  end
  return response_seq.compact # eliminamos los que no corrieron
end
failure(response) click to toggle source

Creates a failure response. Handy method for create_response

@param response [Hash] The information that the response will contain. @return [Response] Response object with information in form of getter methods.

# File lib/cultome_player.rb, line 91
def failure(response)
  create_response(:failure, get_response_params(response))
end
success(response) click to toggle source

Creates a success response. Handy method for create_response

@param response [Hash] The information that the response will contain. @return [Response] Response object with information in form of getter methods.

# File lib/cultome_player.rb, line 83
def success(response)
  create_response(:success, get_response_params(response))
end

Private Instance Methods

get_response_params(response) click to toggle source
# File lib/cultome_player.rb, line 129
def get_response_params(response) 
  return {message: response} if response.instance_of?(String)
  return response
end