Module: Toast::RequestHelpers
- Included in:
- CanonicalRequest, CollectionRequest, PluralAssocRequest, RackApp, SingleRequest, SingularAssocRequest
- Defined in:
- lib/toast/request_helpers.rb
Instance Method Summary collapse
- #allowed_methods(config) ⇒ Object
- #attr_selected?(name) ⇒ Boolean
- #base_uri(request) ⇒ Object
- #call_allow(procs, *args) ⇒ Object
- #call_handler(proc, *args) ⇒ Object
- #get_config(model_class) ⇒ Object
- #is_active_record?(klass) ⇒ Boolean
- #represent(one_or_many_records, config) ⇒ Object
- #represent_one(record, config) ⇒ Object
-
#response(status_sym, headers: {}, msg: nil, body: nil) ⇒ Object
Builds a Rack conform response tripel.
-
#split_link_header(link) ⇒ Object
split the name and id of the resource from a LinkHeader.
Instance Method Details
#allowed_methods(config) ⇒ Object
75 76 77 78 79 |
# File 'lib/toast/request_helpers.rb', line 75 def allowed_methods(config) ["DELETE", "GET", "LINK", "PATCH", "POST", "UNLINK"].select{|m| !config.send("via_#{m.downcase}").nil? }.join(", ") end |
#attr_selected?(name) ⇒ Boolean
143 144 145 |
# File 'lib/toast/request_helpers.rb', line 143 def attr_selected? name (@selected_attributes.nil? or @selected_attributes.include?(name.to_s)) end |
#base_uri(request) ⇒ Object
11 12 13 |
# File 'lib/toast/request_helpers.rb', line 11 def base_uri request Toast.base_uri end |
#call_allow(procs, *args) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/toast/request_helpers.rb', line 105 def call_allow procs, *args procs.each do |proc| # call all procs, break if proc returns false and raise begin result = Object.new.instance_exec *args, &proc rescue => error raise Toast::Errors::AllowError.new(error, proc.source_location.join(':')) end if result == false raise Toast::Errors::NotAllowed.new(proc.source_location.join(':')) end end end |
#call_handler(proc, *args) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/toast/request_helpers.rb', line 120 def call_handler proc, *args result = nil begin context = Object.new context.define_singleton_method(:bad_request) do |, = {}| raise Toast::Errors::BadRequest.new , caller.first.sub(/:in.*/,''), [:code] end result = context.instance_exec *args, &proc rescue Toast::Errors::BadRequest raise # re-raise rescue => error raise Toast::Errors::HandlerError.new(error, error.backtrace.first.try(:sub,/:in.*/,'')) end result end |
#get_config(model_class) ⇒ Object
5 6 7 8 9 |
# File 'lib/toast/request_helpers.rb', line 5 def get_config model_class Toast.expositions.detect do |exp| exp.model_class.name == model_class.name end || raise(Toast::Errors::ConfigNotFound) end |
#is_active_record?(klass) ⇒ Boolean
139 140 141 |
# File 'lib/toast/request_helpers.rb', line 139 def is_active_record? klass klass.include? ActiveRecord::Core end |
#represent(one_or_many_records, config) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/toast/request_helpers.rb', line 62 def represent one_or_many_records, config result = if one_or_many_records.respond_to?(:map) one_or_many_records.map do |record| represent_one(record, config) end else represent_one(one_or_many_records, config) end result.to_json end |
#represent_one(record, config) ⇒ Object
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 59 60 |
# File 'lib/toast/request_helpers.rb', line 20 def represent_one record, config result = {} model_uri = [@base_uri, config.prefix_path, record.class.name.underscore.pluralize].delete_if(&:blank?).join('/') # can we inject model_uri into the recortd, so that virtual attribute methods can use it in their result? # - setting a special property on the record :-/, OK Toast could extend all exposed classes by a special accessor, # maybe only if configured. # - calling somehow this method in different/extended context (how?) # - passing this as parameter, while not requiring all the attribute methods to accept a 2. arg (possible?) # - If the result is a String it can return a templated String like "{{ toast_uri }}", OK for single # strings, but for mebedded Hashes with Strings no, too expensive. (config.readables + config.writables).each do |attr| result[attr.to_s] = record.send(attr) if attr_selected?(attr) end result['self'] = "#{model_uri}/#{record.id}" if attr_selected?('self') # add associations, collections and singles config.associations.each do |name, config| result[name.to_s] = "#{model_uri}/#{record.id}/#{name}" if attr_selected?(name) end config.singles.each do |name, config| result[name.to_s] = "#{model_uri}/#{name}" if attr_selected?(name) end config.collections.each do |name, config| if attr_selected?(name) result[name.to_s] = if name == :all "#{model_uri}" else "#{model_uri}/#{name}" end end end result end |
#response(status_sym, headers: {}, msg: nil, body: nil) ⇒ Object
Builds a Rack conform response tripel. Should be called at the end of the request processing.
Params:
-
status_sym [Symbol] A status code name like :ok, :unauthorized, etc.
-
headers: [Hash] HTTP headers, defaults to empty Hash
-
msg: [String] A Message for Toast log file, will be included in the body,
if body is no set and app is in non-production modes
-
body: [String] The repsosne body text, default to nil
Return: Rack conform response
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/toast/request_helpers.rb', line 92 def response status_sym, headers: {}, msg: nil, body: nil Toast.logger.info "done: #{msg}" unless Rails.env == 'production' # put message in body, too, if body is free body = msg if body.blank? end [ Rack::Utils::SYMBOL_TO_STATUS_CODE[status_sym], headers, [body] ] end |
#split_link_header(link) ⇒ Object
split the name and id of the resource from a LinkHeader
16 17 18 |
# File 'lib/toast/request_helpers.rb', line 16 def split_link_header link URI(link.href).path.sub(@request.script_name,'').split('/')[1..-1] end |