Class: HaveAPI::Example

Inherits:
Object
  • Object
show all
Defined in:
lib/haveapi/example.rb

Instance Method Summary collapse

Constructor Details

#initialize(title) ⇒ Example

Returns a new instance of Example.



3
4
5
# File 'lib/haveapi/example.rb', line 3

def initialize(title)
  @title = title
end

Instance Method Details

#authorize(&block) ⇒ Object



7
8
9
# File 'lib/haveapi/example.rb', line 7

def authorize(&block)
  @authorization = block
end

#authorized?(context) ⇒ Boolean

Returns:



43
44
45
46
47
48
49
50
# 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) ⇒ Object



39
40
41
# File 'lib/haveapi/example.rb', line 39

def comment(str)
  @comment = str
end

#describe(context) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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) ⇒ Object



31
32
33
# File 'lib/haveapi/example.rb', line 31

def errors(errs)
  @errors = errs
end

#http_status(code) ⇒ Object



35
36
37
# File 'lib/haveapi/example.rb', line 35

def http_status(code)
  @http_status = code
end

#message(msg) ⇒ Object



27
28
29
# File 'lib/haveapi/example.rb', line 27

def message(msg)
  @message = msg
end

#path_params(*params) ⇒ Object



11
12
13
# File 'lib/haveapi/example.rb', line 11

def path_params(*params)
  @path_params = params
end

#provided?Boolean

Returns:



52
53
54
55
56
57
58
59
60
# 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) ⇒ Object



15
16
17
# File 'lib/haveapi/example.rb', line 15

def request(f)
  @request = f
end

#response(f) ⇒ Object



19
20
21
# File 'lib/haveapi/example.rb', line 19

def response(f)
  @response = f
end

#status(status) ⇒ Object



23
24
25
# File 'lib/haveapi/example.rb', line 23

def status(status)
  @status = status
end