class HaveAPI::Example

Public Class Methods

new(title) click to toggle source
# File lib/haveapi/example.rb, line 3
def initialize(title)
  @title = title
end

Public Instance Methods

authorize(&block) click to toggle source
# File lib/haveapi/example.rb, line 7
def authorize(&block)
  @authorization = block
end
authorized?(context) click to toggle source
# File lib/haveapi/example.rb, line 43
def authorized?(context)
  if (context.endpoint || context.current_user) \
      && @authorization && !@authorization.call(context.current_user)
    false
  else
    true
  end
end
comment(str) click to toggle source
# File lib/haveapi/example.rb, line 39
def comment(str)
  @comment = str
end
describe(context) click to toggle source
# File lib/haveapi/example.rb, line 62
def describe(context)
  if provided?
    {
      title: @title,
      comment: @comment,
      path_params: @path_params,
      request: filter_input_params(context, @request),
      response: filter_output_params(context, @response),
      status: @status.nil? ? true : @status,
      message: @message,
      errors: @errors,
      http_status: @http_status || 200
    }
  else
    {}
  end
end
errors(errs) click to toggle source
# File lib/haveapi/example.rb, line 31
def errors(errs)
  @errors = errs
end
http_status(code) click to toggle source
# File lib/haveapi/example.rb, line 35
def http_status(code)
  @http_status = code
end
message(msg) click to toggle source
# File lib/haveapi/example.rb, line 27
def message(msg)
  @message = msg
end
path_params(*params) click to toggle source
# File lib/haveapi/example.rb, line 11
def path_params(*params)
  @path_params = params
end
provided?() click to toggle source
# File lib/haveapi/example.rb, line 52
def provided?
  if instance_variables.detect do |v|
    instance_variable_get(v)
  end
    true
  else
    false
  end
end
request(f) click to toggle source
# File lib/haveapi/example.rb, line 15
def request(f)
  @request = f
end
response(f) click to toggle source
# File lib/haveapi/example.rb, line 19
def response(f)
  @response = f
end
status(status) click to toggle source
# File lib/haveapi/example.rb, line 23
def status(status)
  @status = status
end

Protected Instance Methods

filter_input_params(context, input) click to toggle source
# File lib/haveapi/example.rb, line 82
def filter_input_params(context, input)
  case context.action.input.layout
  when :object, :hash
    context.authorization.filter_input(
      context.action.input.params,
      ModelAdapters::Hash.output(context, input)
    )

  when :object_list, :hash_list
    input.map do |obj|
      context.authorization.filter_input(
        context.action.input.params,
        ModelAdapters::Hash.output(context, obj),
        true
      )
    end
  end
end
filter_output_params(context, output) click to toggle source
# File lib/haveapi/example.rb, line 101
def filter_output_params(context, output)
  case context.action.output.layout
  when :object, :hash
    context.authorization.filter_output(
      context.action.output.params,
      ModelAdapters::Hash.output(context, output),
      true
    )

  when :object_list, :hash_list
    output.map do |obj|
      context.authorization.filter_output(
        context.action.output.params,
        ModelAdapters::Hash.output(context, obj),
        true
      )
    end
  end
end