Class: Praxis::RestfulDocGenerator::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/praxis/restful_doc_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition) ⇒ Resource

Returns a new instance of Resource.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/praxis/restful_doc_generator.rb', line 65

def initialize( definition )
  @controller_config = definition
  if controller_config.version == 'n/a'
    @version = 'unversioned'
  else
    @version = controller_config.version
  end
  @media_type = controller_config.media_type
  @reachable_types = Set.new

  # Collect reachable types from the media_type if any (plus itself)
  if @media_type && ! @media_type.is_a?(Praxis::SimpleMediaType)
    add_to_reachable RestfulDocGenerator.inspect_attributes(@media_type)
    @generated_example = @media_type.example(self.id)
  end

  # Collect reachable types from the params and payload definitions
  @controller_config.actions.each do |name, action_config|
    # skip actions with doc_visibility of :none
    next if action_config.[:doc_visibility] == :none

    add_to_reachable RestfulDocGenerator.inspect_attributes(action_config.params)
    add_to_reachable RestfulDocGenerator.inspect_attributes(action_config.payload)

    action_config.responses.values.each do |response|
      add_to_reachable RestfulDocGenerator.inspect_attributes(response.media_type)
    end
  end

end

Instance Attribute Details

#controller_configObject

Returns the value of attribute controller_config.



63
64
65
# File 'lib/praxis/restful_doc_generator.rb', line 63

def controller_config
  @controller_config
end

#media_typeObject

Returns the value of attribute media_type.



63
64
65
# File 'lib/praxis/restful_doc_generator.rb', line 63

def media_type
  @media_type
end

#reachable_typesObject

Returns the value of attribute reachable_types.



63
64
65
# File 'lib/praxis/restful_doc_generator.rb', line 63

def reachable_types
  @reachable_types
end

#versionObject

Returns the value of attribute version.



63
64
65
# File 'lib/praxis/restful_doc_generator.rb', line 63

def version
  @version
end

Instance Method Details

#add_to_reachable(found) ⇒ Object



130
131
132
133
# File 'lib/praxis/restful_doc_generator.rb', line 130

def add_to_reachable( found )
  return if found == nil
  @reachable_types += found
end

#display_nameObject



135
136
137
# File 'lib/praxis/restful_doc_generator.rb', line 135

def display_name
  @controller_config.display_name
end

#friendly_nameObject



119
120
121
122
123
124
125
126
127
128
# File 'lib/praxis/restful_doc_generator.rb', line 119

def friendly_name
  # FIXME: is it really about the controller? or the attached resource definition?
  segments = self.name.split("::")
  # FIXME: Crappy hack to derive a friendly name
  if ["Collection","Links"].include? segments.last
    segments[-2] + segments[-1] # concat the last 2
  else
    segments.last
  end
end

#idObject

TODO: I think that the “id”/“name” of a resource should be provided by the definition/controller…not derived here



97
98
99
100
101
102
103
104
# File 'lib/praxis/restful_doc_generator.rb', line 97

def id
  if @controller_config.controller
    @controller_config.controller.id
  else
    # If an API doesn't quite have the controller defined, let's use the name from the resource definition
    @controller_config.id
  end
end

#nameObject



106
107
108
109
110
111
112
113
# File 'lib/praxis/restful_doc_generator.rb', line 106

def name
  if @controller_config.controller
    @controller_config.controller.name
  else
    # If an API doesn't quite have the controller defined, let's use the name from the resource definition
    @controller_config.name
  end
end

#parentObject



115
116
117
# File 'lib/praxis/restful_doc_generator.rb', line 115

def parent
  @controller_config.parent
end