Module: Sinatra::API
- Extended by:
- Callbacks, ResourceAliases
- Defined in:
- lib/sinatra/api/helpers.rb,
lib/sinatra/api.rb,
lib/sinatra/api/version.rb,
lib/sinatra/api/callbacks.rb,
lib/sinatra/api/resources.rb,
lib/sinatra/api/parameters.rb,
lib/sinatra/api/resource_aliases.rb
Overview
Copyright (c) 2013 Algol Labs, LLC. [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Defined Under Namespace
Modules: Callbacks, Helpers, Parameters, ResourceAliases, Resources
Constant Summary collapse
- ResourcePrefix =
'::'
- VERSION =
"1.0.2"
Class Attribute Summary collapse
-
.instance ⇒ Sinatra::Application
The Sinatra instance that is evaluating the current request.
-
.logger ⇒ ActiveSupport::Logger
A Logger instance.
Attributes included from Callbacks
Attributes included from ResourceAliases
Class Method Summary collapse
-
.parse_json(stream) ⇒ Hash
Parse a JSON construct from a string stream.
- .registered(app) ⇒ Object
Methods included from Callbacks
Methods included from ResourceAliases
alias_resource, aliases_for, export_alias, extended, reset_aliases!
Class Attribute Details
.instance ⇒ Sinatra::Application
The Sinatra instance that is evaluating the current request.
50 51 52 |
# File 'lib/sinatra/api.rb', line 50 def instance @instance end |
.logger ⇒ ActiveSupport::Logger
A Logger instance.
45 46 47 |
# File 'lib/sinatra/api.rb', line 45 def logger @logger end |
Class Method Details
.parse_json(stream) ⇒ Hash
Parse a JSON construct from a string stream.
Override this to use a custom JSON parser, if necessary.
59 60 61 |
# File 'lib/sinatra/api.rb', line 59 def parse_json(stream) ::JSON.parse(stream) end |
.registered(app) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/sinatra/api.rb', line 66 def self.registered(app) base = self self.logger = ActiveSupport::Logger.new(STDOUT) app.helpers Helpers, Parameters, Resources app.before do base.instance = self @api = { required: {}, optional: {} } @parent_resource = nil if api_call? request.body.rewind raw_json = request.body.read.to_s || '' unless raw_json.empty? begin params.merge!(base.parse_json(raw_json)) rescue ::JSON::ParserError => e logger.warn e. logger.warn e.backtrace halt 400, "Malformed JSON content" end end end end app.set(:requires) do |*resources| condition do @required = resources.collect { |r| r.to_s } @required.each do |r| @parent_resource = api_locate_resource(r, @parent_resource) end end end end |