Module: Lanes::API::RequestWrapper

Defined in:
lib/lanes/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/lanes/api/request_wrapper.rb', line 25

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

.get(*args) ⇒ Object



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

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

.make_handler(model, controller, parent_attribute = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lanes/api/request_wrapper.rb', line 31

def make_handler(model, controller, parent_attribute = nil)
    lambda do
        authentication = Lanes::API::AuthenticationProvider.new(request)
        authentication.wrap_model_access(model, self) do
            if parent_attribute
              params[:nested_attribute] = Hash[ parent_attribute,
                                               params[parent_attribute] ]
            end
            wrap_reply(with_transaction: !request.get?) do
                yield controller.new(model, authentication, params, data)
            end
        end
    end
end

.post(*args) ⇒ Object



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

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

.update(*args) ⇒ Object



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

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

.with_authenticated_user(role: nil, with_transaction: true) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lanes/api/request_wrapper.rb', line 46

def with_authenticated_user(role:nil, with_transaction:true)
    lambda do
        authentication = Lanes::API::AuthenticationProvider.new(request)
        user = authentication.current_user
        if user and ( role.nil? or user.roles.include?(role) )
            wrap_reply(with_transaction: with_transaction) do
                yield authentication.current_user, self
            end
        else
            authentication.fail_request(self)
        end
    end
end

Instance Method Details

#log_requestObject



61
62
63
64
# File 'lib/lanes/api/request_wrapper.rb', line 61

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

#wrap_reply(with_transaction: true) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/lanes/api/request_wrapper.rb', line 66

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