Class: Fdoc::EndpointScaffold

Inherits:
Endpoint
  • Object
show all
Defined in:
lib/fdoc/endpoint_scaffold.rb

Overview

EndpointScaffolds aggregate input to guess at the structure of an API endpoint. The #consume_* methods can modify the structure of the in-memory endpoint, to save the results to the file system, call #persist!

Instance Attribute Summary

Attributes inherited from Endpoint

#endpoint_path, #service

Instance Method Summary collapse

Methods inherited from Endpoint

#deprecated?, #description, #path, #request_parameters, #response_codes, #response_parameters, #verb

Constructor Details

#initialize(endpoint_path, service = Fdoc::Service.default_service) ⇒ EndpointScaffold

Returns a new instance of EndpointScaffold.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fdoc/endpoint_scaffold.rb', line 5

def initialize(endpoint_path, service=Fdoc::Service.default_service)
  if File.exist?(endpoint_path)
    super
  else
    @endpoint_path = endpoint_path
    @schema = {
      "description" => "???",
      "responseCodes" => []
    }
    @service = service
  end
end

Instance Method Details

#consume_request(params, successful = true) ⇒ Object



27
28
29
30
31
# File 'lib/fdoc/endpoint_scaffold.rb', line 27

def consume_request(params, successful = true)
  scaffold_schema(request_parameters, stringify_keys(params), {
    :root_object => true
  })
end

#consume_response(params, status_code, successful = true) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fdoc/endpoint_scaffold.rb', line 33

def consume_response(params, status_code, successful=true)
  if successful
    scaffold_schema(response_parameters, stringify_keys(params), {
      :root_object => true
    })
  end

  response_code = response_codes.find do
    |rc| rc["status"] == status_code && rc["successful"] == successful
  end

  if !response_code
    response_codes << {
      "status" => status_code,
      "successful" => successful,
      "description" => "???"
    }
  end
end

#persist!Object



18
19
20
21
22
23
24
25
# File 'lib/fdoc/endpoint_scaffold.rb', line 18

def persist!
  dirname = File.dirname(@endpoint_path)
  FileUtils.mkdir_p(dirname) unless File.directory?(dirname)

  File.open(@endpoint_path, "w") do |file|
    YAML.dump(@schema, file)
  end
end