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
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_length ⇒ Object
46 47 48 49 |
# File 'lib/ruby_robot/webapp.rb', line 46 def # Grab from the schema (response_schema['definitions']['Error']['properties']['message']['maxLength'] || 1024) end |
#not_placed_error ⇒ Object
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_report ⇒ Object
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_schema ⇒ Object
38 39 40 |
# File 'lib/ruby_robot/webapp.rb', line 38 def request_schema @@request_schema ||= schema = load_schema('request') end |
#response_schema ⇒ Object
42 43 44 |
# File 'lib/ruby_robot/webapp.rb', line 42 def response_schema @@response_schema ||= schema = load_schema('response') end |
#robot ⇒ Object
“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 |