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



5
6
7
8
9
10
11
12
13
14
# 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 = []
  @errors = []
end

Instance Attribute Details

#api_versionObject (readonly)

Returns the value of attribute api_version.



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

def api_version
  @api_version
end

#base_pathObject (readonly)

Returns the value of attribute base_path.



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

def base_path
  @base_path
end

#body_paramObject (readonly)

Returns the value of attribute body_param.



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

def body_param
  @body_param
end

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#extra_pathObject (readonly)

Returns the value of attribute extra_path.



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

def extra_path
  @extra_path
end

#full_pathObject (readonly)

Returns the value of attribute full_path.



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

def full_path
  @full_path
end

#header_paramsObject (readonly)

Returns the value of attribute header_params.



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

def header_params
  @header_params
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#path_paramsObject (readonly)

Returns the value of attribute path_params.



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

def path_params
  @path_params
end

#query_paramsObject (readonly)

Returns the value of attribute query_params.



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

def query_params
  @query_params
end

#respObject (readonly)

Returns the value of attribute resp.



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

def resp
  @resp
end

#summaryObject (readonly)

Returns the value of attribute summary.



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

def summary
  @summary
end

Instance Method Details

#body(body_param) ⇒ Object



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

def body(body_param)
  @body_param = body_param
end

#build(neighbors) ⇒ Object



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
122
# File 'lib/api_schema/resource_definition.rb', line 86

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



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

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

#desc(desc) ⇒ Object



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

def desc(desc)
  @description = desc
end

#desc_file(desc_file) ⇒ Object



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

def desc_file(desc_file)
  @description = IO.read("#{api_version.configuration.descriptions_path}/#{desc_file}.md", encoding: 'utf-8')
end

#error!(*codes) ⇒ Object



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

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

#generate_full_pathObject



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

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



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

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

#name(name) ⇒ Object



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

def name(name)
  @summary = name
end

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



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

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

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



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

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

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



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

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



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

def with_body?
  !!body_param
end

#with_errors?Boolean



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

def with_errors?
  !errors.empty?
end

#with_path_param?Boolean



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

def with_path_param?
  !path_params.empty?
end