Module: Sinatra::API
- Extended by:
- Callbacks, ResourceAliases
- Defined in:
- lib/sinatra/api/helpers.rb,
lib/sinatra/api.rb,
lib/sinatra/api/config.rb,
lib/sinatra/api/version.rb,
lib/sinatra/api/callbacks.rb,
lib/sinatra/api/resources.rb,
lib/sinatra/api/parameters.rb,
lib/sinatra/api/error_handler.rb,
lib/sinatra/api/resource_aliases.rb,
lib/sinatra/api/parameter_validator.rb,
lib/sinatra/api/parameter_validators/float_validator.rb,
lib/sinatra/api/parameter_validators/string_validator.rb,
lib/sinatra/api/parameter_validators/integer_validator.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, ErrorHandler, Helpers, Parameters, ResourceAliases, Resources Classes: Config, FloatValidator, IntegerValidator, ParameterValidator, StringValidator
Constant Summary collapse
- ResourcePrefix =
'::'
- VERSION =
"1.1.7"
Class Attribute Summary collapse
-
.app ⇒ Object
Returns the value of attribute app.
-
.config ⇒ Sinatra::API::Config
Runtime configuration options.
-
.instance ⇒ Sinatra::Base
The Sinatra application.
-
.logger ⇒ ActiveSupport::Logger
A Logger instance.
Attributes included from Callbacks
Attributes included from ResourceAliases
Class Method Summary collapse
- .configure(options = {}) ⇒ Object
-
.parse_json(stream) ⇒ Hash
Parse a JSON construct from a string stream.
- .process!(params, request) ⇒ Object
- .registered(app) ⇒ Object
Methods included from Callbacks
Methods included from ResourceAliases
alias_resource, aliases_for, extended, reset_aliases!
Class Attribute Details
.app ⇒ Object
Returns the value of attribute app.
61 62 63 |
# File 'lib/sinatra/api.rb', line 61 def app @app end |
.config ⇒ Sinatra::API::Config
Runtime configuration options.
66 67 68 |
# File 'lib/sinatra/api.rb', line 66 def config @config end |
.instance ⇒ Sinatra::Base
The Sinatra application.
56 57 58 |
# File 'lib/sinatra/api.rb', line 56 def instance @instance end |
.logger ⇒ ActiveSupport::Logger
A Logger instance.
51 52 53 |
# File 'lib/sinatra/api.rb', line 51 def logger @logger end |
Class Method Details
.configure(options = {}) ⇒ Object
79 80 81 |
# File 'lib/sinatra/api.rb', line 79 def configure( = {}) self.config = Config.new() end |
.parse_json(stream) ⇒ Hash
Parse a JSON construct from a string stream.
Override this to use a custom JSON parser, if necessary.
75 76 77 |
# File 'lib/sinatra/api.rb', line 75 def parse_json(stream) ::JSON.parse(stream) end |
.process!(params, request) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/sinatra/api.rb', line 83 def process!(params, request) request.body.rewind raw_json = request.body.read.to_s || '' unless raw_json.empty? begin params.merge!(self.parse_json(raw_json)) rescue ::JSON::ParserError => e logger.warn e. logger.warn e.backtrace instance.halt 400, "Malformed JSON content" end end end |
.registered(app) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/sinatra/api.rb', line 102 def self.registered(app) api = self self.app = app self.logger = ActiveSupport::Logger.new(STDOUT) self.logger.level = 100 app.helpers Helpers, Parameters, Resources ParameterValidator.install(api) on :with_errors_setting do |setting| app.helpers ErrorHandler if setting end on :verbose_setting do |setting| logger.level = setting ? 0 : 100 end app.before do api.instance = self api.trigger :request, self api.process!(params, request) if api_call? end end |