Module: ApiSim::UiApp

Defined in:
lib/api_sim/ui_app.rb

Class Method Summary collapse

Class Method Details

.with_root(root) ⇒ Object



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
# File 'lib/api_sim/ui_app.rb', line 6

def self.with_root(root)
  Module.new do
    def self.registered(app)
      app.helpers ViewHelpers
      app.get "#{app.ui_root}" do
        erb :'index.html', layout: :'layout.html'
      end

      app.get "#{app.ui_root}/response/:method/*" do
        @config = matcher(faux_request(method: http_method, path: route, body: faux_body, query: request.query_string))
        erb :'responses/form.html', layout: :'layout.html'
      end

      app.get "#{app.ui_root}/requests/:method/*" do
        @config = matcher(faux_request(method: http_method, path: route, body: faux_body, query: request.query_string))

        erb :'requests/index.html', layout: :'layout.html'
      end

      app.post "#{app.ui_root}/response/:method/*" do
        @config = matcher(faux_request(method: http_method, path: route, body: faux_body, query: request.query_string))
        unless params['schema'].empty?
          @errors = JSON::Validator.fully_validate(JSON.parse(params['schema']), params['body'])
          if @errors.any?
            return erb :'responses/form.html', layout: :'layout.html'
          end
        end
        new_config = create_matcher_override(mimicked_request)

        self.class.endpoints.unshift(new_config)
        redirect to app.ui_root
      end

      app.delete "#{app.ui_root}/response/:method/*" do
        all_matching_matchers = matchers(mimicked_request)
        all_matching_matchers.each &:reset!
        non_default_matchers = all_matching_matchers.reject(&:default)
        self.class.endpoints.delete_if {|endpoint| non_default_matchers.include?(endpoint)}
        redirect to app.ui_root
      end
    end
  end
end