Class: SwaggerYard::ApiDeclaration

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApiDeclaration

Returns a new instance of ApiDeclaration.



10
11
12
13
14
# File 'lib/swagger_yard/api_declaration.rb', line 10

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

Instance Attribute Details

#apisObject (readonly)

Returns the value of attribute apis.



4
5
6
# File 'lib/swagger_yard/api_declaration.rb', line 4

def apis
  @apis
end

#authorizationsObject (readonly)

Returns the value of attribute authorizations.



4
5
6
# File 'lib/swagger_yard/api_declaration.rb', line 4

def authorizations
  @authorizations
end

#class_nameObject (readonly)

Returns the value of attribute class_name.



4
5
6
# File 'lib/swagger_yard/api_declaration.rb', line 4

def class_name
  @class_name
end

#descriptionObject

Returns the value of attribute description.



3
4
5
# File 'lib/swagger_yard/api_declaration.rb', line 3

def description
  @description
end

#resourceObject

Returns the value of attribute resource.



3
4
5
# File 'lib/swagger_yard/api_declaration.rb', line 3

def resource
  @resource
end

Class Method Details

.from_yard_object(yard_object) ⇒ Object



6
7
8
# File 'lib/swagger_yard/api_declaration.rb', line 6

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

Instance Method Details

#add_api(yard_object) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/swagger_yard/api_declaration.rb', line 54

def add_api(yard_object)
  path = Api.path_from_yard_object(yard_object)

  return if path.nil?

  api = (apis[path] ||= Api.from_yard_object(yard_object, self))
  api.add_operation(yard_object)
end

#add_info(yard_object) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/swagger_yard/api_declaration.rb', line 35

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

  if tag = yard_object.tags.detect {|t| t.tag_name == "resource_path"}
    log.warn "DEPRECATED: @resource_path tag is obsolete."
  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_yard_object(yard_object) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/swagger_yard/api_declaration.rb', line 20

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_api(yard_object)
  end
  self
end

#apis_hashObject



63
64
65
# File 'lib/swagger_yard/api_declaration.rb', line 63

def apis_hash
  Hash[apis.map {|path, api| [path, api.operations_hash]}]
end

#to_tagObject



67
68
69
70
# File 'lib/swagger_yard/api_declaration.rb', line 67

def to_tag
  { "name"        => resource,
    "description" => description }
end

#valid?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/swagger_yard/api_declaration.rb', line 16

def valid?
  !@resource.nil?
end