Class: SwaggerYard::ApiGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/swagger_yard/api_group.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApiGroup

Returns a new instance of ApiGroup.



33
34
35
36
37
# File 'lib/swagger_yard/api_group.rb', line 33

def initialize
  @resource       = nil
  @path_items     = {}
  @authorizations = {}
end

Instance Attribute Details

#authorizationsObject (readonly)

Returns the value of attribute authorizations.



27
28
29
# File 'lib/swagger_yard/api_group.rb', line 27

def authorizations
  @authorizations
end

#class_nameObject (readonly)

Returns the value of attribute class_name.



27
28
29
# File 'lib/swagger_yard/api_group.rb', line 27

def class_name
  @class_name
end

#descriptionObject

Returns the value of attribute description.



26
27
28
# File 'lib/swagger_yard/api_group.rb', line 26

def description
  @description
end

#path_itemsObject (readonly)

Returns the value of attribute path_items.



27
28
29
# File 'lib/swagger_yard/api_group.rb', line 27

def path_items
  @path_items
end

#resourceObject

Returns the value of attribute resource.



26
27
28
# File 'lib/swagger_yard/api_group.rb', line 26

def resource
  @resource
end

Class Method Details

.from_yard_object(yard_object) ⇒ Object



29
30
31
# File 'lib/swagger_yard/api_group.rb', line 29

def self.from_yard_object(yard_object)
  new.add_yard_object(yard_object)
end

Instance Method Details

#add_info(yard_object) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/swagger_yard/api_group.rb', line 66

def add_info(yard_object)
  @description = yard_object.docstring
  @class_name  = yard_object.path

  if tag = yard_object.tags.detect {|t| t.tag_name == "resource"}
    @resource = tag.text
  end

  # we only have api_key auth, the value for now is always empty array
  @authorizations = Hash[yard_object.tags.
                         select {|t| t.tag_name == "authorize_with"}.
                         map(&:text).uniq.
                         map {|k| [k, []]}]
end

#add_path_item(yard_object) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/swagger_yard/api_group.rb', line 81

def add_path_item(yard_object)
  path = path_from_yard_object(yard_object)

  return if path.nil?

  path_item = (path_items[path] ||= PathItem.new(self))
  path_item.add_operation(yard_object)
  path
end

#add_yard_object(yard_object) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/swagger_yard/api_group.rb', line 51

def add_yard_object(yard_object)
  case yard_object.type
  when :class # controller
    add_info(yard_object)
    if valid?
      yard_object.children.each do |child_object|
        add_yard_object(child_object)
      end
    end
  when :method # actions
    add_path_item(yard_object)
  end
  self
end

#path_from_yard_object(yard_object) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/swagger_yard/api_group.rb', line 91

def path_from_yard_object(yard_object)
  if tag = yard_object.tags.detect {|t| t.tag_name == "path"}
    tag.text
  elsif fn = SwaggerYard.config.path_discovery_function
    begin
      method, path = fn[yard_object]
      yard_object.add_tag YARD::Tags::Tag.new("path", path, [method]) if path
      path
    rescue => e
      SwaggerYard.log.warn e.message
      nil
    end
  end
end

#pathsObject



43
44
45
# File 'lib/swagger_yard/api_group.rb', line 43

def paths
  Paths.new(path_items)
end

#tagObject



47
48
49
# File 'lib/swagger_yard/api_group.rb', line 47

def tag
  @tag ||= Tag.new(resource, description)
end

#valid?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/swagger_yard/api_group.rb', line 39

def valid?
  !@resource.nil?
end