Class: RubyRobot::Webapp

Inherits:
Sinatra::Base
  • Object
show all
Includes:
SchemaLoader
Defined in:
lib/ruby_robot/webapp.rb

Overview

Simple Sinatra webapp that supports:

Run HTTP GET on /swagger.json to fetch a static JSON OpenAPI description for this webapp (suitable for use with swagger.io’s GUI).

Constant Summary collapse

REPORT_EXAMPLE_OBJ =
{x:1,y:1,direction: :NORTH}
REPORT_EXAMPLE =
REPORT_EXAMPLE_OBJ.to_json
ERR_PLACEMENT_MSG =
'Robot is not currently placed'

Instance Method Summary collapse

Methods included from SchemaLoader

#load_schema

Instance Method Details

#formatted_reportObject



118
119
120
121
122
123
124
# File 'lib/ruby_robot/webapp.rb', line 118

def formatted_report
  r = robot.REPORT(false)
  if !r.nil?
    r[:direction] = r[:direction].upcase unless r[:direction].nil?
  end
  r
end

#max_error_message_lengthObject



46
47
48
49
# File 'lib/ruby_robot/webapp.rb', line 46

def max_error_message_length
  # Grab from the schema
  (response_schema['definitions']['Error']['properties']['message']['maxLength'] || 1024)
end

#not_placed_errorObject



114
115
116
# File 'lib/ruby_robot/webapp.rb', line 114

def not_placed_error
  [400, {code: 400, message: ERR_PLACEMENT_MSG}.to_json]
end

#position_reportObject



126
127
128
129
130
# File 'lib/ruby_robot/webapp.rb', line 126

def position_report
  # Pass along the report, but the direction needs to be upcased to
  # comply w/ the JSON schema for the web API 
  [200, formatted_report.to_json]
end

#request_schemaObject



38
39
40
# File 'lib/ruby_robot/webapp.rb', line 38

def request_schema
  @@request_schema ||= schema = load_schema('request')
end

#response_schemaObject



42
43
44
# File 'lib/ruby_robot/webapp.rb', line 42

def response_schema
  @@response_schema ||= schema = load_schema('response')
end

#robotObject

“Normal” sinatra/ruby code.



105
106
107
108
109
110
111
112
# File 'lib/ruby_robot/webapp.rb', line 105

def robot
  @@robot ||= proc { 
    rr = ::RubyRobot::Shell.new
    # In the webapp, don't log the REPORT messages to stdout
    rr.logger.formatter = proc { |severity, datetime, progname, msg| "" }
    rr
  }.call
end