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
this is hard when behind a proxy relies on HTTP_X_FORWARDED* headers.
- #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
71 72 73 74 75 |
# File 'lib/toast/request_helpers.rb', line 71 def allowed_methods(config) ["DELETE", "GET", "LINK", "PATCH", "POST", "UNLINK"].select{|m| !config.send("via_#{m.downcase}").nil? }.join(", ") end |
#attr_selected?(name) ⇒ Boolean
139 140 141 |
# File 'lib/toast/request_helpers.rb', line 139 def attr_selected? name (@selected_attributes.nil? or @selected_attributes.include?(name.to_s)) end |
#base_uri(request) ⇒ Object
this is hard when behind a proxy relies on HTTP_X_FORWARDED* headers
13 14 15 16 17 18 |
# File 'lib/toast/request_helpers.rb', line 13 def base_uri request port = ":#{request.port}" unless request.port.in?([80,443]) # remove recource path part form full path (namespace remains) path = request.path.sub(request.path_parameters[:toast_path],'') (request.protocol + request.host + port.to_s + path).chomp('/') end |
#call_allow(procs, *args) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/toast/request_helpers.rb', line 101 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
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/toast/request_helpers.rb', line 116 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.*/,'') 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.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 == model_class end || raise(Toast::Errors::ConfigNotFound) end |
#is_active_record?(klass) ⇒ Boolean
135 136 137 |
# File 'lib/toast/request_helpers.rb', line 135 def is_active_record? klass klass.include? ActiveRecord::Core end |
#represent(one_or_many_records, config) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/toast/request_helpers.rb', line 58 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
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 |
# File 'lib/toast/request_helpers.rb', line 25 def represent_one record, config result = {} (config.readables + config.writables).each do |attr| result[attr.to_s] = record.send(attr) if attr_selected?(attr) end model_uri = [@base_uri, config.prefix_path, record.class.name.underscore.pluralize].delete_if(&:blank?).join('/') 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
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/toast/request_helpers.rb', line 88 def response status_sym, headers: {}, msg: nil, body: nil Toast.logger.info "[#{Thread.current.object_id}] 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
21 22 23 |
# File 'lib/toast/request_helpers.rb', line 21 def split_link_header link URI(link.href).path.sub(@request.script_name,'').split('/')[1..-1] end |