Class: Toast::RackApp

Inherits:
Object
  • Object
show all
Includes:
RequestHelpers
Defined in:
lib/toast/rack_app.rb

Instance Method Summary collapse

Methods included from RequestHelpers

#allowed_methods, #attr_selected?, #base_uri, #call_allow, #call_handler, #get_config, #is_active_record?, #represent, #represent_one, #response

Instance Method Details

#call(env) ⇒ Object



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/toast/rack_app.rb', line 16

def call(env)

  request = ActionDispatch::Request.new(env)
  
  Toast.logger.info "[#{Thread.current.object_id}] processing: <#{URI.decode(request.fullpath)}>"

  # Authentication: respond with 401 on exception or falsy return value: 
  begin
    unless (auth = Toast::ConfigDSL::Settings::AuthenticateContext.new.
                    instance_exec(request, &Toast.settings.authenticate))
       return response :unauthorized, msg: "authentication failed"
    end
  rescue Toast::Errors::CustomAuthFailure => caf   
    return response(caf.response_data[:status] || :unauthorized, 
             msg: caf.response_data[:body],
             headers: caf.response_data[:headers])
  rescue => error
    return response :unauthorized, msg: "authentication failed: `#{error.message}'"
  end

  path       = request.path_parameters[:toast_path].split('/')
  
  # look up requested model
  model_class = resolve_model(path, Toast.path_tree)

  if model_class.nil?
    return response :not_found,
                    msg: "no API configuration found for endpoint /#{path.join('/')}"
  end

  # select base configuration
  base_config = get_config(model_class)

  # remove path prefix
  path = path[(base_config.prefix_path.length)..-1]

  toast_request =
    case path.length
    when 1 # root collection: /apples

      if base_config.collections[:all].nil?
        return response :not_found, msg: "collection `/#{path[0]}' not configured"
      else
        # root collection
        Toast::CollectionRequest.new( base_config.collections[:all],
                                      base_config,
                                      auth,
                                      request)
      end
    when 2 # canonical, single or collection: /apples/10 , /apples/first, /apples/red_ones
      if path.second =~ /\A\d+\z/
        Toast::CanonicalRequest.new( path.second.to_i,
                                     base_config,
                                     auth,
                                     request )
      else
        if col_config = base_config.collections[path.second.to_sym]
          Toast::CollectionRequest.new(col_config,
                                       base_config,
                                       auth,
                                       request)

        elsif sin_config = base_config.singles[path.second.to_sym]
          Toast::SingleRequest.new(sin_config,
                                   base_config,
                                   auth,
                                   request)
        else
          return response :not_found,
                          msg: "collection or single `#{path.second}' not configured in: #{base_config.source_location}"
        end
      end

    when 3 # association: /apples/10/tree, /tree/5/apples

      if assoc_config = base_config.associations[path.third.to_sym]
        if assoc_config.singular

          Toast::SingularAssocRequest.new( path.second.to_i,
                                           assoc_config,
                                           base_config,
                                           auth,
                                           request )

        else
          # process_plural_association assoc_config, path.second.to_i
          Toast::PluralAssocRequest.new( path.second.to_i,
                                         assoc_config,
                                         base_config,
                                         auth,
                                         request )
        end
      else
        return response :not_found,
                        msg: "association `#{model_class.name}##{path.third}' not configured"
      end

    else
      return response :not_found,
                      msg: "#{request.url} not found (invalid path)"
    end

  toast_request.respond

end