Module: Hippo::API::RequestWrapper

Defined in:
lib/hippo/api/request_wrapper.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
    with_transaction: true, require_tenant: true
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.delete(*args) ⇒ Object


29
30
31
32
33
# File 'lib/hippo/api/request_wrapper.rb', line 29

def delete(*args)
    make_handler(*args) do |controller|
        controller.destroy
    end
end

.get(*args) ⇒ Object


11
12
13
14
15
# File 'lib/hippo/api/request_wrapper.rb', line 11

def get(*args)
    make_handler(*args) do |controller|
        controller.show
    end
end

.post(*args) ⇒ Object


17
18
19
20
21
# File 'lib/hippo/api/request_wrapper.rb', line 17

def post(*args)
    make_handler(*args) do |controller|
        controller.create
    end
end

.update(*args) ⇒ Object


23
24
25
26
27
# File 'lib/hippo/api/request_wrapper.rb', line 23

def update(*args)
    make_handler(*args) do |controller|
        controller.update
    end
end

.with_authenticated_user(options = {with_transaction: true}) ⇒ Object

Ensure request is performed with a logged in user. The provided block will be called with |user, request|

Parameters:

  • options (options) (defaults to: {with_transaction: true})

    for additional checks

  • opts (Hash)

    a customizable set of options

Options Hash (options):

  • :role (String)

    A role name that the user must have


59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/hippo/api/request_wrapper.rb', line 59

def with_authenticated_user(options = {with_transaction: true})
    role = options[:role]
    lambda do
        authentication = Hippo::API::AuthenticationProvider.new(request)
        user = authentication.current_user
        if user && (role.nil? || user.roles.include?(role))
            wrap_reply(options) do
                yield authentication.current_user, self
            end
        else
            authentication.fail_request(self)
        end
    end
end

Instance Method Details

#log_requestObject

Logs UserID and params for a request. In non-production, the JSON payload is also logged


115
116
117
118
# File 'lib/hippo/api/request_wrapper.rb', line 115

def log_request
    Hippo.logger.info "UserID: #{session['user_id']}, Params: #{request.params}"
    Hippo.logger.debug JSON.pretty_generate(data) unless Hippo.env.production? or data.nil?
end

#with_user(options = DEFAULT_OPTIONS) ⇒ Object


75
76
77
78
79
80
# File 'lib/hippo/api/request_wrapper.rb', line 75

def with_user(options = DEFAULT_OPTIONS)
    authentication = Hippo::API::AuthenticationProvider.new(request)
    wrap_reply(options) do
        yield authentication.current_user
    end
end

#wrap_reply(options = DEFAULT_OPTIONS) ⇒ Object

Wraps a HTTP request in an optional DB transaction and converts yeilded data to JSON

Parameters:

  • options (options) (defaults to: DEFAULT_OPTIONS)

    for additional checks

  • opts (Hash)

    a customizable set of options


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/hippo/api/request_wrapper.rb', line 87

def wrap_reply(options = DEFAULT_OPTIONS)
    if options[:require_tenant] && Hippo::Tenant.current.nil?
        return json_reply(
                   { success: false, message: "invalid address",
                     errors: { address: 'invalid' } }
               )
    end
    response = { success: false, message: "No response was generated" }
    log_request
    if options[:with_transaction]
        Hippo::Model.transaction do
            response = yield || {success: false}
            # This is quite possibly a horrible idea.
            # It enables test specs to reset the db state after a request
            if !Hippo.env.production? && request.env['HTTP_X_ROLLBACK_AFTER_REQUEST']
                Hippo::Model.connection.rollback_db_transaction
            end
        end
    else
        response = yield
    end
    if false == response[:success]
        status(406)
    end
    json_reply response
end