Class: ApiSchema::ResourceDefinition

Inherits:
Object
  • Object
show all
Includes:
Swagger::Blocks::ClassMethods
Defined in:
lib/api_schema/resource_definition.rb

Defined Under Namespace

Classes: HeaderParam, PathParam, QueryParam

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, api_version, base_path, extra_path = nil) ⇒ ResourceDefinition

Returns a new instance of ResourceDefinition.



5
6
7
8
9
10
11
12
13
# File 'lib/api_schema/resource_definition.rb', line 5

def initialize(method, api_version, base_path, extra_path = nil)
  @base_path = base_path
  @extra_path = extra_path
  @method = method
  @api_version = api_version
  @header_params = []
  @path_params = []
  @query_params = []
end

Instance Attribute Details

#api_versionObject (readonly)

Returns the value of attribute api_version.



19
20
21
# File 'lib/api_schema/resource_definition.rb', line 19

def api_version
  @api_version
end

#base_pathObject (readonly)

Returns the value of attribute base_path.



19
20
21
# File 'lib/api_schema/resource_definition.rb', line 19

def base_path
  @base_path
end

#body_paramObject (readonly)

Returns the value of attribute body_param.



19
20
21
# File 'lib/api_schema/resource_definition.rb', line 19

def body_param
  @body_param
end

#descriptionObject (readonly)

Returns the value of attribute description.



19
20
21
# File 'lib/api_schema/resource_definition.rb', line 19

def description
  @description
end

#errorsObject (readonly)

Returns the value of attribute errors.



19
20
21
# File 'lib/api_schema/resource_definition.rb', line 19

def errors
  @errors
end

#extra_pathObject (readonly)

Returns the value of attribute extra_path.



19
20
21
# File 'lib/api_schema/resource_definition.rb', line 19

def extra_path
  @extra_path
end

#full_pathObject (readonly)

Returns the value of attribute full_path.



19
20
21
# File 'lib/api_schema/resource_definition.rb', line 19

def full_path
  @full_path
end

#header_paramsObject (readonly)

Returns the value of attribute header_params.



19
20
21
# File 'lib/api_schema/resource_definition.rb', line 19

def header_params
  @header_params
end

#methodObject (readonly)

Returns the value of attribute method.



19
20
21
# File 'lib/api_schema/resource_definition.rb', line 19

def method
  @method
end

#path_paramsObject (readonly)

Returns the value of attribute path_params.



19
20
21
# File 'lib/api_schema/resource_definition.rb', line 19

def path_params
  @path_params
end

#query_paramsObject (readonly)

Returns the value of attribute query_params.



19
20
21
# File 'lib/api_schema/resource_definition.rb', line 19

def query_params
  @query_params
end

#respObject (readonly)

Returns the value of attribute resp.



19
20
21
# File 'lib/api_schema/resource_definition.rb', line 19

def resp
  @resp
end

#summaryObject (readonly)

Returns the value of attribute summary.



19
20
21
# File 'lib/api_schema/resource_definition.rb', line 19

def summary
  @summary
end

Instance Method Details

#body(body_param) ⇒ Object



39
40
41
# File 'lib/api_schema/resource_definition.rb', line 39

def body(body_param)
  @body_param = body_param
end

#build(neighbors) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/api_schema/resource_definition.rb', line 85

def build(neighbors)
  error_model = :error_model
  error_desc = {
    '401' => "Unauthorized",
    '403' => "Forbidden",
    '404' => "Not found",
    '422' => "Unprocessable Entity"
 }
  resource = self
  swagger_path resource.full_path do
    neighbors[resource.full_path].each do |r|
      operation(r.method) do
        key :summary, r.summary
        key :description, r.description
        key :operationId, "#{r.method}_#{r.full_path}"
        key :tags, r.base_path
        security do
          key :authorization, []
        end
        body_param(r.body_param) if r.with_body?

        r.header_params.each do |p|
          header_param(p.name, p.type, p.required)
        end
        r.path_params.each do |p|
          path_param(p.name, p.type, p.required)
        end
        r.query_params.each do |p|
          query_param(p.name, p.type, p.required)
        end

        success_response(r.resp.code, r.resp.model, r.resp.fields)
        error_responses(error_model, error_desc, *r.errors) if r.with_errors?
      end
    end
  end
end

#build_neighbors(neighbors) ⇒ Object



79
80
81
82
83
# File 'lib/api_schema/resource_definition.rb', line 79

def build_neighbors(neighbors)
  generate_full_path
  neighbors[full_path] ||= []
  neighbors[full_path] << self
end

#desc(desc) ⇒ Object



27
28
29
# File 'lib/api_schema/resource_definition.rb', line 27

def desc(desc)
  @description = desc
end

#desc_file(desc_file) ⇒ Object



31
32
33
# File 'lib/api_schema/resource_definition.rb', line 31

def desc_file(desc_file)
  @description = IO.read("#{api_version.configuration.descriptions_path}/#{desc_file}.md")
end

#error!(*codes) ⇒ Object



58
59
60
# File 'lib/api_schema/resource_definition.rb', line 58

def error!(*codes)
  @errors = *codes
end

#generate_full_pathObject



74
75
76
77
# File 'lib/api_schema/resource_definition.rb', line 74

def generate_full_path
  @full_path = with_path_param? ? "/#{base_path}/{id}" : "/#{base_path}"
  @full_path << "/#{extra_path}" if extra_path
end

#header(name, type, required: true) ⇒ Object



35
36
37
# File 'lib/api_schema/resource_definition.rb', line 35

def header(name, type, required: true)
  @header_params << HeaderParam.new(name, type, required)
end

#name(name) ⇒ Object



23
24
25
# File 'lib/api_schema/resource_definition.rb', line 23

def name(name)
  @summary = name
end

#path_param(name, type, required: true) ⇒ Object



43
44
45
# File 'lib/api_schema/resource_definition.rb', line 43

def path_param(name, type, required: true)
  @path_params << PathParam.new(name, type, required)
end

#query_param(name, type, required: true) ⇒ Object



47
48
49
# File 'lib/api_schema/resource_definition.rb', line 47

def query_param(name, type, required: true)
  @query_params << QueryParam.new(name, type, required)
end

#response(code, model_name = nil, &block) ⇒ Object



51
52
53
54
55
56
# File 'lib/api_schema/resource_definition.rb', line 51

def response(code, model_name = nil, &block)
  @resp = Response.new(code, model_name)
  if block && model_name.nil?
    block.call(@resp)
  end
end

#with_body?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/api_schema/resource_definition.rb', line 66

def with_body?
  !!body_param
end

#with_errors?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/api_schema/resource_definition.rb', line 70

def with_errors?
  !errors.empty?
end

#with_path_param?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/api_schema/resource_definition.rb', line 62

def with_path_param?
  !path_params.empty?
end