Class: Lurker::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/lurker/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, extensions = {}, service = Lurker::Service.default_service) ⇒ Endpoint

Returns a new instance of Endpoint.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/lurker/endpoint.rb', line 12

def initialize(endpoint_path, extensions={}, service=Lurker::Service.default_service)
  @endpoint_path = endpoint_path
  @extensions = extensions
  @schema = Lurker::Schema.new(
    load_file(@endpoint_path),
    stringify_keys(extensions)
  )
  @service = service
  @errors = []
  @current_scaffold = Lurker::EndpointScaffold.new(
    "#{endpoint_path}.new", extensions, service
  )
end

Instance Attribute Details

#current_scaffoldObject (readonly)

Returns the value of attribute current_scaffold.



9
10
11
# File 'lib/lurker/endpoint.rb', line 9

def current_scaffold
  @current_scaffold
end

#endpoint_pathObject (readonly)

Returns the value of attribute endpoint_path.



9
10
11
# File 'lib/lurker/endpoint.rb', line 9

def endpoint_path
  @endpoint_path
end

#errorsObject

Returns the value of attribute errors.



10
11
12
# File 'lib/lurker/endpoint.rb', line 10

def errors
  @errors
end

#extensionsObject (readonly)

Returns the value of attribute extensions.



9
10
11
# File 'lib/lurker/endpoint.rb', line 9

def extensions
  @extensions
end

#schemaObject (readonly)

Returns the value of attribute schema.



9
10
11
# File 'lib/lurker/endpoint.rb', line 9

def schema
  @schema
end

#serviceObject (readonly)

Returns the value of attribute service.



9
10
11
# File 'lib/lurker/endpoint.rb', line 9

def service
  @service
end

Instance Method Details

#consume!(request_params, response_params, status_code, successful = true) ⇒ Object



35
36
37
38
39
# File 'lib/lurker/endpoint.rb', line 35

def consume!(request_params, response_params, status_code, successful=true)
  consume_request(request_params, successful)
  consume_response(response_params, status_code, successful)
  raise_errors!
end

#consume_request(params, successful = true) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/lurker/endpoint.rb', line 41

def consume_request(params, successful=true)
  if successful
    unless validate(request_parameters, params, 'Request')
      current_scaffold.consume_request(params, successful)
    end
  end
end

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lurker/endpoint.rb', line 49

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 Lurker::UndocumentedResponseCode,
      'Undocumented response: %s, successful: %s' % [
        status_code, successful
      ]
  elsif successful
    unless validate(response_parameters, params, 'Response')
      current_scaffold.consume_response(params, status_code, successful)
    end
  else
    true
  end
end

#deprecated?Boolean

properties

Returns:

  • (Boolean)


84
85
86
# File 'lib/lurker/endpoint.rb', line 84

def deprecated?
  @schema["deprecated"]
end

#descriptionObject



93
94
95
# File 'lib/lurker/endpoint.rb', line 93

def description
  @schema["description"]
end

#indexed?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/lurker/endpoint.rb', line 31

def indexed?
  prefix.present? && description.present?
end

#pathObject



76
77
78
79
80
# File 'lib/lurker/endpoint.rb', line 76

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

#persist!Object



26
27
28
29
# File 'lib/lurker/endpoint.rb', line 26

def persist!
  return unless ENV['LURKER_UPGRADE']
  schema.write_to(endpoint_path)
end

#prefixObject



89
90
91
# File 'lib/lurker/endpoint.rb', line 89

def prefix
  @schema["prefix"]
end

#query_paramsObject



101
102
103
# File 'lib/lurker/endpoint.rb', line 101

def query_params
  (schema.extensions['query_params'] || {})
end

#request_parametersObject



105
106
107
# File 'lib/lurker/endpoint.rb', line 105

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

#response_codesObject



113
114
115
# File 'lib/lurker/endpoint.rb', line 113

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

#response_parametersObject



109
110
111
# File 'lib/lurker/endpoint.rb', line 109

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

#url_paramsObject



97
98
99
# File 'lib/lurker/endpoint.rb', line 97

def url_params
  (schema.extensions['path_params'] || {}).reject { |k, _| ['action', 'controller', 'format'].include? k }
end

#verbObject



72
73
74
# File 'lib/lurker/endpoint.rb', line 72

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