Module: Api::Scheme
- Defined in:
- lib/api/scheme/version.rb,
lib/api/scheme.rb
Defined Under Namespace
Modules: Action
Constant Summary collapse
- VERSION =
"0.2.1"
Class Method Summary collapse
Instance Method Summary collapse
- #_getter(var) ⇒ Object
- #code_parse(code, e) ⇒ Object
- #default_error_proc(_, _, _, code, e) ⇒ Object
- #default_success_proc(data) ⇒ Object
- #error_map ⇒ Object
- #get_code_of_error_map(e) ⇒ Object
- #get_code_of_model_error_map(e) ⇒ Object
- #get_code_text(code) ⇒ Object
- #get_pure_code(code) ⇒ Object
- #get_sub_code(code) ⇒ Object
- #model ⇒ Object
- #model_instance ⇒ Object
- #model_instance=(value) ⇒ Object
- #model_instances ⇒ Object
- #model_instances=(value) ⇒ Object
- #model_key ⇒ Object
- #models_key ⇒ Object
- #parse_error_map ⇒ Object
- #permitted_params ⇒ Object
- #permitted_params_permit(keys) ⇒ Object
- #permitted_params_require(key, map) ⇒ Object
- #render_default_error(e) ⇒ Object
- #render_default_success(data, options = {}) ⇒ Object
- #validate_access_token ⇒ Object
Class Method Details
.included(klass) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/api/scheme.rb', line 5 def self.included klass # NOTE put default render handlers to the module to use from as if is the ancestor self.instance_variable_set(:@success_proc, :default_success_proc) self.instance_variable_set(:@error_proc, :default_error_proc) klass.class_eval %Q" class << self def error_map map @error_map ||= map end def model_error_map map @model_error_map ||= map end def param_map map @param_map ||= map end def render_success_with method = nil, &prc @success_proc ||= method || prc end def render_error_with method = nil, &prc @error_proc ||= method || prc end def render_error_code options = {} @error_proces = (@error_proces || {}).merge(options) end def use_model name @model_name = name end def use_actions *list list.each do |action| Api::Scheme::Action.define_for(self, action) end Api::Scheme::Action::SCHEME.each do |method_name, cases| before = [ cases[:before] ].flatten.compact & list if before.present? Api::Scheme::Action.define_for(self, method_name) self.before_action(method_name, only: before) end end end end " klass.rescue_from StandardError, with: :render_default_error end |
Instance Method Details
#_getter(var) ⇒ Object
60 61 62 |
# File 'lib/api/scheme.rb', line 60 def _getter var self.class.ancestors.reduce(nil) { |value, klass| value || klass.instance_variable_get(var) } end |
#code_parse(code, e) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/api/scheme.rb', line 98 def code_parse code, e json = { message: e., type: e.class } case code when Range logger.error "#{e.class}: #{e.message}" [ code.first, json.merge(error_code: code.last) ] when NilClass logger.error "#{e.class}: #{e.message}\n\t#{e.backtrace[0...50].join("\n\t")}" [ 500, json ] else logger.error "#{e.class}: #{e.message}" [ code, json ] end end |
#default_error_proc(_, _, _, code, e) ⇒ Object
180 181 182 183 184 |
# File 'lib/api/scheme.rb', line 180 def default_error_proc _, _, _, code, e status, json = code_parse(code, e) render data: json, status: status end |
#default_success_proc(data) ⇒ Object
176 177 178 |
# File 'lib/api/scheme.rb', line 176 def default_success_proc data render data: data, status: 200 end |
#error_map ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/api/scheme.rb', line 64 def error_map error_map = _getter(:@error_map) error_map ||= map return error_map if _getter(:@error_map_parsed) error_map = parse_error_map self.class.instance_variable_set(:@error_map_parsed, true) self.class.instance_variable_set(:@error_map, error_map) end |
#get_code_of_error_map(e) ⇒ Object
125 126 127 128 129 |
# File 'lib/api/scheme.rb', line 125 def get_code_of_error_map e error_map.find do |errors, codes| errors.any? { |error| e.kind_of?(error) } end.try(:last) end |
#get_code_of_model_error_map(e) ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/api/scheme.rb', line 117 def get_code_of_model_error_map e error_text = e.record.errors..reduce(nil) {|s, (_, v)| s || v.join } _getter(:@model_error_map).to_a.reverse.reduce(nil) do |code, (re, new_code)| re =~ error_text && new_code || code end end |
#get_code_text(code) ⇒ Object
145 146 147 148 149 |
# File 'lib/api/scheme.rb', line 145 def get_code_text code path = code.is_a?(Range) && "#{code.begin}.#{code.end}" || code.to_s I18n.t("action_controller.#{controller_path}.errors.#{path}") end |
#get_pure_code(code) ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/api/scheme.rb', line 131 def get_pure_code code if code.is_a?(Range) code.begin else code end end |
#get_sub_code(code) ⇒ Object
139 140 141 142 143 |
# File 'lib/api/scheme.rb', line 139 def get_sub_code code if code.is_a?(Range) code.end end end |
#model ⇒ Object
186 187 188 |
# File 'lib/api/scheme.rb', line 186 def model @model ||= _getter(:@model_name).constantize end |
#model_instance ⇒ Object
198 199 200 |
# File 'lib/api/scheme.rb', line 198 def model_instance self.instance_variable_get(:"@#{model_key}") end |
#model_instance=(value) ⇒ Object
206 207 208 |
# File 'lib/api/scheme.rb', line 206 def model_instance= value self.instance_variable_set(:"@#{model_key}", value) end |
#model_instances ⇒ Object
202 203 204 |
# File 'lib/api/scheme.rb', line 202 def model_instances self.instance_variable_get(:"@#{models_key}") end |
#model_instances=(value) ⇒ Object
210 211 212 |
# File 'lib/api/scheme.rb', line 210 def model_instances= value self.instance_variable_set(:"@#{models_key}", value) end |
#model_key ⇒ Object
190 191 192 |
# File 'lib/api/scheme.rb', line 190 def model_key model.name.tableize.singularize end |
#models_key ⇒ Object
194 195 196 |
# File 'lib/api/scheme.rb', line 194 def models_key model.name.tableize end |
#parse_error_map ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/api/scheme.rb', line 75 def parse_error_map error_map = _getter(:@error_map) error_map.map do |(errors, code)| list = errors.map do |e| begin e.constantize rescue NameError begin "#{self.class}::#{e}".constantize rescue NameError begin send(:class_eval, e.camelize) rescue NameError raise InvalidErrorTypeError, e end end end end [ list, code ] end.to_h end |
#permitted_params ⇒ Object
227 228 229 230 231 232 233 234 235 |
# File 'lib/api/scheme.rb', line 227 def permitted_params param_map = _getter(:@param_map) if param_map.is_a?(Hash) permitted_params_require(param_map.keys.first, param_map.values.first) else permitted_params_permit([ param_map ].flatten) end end |
#permitted_params_permit(keys) ⇒ Object
218 219 220 221 222 223 224 225 |
# File 'lib/api/scheme.rb', line 218 def permitted_params_permit keys default = {} keys.reduce(default) do |h, key| value = /(?<name>.*)\?$/ =~ key ? params[name] : params.require(key) value.nil? && h || h.merge( (name || key).to_sym => value ) end end |
#permitted_params_require(key, map) ⇒ Object
214 215 216 |
# File 'lib/api/scheme.rb', line 214 def permitted_params_require key, map params.require(key).permit(*map) end |
#render_default_error(e) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/api/scheme.rb', line 159 def render_default_error e code = if e.to_s.split('::').last == 'Validations' get_code_of_model_error_map(e) else get_code_of_error_map(e) end error_proces = _getter(:@error_proces) error_proc = error_proces[code] || error_proc = _getter(:@error_proc) prc = error_proc.kind_of?(Proc) && error_proc || self.method(error_proc.to_s.to_sym) args = [get_code_text(code), get_sub_code(code), get_pure_code(code), code, e ] prc[*args[0...prc.arity.abs]] end |
#render_default_success(data, options = {}) ⇒ Object
151 152 153 154 155 156 157 |
# File 'lib/api/scheme.rb', line 151 def render_default_success data, = {} success_proc = _getter(:@success_proc) prc = success_proc.kind_of?(Proc) && success_proc || self.method(success_proc.to_s.to_sym) prc[data, ] end |
#validate_access_token ⇒ Object
237 238 239 |
# File 'lib/api/scheme.rb', line 237 def validate_access_token raise InvalidUserError if not current_user end |