Class: Praxis::Docs::OpenApi::PathsObject

Inherits:
Object
  • Object
show all
Defined in:
lib/praxis/docs/open_api/paths_object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resources:) ⇒ PathsObject

Returns a new instance of PathsObject.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/praxis/docs/open_api/paths_object.rb', line 11

def initialize(resources:)
  @resources = resources
  # A hash with keys of paths, and values of hash
  # where the subhash has verb keys and path_items as values
  # {
  #   "/pets": {
  #       "get": {...},
  #       "post": { ...}
  #   "/humans": {
  #       "get": {...},
  @paths = Hash.new { |h, k| h[k] = {} }
end

Instance Attribute Details

#pathsObject (readonly)



9
10
11
# File 'lib/praxis/docs/open_api/paths_object.rb', line 9

def paths
  @paths
end

#resourcesObject (readonly)



9
10
11
# File 'lib/praxis/docs/open_api/paths_object.rb', line 9

def resources
  @resources
end

Instance Method Details

#compute_resource_paths(resource) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/praxis/docs/open_api/paths_object.rb', line 31

def compute_resource_paths(resource)
  id = resource.id
  # fill in the paths hash with a key for each path for each action/route
  resource.actions.each do |action_name, action|
    params_example = action.params&.example(nil)
    url = ActionDefinition.url_description(route: action.route, params: action.params, params_example: params_example)

    verb = url[:verb].downcase
    templetized_path = OpenApiGenerator.templatize_url(url[:path])
    path_entry = paths[templetized_path]
    # Let's fill in verb stuff within the working hash
    raise "VERB #{verb} already defined for #{id}!?!?!" if path_entry[verb]

    action_uid = "action-#{action_name}-#{id}"
    # Add a tag matching the resource name (hoping all actions of a resource are grouped)
    action_tags = [resource.display_name]
    path_entry[verb] = OperationObject.new(id: action_uid, url: url, action: action, tags: action_tags).dump
  end
  # For each path, we can further annotate with
  # servers
  # parameters
  # But we don't have that concept in praxis
end

#dumpObject



24
25
26
27
28
29
# File 'lib/praxis/docs/open_api/paths_object.rb', line 24

def dump
  resources.each do |resource|
    compute_resource_paths(resource)
  end
  paths
end