Class: ApiSim::AppBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/api_sim/app_builder.rb

Constant Summary collapse

NOT_FOUND =
[nil, [404, {}, 'NOT FOUND']]

Instance Method Summary collapse

Instance Method Details

#configure_dynamic_endpoint(http_method, route, response_logic) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/api_sim/app_builder.rb', line 36

def configure_dynamic_endpoint(http_method, route, response_logic)
  endpoint_configurations.push(
    Matchers::DynamicRequestMatcher.new(
      http_method: http_method,
      route: route,
      default: true,
      response_generator: response_logic
    )
  )
end

#configure_endpoint(http_method, route, response_body, response_code = 200, headers = {}, schema_string = '', request_schema: nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/api_sim/app_builder.rb', line 21

def configure_endpoint(http_method, route, response_body, response_code=200, headers={}, schema_string='', request_schema: nil)
  endpoint_configurations.push(
    Matchers::StaticRequestMatcher.new(
      http_method: http_method,
      route: route,
      response_code: response_code,
      headers: headers,
      default: true,
      response_body: response_body,
      schema: schema_string,
      request_schema: request_schema
    )
  )
end

#configure_fixture_directory(dir) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/api_sim/app_builder.rb', line 63

def configure_fixture_directory(dir)
  dir = dir.chomp('/')
  Dir[File.join(dir, "**/*.json.erb")].each do |path|
    endpoint_match = path.match(%r{#{dir}([/\w+\_\-]+)/(GET|POST|PATCH|OPTIONS|HEAD|PUT|DELETE).json})
    config = JSON.parse(File.read(path))
    configure_endpoint endpoint_match[2],
      endpoint_match[1],
      config['body'].to_json,
      config['status'],
      config['headers'],
      config['schema'].to_json
  end
end

#configure_matcher_endpoint(http_method, route, matchers_to_responses) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/api_sim/app_builder.rb', line 47

def configure_matcher_endpoint(http_method, route, matchers_to_responses)
  matchers_to_responses.each do |matcher, response|
    endpoint_configurations.push(
      Matchers::RequestBodyMatcher.new(
        http_method: http_method,
        route: route,
        response_code: response[0],
        headers: response[1],
        response_body: response[2],
        default: true,
        body_matches: matcher,
      )
    )
  end
end

#endpoint_configurationsObject



77
78
79
# File 'lib/api_sim/app_builder.rb', line 77

def endpoint_configurations
  @endpoint_configurations ||= []
end

#rackappObject



8
9
10
11
12
13
14
# File 'lib/api_sim/app_builder.rb', line 8

def rackapp
  config = self
  Sinatra.new(BuiltApp) do
    endpoints config.endpoint_configurations
    ui_root config.ui_root || '/ui'
  end
end

#ui_root(root = nil) ⇒ Object



16
17
18
19
# File 'lib/api_sim/app_builder.rb', line 16

def ui_root(root = nil)
  @ui_root = root if root
  @ui_root
end