Class: Apia::Rack
- Inherits:
-
Object
- Object
- Apia::Rack
- Defined in:
- lib/apia/rack.rb
Class Method Summary collapse
-
.error_triplet(code, description: nil, detail: {}, status: 500, headers: {}) ⇒ Array
Return a triplet for a given error using the standard error schema.
-
.json_triplet(body, status: 200, headers: {}) ⇒ Array
Return a JSON-ready triplet for the given body.
Instance Method Summary collapse
-
#api ⇒ Apia::API
Return the API object.
-
#call(env) ⇒ Array
Actually make the request.
-
#development? ⇒ Boolean
Is this supposed to be running in development? This will validate the whole API on each request as well as being more verbose about internal server errors that are encountered.
-
#find_route(method, path) ⇒ nil, Hash
Parse a given full path and return nil if it doesn’t match our namespace or return a hash with the controller and endpoint named as available.
-
#initialize(app, api, namespace, **options) ⇒ Rack
constructor
A new instance of Rack.
Constructor Details
#initialize(app, api, namespace, **options) ⇒ Rack
Returns a new instance of Rack.
11 12 13 14 15 16 |
# File 'lib/apia/rack.rb', line 11 def initialize(app, api, namespace, **) @app = app @api = api @namespace = '/' + namespace.sub(/\A\/+/, '').sub(/\/+\z/, '') @options = end |
Class Method Details
.error_triplet(code, description: nil, detail: {}, status: 500, headers: {}) ⇒ Array
Return a triplet for a given error using the standard error schema
171 172 173 174 175 176 177 178 179 |
# File 'lib/apia/rack.rb', line 171 def error_triplet(code, description: nil, detail: {}, status: 500, headers: {}) json_triplet({ error: { code: code, description: description, detail: detail } }, status: status, headers: headers.merge('x-api-schema' => 'json-error')) end |
.json_triplet(body, status: 200, headers: {}) ⇒ Array
Return a JSON-ready triplet for the given body.
154 155 156 157 158 159 160 161 |
# File 'lib/apia/rack.rb', line 154 def json_triplet(body, status: 200, headers: {}) body_as_json = body.to_json [ status, headers.merge('content-type' => 'application/json', 'content-length' => body_as_json.bytesize.to_s), [body_as_json] ] end |
Instance Method Details
#api ⇒ Apia::API
Return the API object
45 46 47 48 49 50 |
# File 'lib/apia/rack.rb', line 45 def api return Object.const_get(@api) if @api.is_a?(String) && development? return @cached_api ||= Object.const_get(@api) if @api.is_a?(String) @api end |
#call(env) ⇒ Array
Actually make the request
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/apia/rack.rb', line 56 def call(env) if @options[:hosts]&.none? { |host| host == env['HTTP_HOST'] } return @app.call(env) end unless env['PATH_INFO'] =~ /\A#{Regexp.escape(@namespace)}\/([a-z].*)\z/i return @app.call(env) end api_path = Regexp.last_match(1) triplet = handle_request(env, api_path) add_cors_headers(env, triplet) triplet end |
#development? ⇒ Boolean
Is this supposed to be running in development? This will validate the whole API on each request as well as being more verbose about internal server errors that are encountered.
23 24 25 26 27 28 |
# File 'lib/apia/rack.rb', line 23 def development? env_is_dev = ENV['RACK_ENV'] == 'development' return true if env_is_dev && @options[:development].nil? @options[:development] == true end |
#find_route(method, path) ⇒ nil, Hash
Parse a given full path and return nil if it doesn’t match our namespace or return a hash with the controller and endpoint named as available.
36 37 38 39 40 |
# File 'lib/apia/rack.rb', line 36 def find_route(method, path) return if api.nil? api.definition.route_set.find(method.to_s.downcase.to_sym, path).first end |