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
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.



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

def api_version
  @api_version
end

#base_pathObject (readonly)

Returns the value of attribute base_path.



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

def base_path
  @base_path
end

#body_paramObject (readonly)

Returns the value of attribute body_param.



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

def body_param
  @body_param
end

#desc_file_nameObject (readonly)

Returns the value of attribute desc_file_name.



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

def desc_file_name
  @desc_file_name
end

#desc_file_pathObject

Returns the value of attribute desc_file_path.



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

def desc_file_path
  @desc_file_path
end

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#extra_pathObject (readonly)

Returns the value of attribute extra_path.



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

def extra_path
  @extra_path
end

#full_pathObject (readonly)

Returns the value of attribute full_path.



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

def full_path
  @full_path
end

#header_paramsObject (readonly)

Returns the value of attribute header_params.



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

def header_params
  @header_params
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#path_paramsObject (readonly)

Returns the value of attribute path_params.



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

def path_params
  @path_params
end

#query_paramsObject (readonly)

Returns the value of attribute query_params.



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

def query_params
  @query_params
end

#respObject (readonly)

Returns the value of attribute resp.



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

def resp
  @resp
end

#summaryObject (readonly)

Returns the value of attribute summary.



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

def summary
  @summary
end

Instance Method Details

#body(body_param) ⇒ Object



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

def body(body_param)
  @body_param = body_param
end

#build(neighbors) ⇒ Object



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
123
124
125
126
127
128
129
130
# File 'lib/api_schema/resource_definition.rb', line 92

def build(neighbors)
  error_model = :error_model
  error_desc = {
    '401' => "Unauthorized",
    '403' => "Forbidden",
    '404' => "Not found",
    '422' => "Unprocessable Entity"
 }

  build_description if desc_file_name
  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_descriptionObject



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

def build_description
  @description = IO.read(desc_file_path, encoding: 'utf-8')
end

#build_neighbors(neighbors) ⇒ Object



86
87
88
89
90
# File 'lib/api_schema/resource_definition.rb', line 86

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

#desc(desc) ⇒ Object



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

def desc(desc)
  @description = desc
end

#desc_file(desc_file_name) ⇒ Object



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

def desc_file(desc_file_name)
  @desc_file_name =  desc_file_name
end

#error!(*codes) ⇒ Object



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

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

#generate_full_pathObject



77
78
79
80
# File 'lib/api_schema/resource_definition.rb', line 77

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



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

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

#name(name) ⇒ Object



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

def name(name)
  @summary = name
end

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



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

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

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



50
51
52
# File 'lib/api_schema/resource_definition.rb', line 50

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

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



54
55
56
57
58
59
# File 'lib/api_schema/resource_definition.rb', line 54

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)


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

def with_body?
  !!body_param
end

#with_errors?Boolean

Returns:

  • (Boolean)


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

def with_errors?
  !errors.empty?
end

#with_path_param?Boolean

Returns:

  • (Boolean)


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

def with_path_param?
  !path_params.empty?
end