class Lurker::Endpoint

Constants

ACTION
CONTROLLER
DEPRECATED
DESCRIPTION
DESCRIPTIONS
EXTENSIONS
PATH_PARAMS
PREFIX
QUERY_PARAMS
REQUEST_PARAMETERS
RESPONSE_CODES
RESPONSE_PARAMETERS

Attributes

endpoint_path[R]
extensions[R]
schema[R]
service[R]

Public Class Methods

new(endpoint_path, extensions = {}, service = Lurker::Service.default_service) click to toggle source
# 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

Public Instance Methods

consume!(request_params, response_params, status_code, successful = true) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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?() click to toggle source

properties

# File lib/lurker/endpoint.rb, line 102
def deprecated?
  @schema[DEPRECATED]
end
description() click to toggle source
# File lib/lurker/endpoint.rb, line 110
def description
  @schema[DESCRIPTION]
end
documentation() click to toggle source
# File lib/lurker/endpoint.rb, line 134
def documentation
  @schema.documentation
end
indexed?() click to toggle source
# File lib/lurker/endpoint.rb, line 52
def indexed?
  prefix.present? && description.present?
end
path() click to toggle source
# 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!() click to toggle source
# 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() click to toggle source
# File lib/lurker/endpoint.rb, line 106
def prefix
  @schema[PREFIX]
end
query_params() click to toggle source
# File lib/lurker/endpoint.rb, line 118
def query_params
  (@schema[EXTENSIONS][QUERY_PARAMS] || {})
end
request_parameters() click to toggle source
# File lib/lurker/endpoint.rb, line 122
def request_parameters
  @schema[REQUEST_PARAMETERS]
end
response_codes() click to toggle source
# File lib/lurker/endpoint.rb, line 130
def response_codes
  @schema[RESPONSE_CODES]
end
response_parameters() click to toggle source
# File lib/lurker/endpoint.rb, line 126
def response_parameters
  @schema[RESPONSE_PARAMETERS]
end
url_params() click to toggle source
# File lib/lurker/endpoint.rb, line 114
def url_params
  (@schema[EXTENSIONS][PATH_PARAMS] || {}).reject { |k, _| %w(action controller format).include? k }
end
verb() click to toggle source
# File lib/lurker/endpoint.rb, line 90
def verb
  @verb ||= endpoint_path.match(/([A-Z]*)\.json(\.yml)?(\.erb)?$/)[1]
end

Protected Instance Methods

build_schema() click to toggle source
# File lib/lurker/endpoint.rb, line 151
def build_schema
  @persisted = false

  payload = {
    DESCRIPTION => '',
    PREFIX => '',
    REQUEST_PARAMETERS => {},
    RESPONSE_CODES => [],
    RESPONSE_PARAMETERS => {}
  }
  schemify(payload)
end
finalize_schema!() click to toggle source
# File lib/lurker/endpoint.rb, line 171
def finalize_schema!
  path_params = schema[EXTENSIONS][PATH_PARAMS] || {}
  subject = path_params[CONTROLLER].to_s.split(/\//).last.to_s
  description = DESCRIPTIONS[path_params[ACTION]]

  schema[DESCRIPTION] = "#{subject.singularize} #{description}".strip if schema[DESCRIPTION].blank?
  schema[PREFIX] = "#{subject} management" if schema[PREFIX].blank?
end
load_schema() click to toggle source
# File lib/lurker/endpoint.rb, line 144
def load_schema
  @persisted = true

  reader = Lurker::Json::Reader.new(endpoint_path)
  schemify(reader.payload)
end
persisted?() click to toggle source
# File lib/lurker/endpoint.rb, line 140
def persisted?
  !!@persisted
end
raise_errors!() click to toggle source
# File lib/lurker/endpoint.rb, line 180
def raise_errors!
  return if @response_errors.empty?

  errors = (@request_errors | @response_errors) * "\n"
  exception = Lurker::ValidationError.new(word_wrap errors)
  if (example = Lurker::Spy.current.block).respond_to?(:metadata) && (metadata = example.metadata).respond_to?(:location, true)
    exception.set_backtrace [metadata.send(:location)]
  end

  raise exception
end
schemify(payload) click to toggle source
# File lib/lurker/endpoint.rb, line 164
def schemify(payload)
  Lurker::Json::Parser.plain(uri: endpoint_path).parse(payload).tap do |schm|
    ext = Lurker::Json::Extensions.new(stringify_keys extensions)
    schm.merge!(EXTENSIONS => ext)
  end
end
word_wrap(text) click to toggle source
# File lib/lurker/endpoint.rb, line 192
def word_wrap(text)
  # strip .json# | .json.yml# | .json.yml.erb#
  text = text.reverse
  text.gsub!(/(\n|^)#bre\./, "\nbre.")   # erb
  text.gsub!(/(\n|^)#lmy\./, "\nlmy.")   # yml
  text.gsub!(/(\n|^)#nosj\./, "\nnosj.") # json
  text.strip!
  text = text.reverse

  text.gsub!(/\s+in schema/m, "\n  in schema")
  if defined?(Rails)
    text.gsub!(Regexp.new("#{Rails.root}\/"), "")
  end
  text
end