Class: Lurker::Endpoint
- Inherits:
-
Object
- Object
- Lurker::Endpoint
- Includes:
- Utils
- Defined in:
- lib/lurker/endpoint.rb
Constant Summary collapse
- PREFIX =
'prefix'.freeze
- ACTION =
'action'.freeze
- CONTROLLER =
'controller'.freeze
- EXTENSIONS =
'extensions'.freeze
- PATH_PARAMS =
'path_params'.freeze
- QUERY_PARAMS =
'query_params'.freeze
- DEPRECATED =
'deprecated'.freeze
- DESCRIPTION =
'description'.freeze
- RESPONSE_CODES =
'responseCodes'.freeze
- REQUEST_PARAMETERS =
'requestParameters'.freeze
- RESPONSE_PARAMETERS =
'responseParameters'.freeze
- DESCRIPTIONS =
{ 'index' => 'listing', 'show' => '', 'edit' => 'editing', 'create' => 'creation', 'update' => 'updating', 'destroy' => 'descruction' }.freeze
Instance Attribute Summary collapse
-
#endpoint_path ⇒ Object
readonly
Returns the value of attribute endpoint_path.
-
#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
- #documentation ⇒ 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
Methods included from Utils
Constructor Details
#initialize(endpoint_path, extensions = {}, service = Lurker::Service.default_service) ⇒ Endpoint
Returns a new instance of Endpoint.
33 34 35 36 37 38 39 40 41 |
# File 'lib/lurker/endpoint.rb', line 33 def initialize(endpoint_path, extensions = {}, service = Lurker::Service.default_service) @endpoint_path = endpoint_path @extensions = extensions @service = service @persisted = false @schema = File.exist?(endpoint_path) ? load_schema : build_schema @request_errors = [] @response_errors = [] end |
Instance Attribute Details
#endpoint_path ⇒ Object (readonly)
Returns the value of attribute endpoint_path.
31 32 33 |
# File 'lib/lurker/endpoint.rb', line 31 def endpoint_path @endpoint_path end |
#extensions ⇒ Object (readonly)
Returns the value of attribute extensions.
31 32 33 |
# File 'lib/lurker/endpoint.rb', line 31 def extensions @extensions end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
31 32 33 |
# File 'lib/lurker/endpoint.rb', line 31 def schema @schema end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
31 32 33 |
# File 'lib/lurker/endpoint.rb', line 31 def service @service end |
Instance Method Details
#consume!(request_params, response_params, status_code, successful = true) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/lurker/endpoint.rb', line 56 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
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/lurker/endpoint.rb', line 63 def consume_request(params, successful = true) parameters = stringify_keys(params) if persisted? @request_errors = request_parameters.validate(parameters) @request_errors.unshift('Request') unless @request_errors.empty? end request_parameters.merge!(parameters) if successful end |
#consume_response(params, status_code, successful = true) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/lurker/endpoint.rb', line 74 def consume_response(params, status_code, successful = true) parameters = stringify_keys(params) if persisted? response_codes.validate!(status_code, successful) @response_errors = response_parameters.validate(parameters) @response_errors.unshift('Response') unless @response_errors.empty? return end response_parameters.merge!(parameters) if successful response_codes.merge!(status_code, successful) end |
#deprecated? ⇒ Boolean
properties
102 103 104 |
# File 'lib/lurker/endpoint.rb', line 102 def deprecated? @schema[DEPRECATED] end |
#description ⇒ Object
110 111 112 |
# File 'lib/lurker/endpoint.rb', line 110 def description @schema[DESCRIPTION] end |
#documentation ⇒ Object
134 135 136 |
# File 'lib/lurker/endpoint.rb', line 134 def documentation @schema.documentation end |
#indexed? ⇒ Boolean
52 53 54 |
# File 'lib/lurker/endpoint.rb', line 52 def indexed? prefix.present? && description.present? end |
#path ⇒ Object
94 95 96 97 98 |
# File 'lib/lurker/endpoint.rb', line 94 def path @path ||= endpoint_path. gsub(service.service_dir, ''). match(/\/?(.*)[-\/][A-Z]+\.json(\.yml)?(\.erb)?$/)[1] end |
#persist! ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/lurker/endpoint.rb', line 43 def persist! finalize_schema! Lurker::Json::Orderer.reorder(schema) unless persisted? Lurker::Json::Writer.write(schema, endpoint_path) @persisted = true end |
#prefix ⇒ Object
106 107 108 |
# File 'lib/lurker/endpoint.rb', line 106 def prefix @schema[PREFIX] end |
#query_params ⇒ Object
118 119 120 |
# File 'lib/lurker/endpoint.rb', line 118 def query_params (@schema[EXTENSIONS][QUERY_PARAMS] || {}) end |
#request_parameters ⇒ Object
122 123 124 |
# File 'lib/lurker/endpoint.rb', line 122 def request_parameters @schema[REQUEST_PARAMETERS] end |
#response_codes ⇒ Object
130 131 132 |
# File 'lib/lurker/endpoint.rb', line 130 def response_codes @schema[RESPONSE_CODES] end |
#response_parameters ⇒ Object
126 127 128 |
# File 'lib/lurker/endpoint.rb', line 126 def response_parameters @schema[RESPONSE_PARAMETERS] end |
#url_params ⇒ Object
114 115 116 |
# File 'lib/lurker/endpoint.rb', line 114 def url_params (@schema[EXTENSIONS][PATH_PARAMS] || {}).reject { |k, _| %w(action controller format).include? k } end |
#verb ⇒ Object
90 91 92 |
# File 'lib/lurker/endpoint.rb', line 90 def verb @verb ||= endpoint_path.match(/([A-Z]*)\.json(\.yml)?(\.erb)?$/)[1] end |