Class: Lurker::Endpoint
- Inherits:
-
Object
- Object
- Lurker::Endpoint
- 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
Instance Attribute Summary collapse
-
#current_scaffold ⇒ Object
readonly
Returns the value of attribute current_scaffold.
-
#endpoint_path ⇒ Object
readonly
Returns the value of attribute endpoint_path.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#extensions ⇒ Object
readonly
Returns the value of attribute extensions.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
Instance Method Summary collapse
- #consume!(request_params, response_params, status_code, successful = true) ⇒ Object
- #consume_request(params, successful = true) ⇒ Object
- #consume_response(params, status_code, successful = true) ⇒ Object
-
#deprecated? ⇒ Boolean
properties.
- #description ⇒ Object
- #indexed? ⇒ Boolean
-
#initialize(endpoint_path, extensions = {}, service = Lurker::Service.default_service) ⇒ Endpoint
constructor
A new instance of Endpoint.
- #path ⇒ Object
- #persist! ⇒ Object
- #prefix ⇒ Object
- #query_params ⇒ Object
- #request_parameters ⇒ Object
- #response_codes ⇒ Object
- #response_parameters ⇒ Object
- #url_params ⇒ Object
- #verb ⇒ Object
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_scaffold ⇒ Object (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_path ⇒ Object (readonly)
Returns the value of attribute endpoint_path.
9 10 11 |
# File 'lib/lurker/endpoint.rb', line 9 def endpoint_path @endpoint_path end |
#errors ⇒ Object
Returns the value of attribute errors.
10 11 12 |
# File 'lib/lurker/endpoint.rb', line 10 def errors @errors end |
#extensions ⇒ Object (readonly)
Returns the value of attribute extensions.
9 10 11 |
# File 'lib/lurker/endpoint.rb', line 9 def extensions @extensions end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
9 10 11 |
# File 'lib/lurker/endpoint.rb', line 9 def schema @schema end |
#service ⇒ Object (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
84 85 86 |
# File 'lib/lurker/endpoint.rb', line 84 def deprecated? @schema["deprecated"] end |
#description ⇒ Object
93 94 95 |
# File 'lib/lurker/endpoint.rb', line 93 def description @schema["description"] end |
#indexed? ⇒ Boolean
31 32 33 |
# File 'lib/lurker/endpoint.rb', line 31 def indexed? prefix.present? && description.present? end |
#path ⇒ Object
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 |
#prefix ⇒ Object
89 90 91 |
# File 'lib/lurker/endpoint.rb', line 89 def prefix @schema["prefix"] end |
#query_params ⇒ Object
101 102 103 |
# File 'lib/lurker/endpoint.rb', line 101 def query_params (schema.extensions['query_params'] || {}) end |
#request_parameters ⇒ Object
105 106 107 |
# File 'lib/lurker/endpoint.rb', line 105 def request_parameters @schema["requestParameters"] ||= {} end |
#response_codes ⇒ Object
113 114 115 |
# File 'lib/lurker/endpoint.rb', line 113 def response_codes @schema["responseCodes"] ||= [] end |
#response_parameters ⇒ Object
109 110 111 |
# File 'lib/lurker/endpoint.rb', line 109 def response_parameters @schema["responseParameters"] ||= {} end |
#url_params ⇒ Object
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 |
#verb ⇒ Object
72 73 74 |
# File 'lib/lurker/endpoint.rb', line 72 def verb @verb ||= endpoint_path.match(/([A-Z]*)\.json(\.yml)?(\.erb)?$/)[1] end |