./lib/lux/api/api.rb in lux-fw-0.1.17 vs ./lib/lux/api/api.rb in lux-fw-0.1.35

- old
+ new

@@ -8,13 +8,13 @@ # 404 Not Found # 500 Internal Server Error # 503 Service Unavailable class Lux::Api + attr_accessor :message, :response BeforeAndAfter.define self, :before, :after - Lux::RescueFrom.define(self) class << self # public mount method for router def call path @@ -58,58 +58,59 @@ return Lux.page.body({ error:"API #{klass} not found" }) end klass.new.call(action.to_sym, params) end - end - def call(action, params={}) - @@rescue_from_ivar.call do - rescued_call(action, params) - BeforeAndAfter.execute(self, :after) - end + ### - @response + def call action, params={} + @@rescue_from_ivar.call(self) { rescued_call action, params } + + BeforeAndAfter.execute(self, :after) + + response.render end # internal method for running actions # UserApi.new.call(:login, { email:'', pass:'' }) - def rescued_call(action, params={}) + def rescued_call action, params={} raise ForbidenError, "Protected action call" if [:call, :rescued_call, :params, :error].index action raise NotFoundError, "Action #{action} not found in #{self.class.to_s}" unless respond_to? action - @response = {} - @message = nil - @params = params + @response = Lux::Api::Response.new + @params = params @class_name = self.class.to_s.sub(/Api$/,'') # load default object if @params[:_id] eval "@object = @#{@class_name.underscore} = #{@class_name}[@params[:_id].to_i]" @params.delete(:_id) - @response[:path] = @object.path rescue nil if @object + @response.meta :path, @object.path if @object.respond_to?(:path) end + check_params_and_mock_instance_variables action + return if response.errors? + # execte api call and verify params if possible BeforeAndAfter.execute(self, :before) - return unless check_params_and_mock_instance_variables action + api_data = send action - api_data = send(action) - format_response!(api_data) + format_response! api_data end def params @params end - def error(what) - raise StandardError, what + def error what + raise Lux::Api::Error.new(what) end - def format_response!(api_data=nil) + def format_response! api_data=nil if api_data # api_data = instance_eval(&res) if api_data.kind_of?(Proc) api_data = api_data.all.map{ |el| el.attributes } if api_data.class.name == 'ActiveRecord::Relation' # sequel array of objects returned @@ -121,30 +122,28 @@ if api_data.respond_to?(:attributes) @response[:path] = api_data.path if api_data.id && api_data.respond_to?(:path) api_data = api_data.attributes.reject{ |f| ['updated_by', 'updated_at', 'created_by', 'created_at'].index(f) } end - @response[:data] = api_data - @response[:message] = @message.to_s unless @message.nil? - # @response[:message] = api_data if !@message && api_data.kind_of?(String) + response.data = api_data # if we define _redirect then we will be redirected to exact page # useful for file uploads if @params[:_redirect] - if @response[:error] - Lux.page.flash.error @response[:error] - elsif @response[:message] + if response.errors + Lux.page.flash.error @response[:errors].join(', ') + elsif response.message Lux.page.flash.info @response[:message] end Lux.page.redirect(@params[:_redirect]) end end - ap @response if Lux.config.debug_api_response + ap response.render if Lux.config.log_to_stdout end def message(what) - @message = what + response.message = what end end