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



64
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
95
96
# File 'lib/praxis/restful_doc_generator.rb', line 64

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)
    @media_type.attributes.each do |name, attr|
      add_to_reachable RestfulDocGenerator.inspect_attributes(attr)
    end
    @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.



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

def controller_config
  @controller_config
end

#media_typeObject

Returns the value of attribute media_type.



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

def media_type
  @media_type
end

#reachable_typesObject

Returns the value of attribute reachable_types.



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

def reachable_types
  @reachable_types
end

#versionObject

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

#add_to_reachable(found) ⇒ Object



132
133
134
135
# File 'lib/praxis/restful_doc_generator.rb', line 132

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

#friendly_nameObject



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

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



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

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



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

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



117
118
119
# File 'lib/praxis/restful_doc_generator.rb', line 117

def parent
  @controller_config.parent
end