Class: TentD::API::Middleware

Inherits:
Object
  • Object
show all
Includes:
Authorizable
Defined in:
lib/tentd/api/middleware.rb

Direct Known Subclasses

Apps::AuthorizationTokenExchange, Apps::AuthorizeReadAll, Apps::AuthorizeReadOne, Apps::AuthorizeWriteOne, Apps::Create, Apps::CreateAuthorization, Apps::Destroy, Apps::DestroyAppAuthorization, Apps::GetActualId, Apps::GetAll, Apps::GetOne, Apps::Update, Apps::UpdateAppAuthorization, AuthenticationFinalize, AuthenticationLookup, AuthenticationVerification, Authorization, CorsPreflight, Followers::AuthorizeReadMany, Followers::AuthorizeReadOne, Followers::AuthorizeWriteOne, Followers::Confirm, Followers::Create, Followers::Destroy, Followers::Discover, Followers::GetActualId, Followers::GetCount, Followers::GetMany, Followers::GetOne, Followers::Import, Followers::Notify, Followers::Update, Followings::AuthorizeWrite, Followings::Create, Followings::Destroy, Followings::Discover, Followings::Follow, Followings::GetActualId, Followings::GetCount, Followings::GetMany, Followings::GetOne, Followings::Notify, Followings::ProxyRequest, Followings::RedirectToFollowUI, Followings::RewriteProxyCaptureParams, Followings::Update, Groups::AuthorizeRead, Groups::AuthorizeWrite, Groups::Create, Groups::Destroy, Groups::GetActualId, Groups::GetAll, Groups::GetCount, Groups::GetOne, Groups::Notify, Groups::Update, HelloWorld, Posts::ConfirmFollowing, Posts::CreateAttachments, Posts::CreatePost, Posts::Destroy, Posts::GetActualId, Posts::GetAttachment, Posts::GetCount, Posts::GetFeed, Posts::GetOne, Posts::Notify, Posts::TriggerUpdates, Posts::Update, Profile::AuthorizeWrite, Profile::Get, Profile::Notify, Profile::Patch, Profile::Update, UserLookup

Instance Method Summary collapse

Methods included from Authorizable

#authorize_env!, #authorize_env?

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



8
9
10
# File 'lib/tentd/api/middleware.rb', line 8

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/tentd/api/middleware.rb', line 12

def call(env)
  env = Hashie::Mash.new(env) unless env.kind_of?(Hashie::Mash)
  response = action(env)
  response.kind_of?(Hash) ? @app.call(response) : response
rescue Unauthorized
  [403, {}, ['Unauthorized']]
rescue DataMapper::SaveFailureError, DataObjects::IntegrityError
  [422, {}, ['Invalid Attributes']]
rescue Exception => e
  if ENV['RACK_ENV'] == 'test'
    raise
  elsif defined?(Airbrake)
    Airbrake.notify_or_ignore(e, :rack_env => env)
  else
    puts $!.inspect, $@
  end
  [500, {}, ['Internal Server Error']]
end