Module: Hippo::API::RequestWrapper

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.delete(*args) ⇒ Object



25
26
27
28
29
# File 'lib/hippo/api/request_wrapper.rb', line 25

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

.get(*args) ⇒ Object



7
8
9
10
11
# File 'lib/hippo/api/request_wrapper.rb', line 7

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

.post(*args) ⇒ Object



13
14
15
16
17
# File 'lib/hippo/api/request_wrapper.rb', line 13

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

.update(*args) ⇒ Object



19
20
21
22
23
# File 'lib/hippo/api/request_wrapper.rb', line 19

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/hippo/api/request_wrapper.rb', line 55

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



104
105
106
107
# File 'lib/hippo/api/request_wrapper.rb', line 104

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

#wrap_reply(options = { with_transaction: true, require_tenant: true }) ⇒ Object

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

Parameters:

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

    for additional checks

  • opts (Hash)

    a customizable set of options



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
# File 'lib/hippo/api/request_wrapper.rb', line 76

def wrap_reply(options = { with_transaction: true, require_tenant: true })
    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