Class: Fdoc::Endpoint

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

Overview

Endpoints represent the schema for an API endpoint The #consume_* methods will raise exceptions if input differs from the schema

Direct Known Subclasses

EndpointScaffold

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Endpoint.



10
11
12
13
14
# File 'lib/fdoc/endpoint.rb', line 10

def initialize(endpoint_path, service=Fdoc::Service.default_service)
  @endpoint_path = endpoint_path
  @schema = YAML.load_file(@endpoint_path)
  @service = service
end

Instance Attribute Details

#endpoint_pathObject (readonly)

Returns the value of attribute endpoint_path.



8
9
10
# File 'lib/fdoc/endpoint.rb', line 8

def endpoint_path
  @endpoint_path
end

#serviceObject (readonly)

Returns the value of attribute service.



7
8
9
# File 'lib/fdoc/endpoint.rb', line 7

def service
  @service
end

Instance Method Details

#consume_request(params, successful = true) ⇒ Object



16
17
18
19
20
21
# File 'lib/fdoc/endpoint.rb', line 16

def consume_request(params, successful=true)
  if successful
    schema = set_additional_properties_false_on(request_parameters.dup)
    JSON::Validator.validate!(schema, stringify_keys(params))
  end
end

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fdoc/endpoint.rb', line 23

def consume_response(params, status_code, successful=true)
  response_code = response_codes.find do |rc|
    rc["successful"] == successful && (
      rc["status"]      == status_code || # 200
      rc["status"].to_i == status_code    # "200 OK"
    )
  end


  if !response_code
    raise Fdoc::UndocumentedResponseCode,
      'Undocumented response: %s, successful: %s' % [
        status_code, successful
      ]
  elsif successful
    schema = set_additional_properties_false_on(response_parameters.dup)
    JSON::Validator.validate!(schema, stringify_keys(params))
  else
    true
  end
end

#deprecated?Boolean

properties

Returns:

  • (Boolean)


57
58
59
# File 'lib/fdoc/endpoint.rb', line 57

def deprecated?
  @schema["deprecated"]
end

#descriptionObject



61
62
63
# File 'lib/fdoc/endpoint.rb', line 61

def description
  @schema["description"]
end

#pathObject



49
50
51
52
53
# File 'lib/fdoc/endpoint.rb', line 49

def path
  @path ||= endpoint_path.
              gsub(service.service_dir, "").
              match(/\/?(.*)[-\/][A-Z]+\.fdoc/)[1]
end

#request_parametersObject



65
66
67
# File 'lib/fdoc/endpoint.rb', line 65

def request_parameters
  @schema["requestParameters"] ||= {}
end

#response_codesObject



73
74
75
# File 'lib/fdoc/endpoint.rb', line 73

def response_codes
  @schema["responseCodes"] ||= []
end

#response_parametersObject



69
70
71
# File 'lib/fdoc/endpoint.rb', line 69

def response_parameters
  @schema["responseParameters"] ||= {}
end

#verbObject



45
46
47
# File 'lib/fdoc/endpoint.rb', line 45

def verb
  @verb ||= endpoint_path.match(/([A-Z]*)\.fdoc$/)[1]
end