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



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

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



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

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

#not_placed_errorObject



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

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

#position_reportObject



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

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



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

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

#response_schemaObject



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

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

#robotObject

“Normal” sinatra/ruby code.



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

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