lib/lurker/endpoint.rb in lurker-0.6.0 vs lib/lurker/endpoint.rb in lurker-0.6.1
- old
+ new
@@ -10,24 +10,19 @@
attr_accessor :errors
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
- )
+ @persisted = false
+ @schema = File.exist?(endpoint_path) ? load_schema : build_schema
end
def persist!
- return unless ENV['LURKER_UPGRADE']
- schema.write_to(endpoint_path)
+ schema.ordered!.write_to(endpoint_path)
+ @persisted = true
end
def indexed?
prefix.present? && description.present?
end
@@ -38,36 +33,29 @@
raise_errors!
end
def consume_request(params, successful=true)
if successful
- unless validate(request_parameters, params, 'Request')
- current_scaffold.consume_request(params, successful)
- end
+ Lurker::SchemaModifier.merge!(request_parameters, stringify_keys(params))
end
end
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"
- )
+ return validate_response(params, status_code, successful) if persisted?
+
+ if successful
+ Lurker::SchemaModifier.merge!(response_parameters, stringify_keys(params))
end
+ if !status_code_exists?(status_code, successful)
+ response_code = {
+ "status" => status_code,
+ "successful" => successful,
+ "description" => ""
+ }
- 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
+ Lurker::SchemaModifier.append!(response_codes, response_code)
end
end
def verb
@verb ||= endpoint_path.match(/([A-Z]*)\.json(\.yml)?(\.erb)?$/)[1]
@@ -114,10 +102,36 @@
@schema["responseCodes"] ||= []
end
protected
+ def persisted?
+ !!@persisted
+ end
+
+ def load_schema
+ @persisted = true
+
+ Lurker::Schema.new(
+ load_file(endpoint_path),
+ stringify_keys(extensions)
+ )
+ end
+
+ def build_schema
+ @persisted = false
+
+ Lurker::Schema.new(
+ {
+ "prefix" => "",
+ "description" => "",
+ "responseCodes" => []
+ },
+ stringify_keys(extensions)
+ )
+ end
+
def load_file(fname)
if fname.match(/\.erb$/)
context = Lurker::ErbSchemaContext.new
erb = ERB.new(IO.read(fname)).result(context.get_binding)
YAML.load(erb)
@@ -133,9 +147,30 @@
self.errors << prefix
_errors.each { |e| self.errors << "- #{e}" }
return false
end
true
+ end
+
+ def validate_response(params, status_code, successful)
+ if !status_code_exists?(status_code, successful)
+ raise Lurker::UndocumentedResponseCode,
+ 'Undocumented response: %s, successful: %s' % [
+ status_code, successful
+ ]
+ elsif successful
+ validate(response_parameters, params, 'Response')
+ else
+ true
+ end
+ end
+
+ def status_code_exists?(status_code, successful)
+ !!response_codes.detect do |code|
+ code["successful"] == successful &&
+ (code["status"] == status_code || # 200
+ code["status"].to_i == status_code) # "200 OK"
+ end
end
def raise_errors!
unless errors.empty?
raise Lurker::ValidationError.new(word_wrap((