Module: Swagger::Helper

Included in:
ControllerSetup, NestedControllerSetup
Defined in:
app/domain/swagger/helper.rb

Instance Method Summary collapse

Instance Method Details

#controller_routeObject



24
25
26
# File 'app/domain/swagger/helper.rb', line 24

def controller_route
  controller_class.model_class.new(id: 1)
end

#description(controller = controller_class) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/domain/swagger/helper.rb', line 40

def description(controller = controller_class)
  # relationships = controller.model_class
  #   .reflect_on_all_associations(:belongs_to)
  #   .map(&:name).sort
  #
  # relationships += controller.model_class
  # .reflect_on_all_associations(:has_many)
  # .map(&:name).sort

  relationships = controller.model_class
    .reflect_on_all_associations
    .map(&:name).sort

  'The following relationships are available: ' \
    "#{relationships.join(', ')} (separate values with a comma)"
end

#human_nameObject



16
17
18
# File 'app/domain/swagger/helper.rb', line 16

def human_name
  model_name.human
end

#model_nameObject



12
13
14
# File 'app/domain/swagger/helper.rb', line 12

def model_name
  controller_class.model_class.model_name
end

#nested_controller_idObject



32
33
34
# File 'app/domain/swagger/helper.rb', line 32

def nested_controller_id
  controller_class.model_class.model_name.route_key.singularize + '_id'
end

#nested_human_nameObject



20
21
22
# File 'app/domain/swagger/helper.rb', line 20

def nested_human_name
  nested_model_name.human
end

#nested_model_nameObject



36
37
38
# File 'app/domain/swagger/helper.rb', line 36

def nested_model_name
  nested_class.model_class.model_name
end

#nested_root_pathObject



28
29
30
# File 'app/domain/swagger/helper.rb', line 28

def nested_root_path
  nested_model_name.route_key
end

#parameter_id(swagger_doc, helper) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'app/domain/swagger/helper.rb', line 91

def parameter_id(swagger_doc, helper)
  swagger_doc.parameter do
    key :name, :id
    key :in, :path
    key :description, "ID of #{helper.human_name} to fetch"
    key :required, true
    key :type, :integer
  end
end

#parameter_include(swagger_doc, helper, type) ⇒ Object

rubocop:disable Metrics/MethodLength



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/domain/swagger/helper.rb', line 101

def parameter_include(swagger_doc, helper, type) # rubocop:disable Metrics/MethodLength
  desc = case type.to_sym
         when :index, :show then description
         when :nested       then description helper.nested_class
         end
  swagger_doc.parameter do
    key :name,        :include
    key :in,          :query
    key :description, desc
    key :required,    false
    key :type,        :string
  end
end

#parameters(swagger_doc, helper, type) ⇒ Object



83
84
85
86
87
88
89
# File 'app/domain/swagger/helper.rb', line 83

def parameters(swagger_doc, helper, type)
  case type.to_sym
  when :show, :nested then parameter_id swagger_doc, helper
  end

  parameter_include swagger_doc, helper, type
end

#path_spec(swagger_doc, helper, type) ⇒ Object

rubocop:disable Metrics/MethodLength



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/domain/swagger/helper.rb', line 57

def path_spec(swagger_doc, helper, type) # rubocop:disable Metrics/MethodLength
  summary =
    case type.to_sym
    when :index  then "All #{human_name.pluralize}"
    when :show   then "Single #{human_name}"
    when :nested then "All #{nested_human_name.pluralize} belonging to #{human_name}"
    end

  swagger_doc.operation :get do
    key :summary, summary
    helper.setup_tags(self)
    helper.parameters(self, helper, type)
    response 200 do
      key :description, summary + ' Response'
      helper.response_schema(self, helper, type)
    end
  end
end

#response_schema(swagger_doc, helper, type) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/domain/swagger/helper.rb', line 115

def response_schema(swagger_doc, helper, type)
  ref = case type.to_sym
        when :index, :show then helper.model_name
        when :nested       then helper.nested_model_name
        end

  swagger_doc.schema do
    key :type, :array
    items do
      key :'$ref', ref
    end
  end
end

#setup_swagger_path(path, helper = self, &block) ⇒ Object



4
5
6
7
8
9
10
# File 'app/domain/swagger/helper.rb', line 4

def setup_swagger_path(path, helper = self, &block)
  return unless path
  @path = path.gsub('1', '{id}')
  controller_class.send(:swagger_path, @path) do
    instance_exec(helper, &block)
  end
end

#setup_tags(swagger_doc) ⇒ Object



76
77
78
79
80
81
# File 'app/domain/swagger/helper.rb', line 76

def setup_tags(swagger_doc)
  swagger_doc.key :tags, [
    'All',
    Swagger::TagsSetup.path_tag(@path)
  ]
end