Class: RubyRobot::Webapp
- Inherits:
-
Sinatra::Base
- Object
- Sinatra::Base
- RubyRobot::Webapp
- 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
- #formatted_report ⇒ Object
- #max_error_message_length ⇒ Object
- #not_placed_error ⇒ Object
- #position_report ⇒ Object
- #request_schema ⇒ Object
- #response_schema ⇒ Object
-
#robot ⇒ Object
“Normal” sinatra/ruby code.
Methods included from SchemaLoader
Instance Method Details
#formatted_report ⇒ Object
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_length ⇒ Object
45 46 47 48 |
# File 'lib/ruby_robot/webapp.rb', line 45 def # Grab from the schema (response_schema['definitions']['Error']['properties']['message']['maxLength'] || 1024) end |
#not_placed_error ⇒ Object
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_report ⇒ Object
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_schema ⇒ Object
37 38 39 |
# File 'lib/ruby_robot/webapp.rb', line 37 def request_schema @@request_schema ||= schema = load_schema('request') end |
#response_schema ⇒ Object
41 42 43 |
# File 'lib/ruby_robot/webapp.rb', line 41 def response_schema @@response_schema ||= schema = load_schema('response') end |
#robot ⇒ Object
“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 |