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

Attributes included from Callbacks

#callbacks

Attributes included from ResourceAliases

#resource_aliases

Class Method Summary collapse

Methods included from Callbacks

extended, on, trigger

Methods included from ResourceAliases

alias_resource, aliases_for, extended, reset_aliases!

Class Attribute Details

.appObject

Returns the value of attribute app.



61
62
63
# File 'lib/sinatra/api.rb', line 61

def app
  @app
end

.configSinatra::API::Config

Runtime configuration options.



66
67
68
# File 'lib/sinatra/api.rb', line 66

def config
  @config
end

.instanceSinatra::Base

The Sinatra application.

Returns:

  • (Sinatra::Base)


56
57
58
# File 'lib/sinatra/api.rb', line 56

def instance
  @instance
end

.loggerActiveSupport::Logger

A Logger instance.

Returns:

  • (ActiveSupport::Logger)


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(options = {})
  self.config = Config.new(options)
end

.parse_json(stream) ⇒ Hash

Parse a JSON construct from a string stream.

Override this to use a custom JSON parser, if necessary.

Parameters:

  • stream (String)

    The raw JSON stream.

Returns:

  • (Hash)

    A Hash of the parsed JSON.



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.message
      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