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(record_or_enum, config) ⇒ Object
- #represent_one(record, config) ⇒ Object
-
#response(status_sym, headers: {}, msg: nil, body: nil) ⇒ Object
Builds a Rack conform response tripel.
Instance Method Details
#allowed_methods(config) ⇒ Object
63 64 65 66 67 |
# File 'lib/toast/request_helpers.rb', line 63 def allowed_methods(config) ["DELETE", "GET", "LINK", "PATCH", "POST", "UNLINK"].select{|m| !config.send("via_#{m.downcase}").nil? }.join(", ") end |
#attr_selected?(name) ⇒ Boolean
131 132 133 |
# File 'lib/toast/request_helpers.rb', line 131 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 |
# File 'lib/toast/request_helpers.rb', line 13 def base_uri request port = ":#{request.port}" unless request.port.in?([80,443]) path = request.path.sub(request.path_parameters[:toast_path]+'/','') (request.protocol + request.host + port.to_s + path).chomp('/') end |
#call_allow(procs, *args) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/toast/request_helpers.rb', line 93 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
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/toast/request_helpers.rb', line 108 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
127 128 129 |
# File 'lib/toast/request_helpers.rb', line 127 def is_active_record? klass klass.include? ActiveRecord::Core end |
#represent(record_or_enum, config) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/toast/request_helpers.rb', line 51 def represent record_or_enum, config result = if record_or_enum.is_a? Enumerable record_or_enum.map do |record| represent_one(record, config) end else represent_one(record_or_enum, config) end result.to_json end |
#represent_one(record, config) ⇒ Object
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 |
# File 'lib/toast/request_helpers.rb', line 19 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}/#{record.class.name.underscore.pluralize}" 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
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/toast/request_helpers.rb', line 80 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 end [ Rack::Utils::SYMBOL_TO_STATUS_CODE[status_sym], headers, [body] ] end |