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.



58
59
60
61
62
63
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
# File 'lib/praxis/restful_doc_generator.rb', line 58

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.



56
57
58
# File 'lib/praxis/restful_doc_generator.rb', line 56

def controller_config
  @controller_config
end

#media_typeObject

Returns the value of attribute media_type.



56
57
58
# File 'lib/praxis/restful_doc_generator.rb', line 56

def media_type
  @media_type
end

#reachable_typesObject

Returns the value of attribute reachable_types.



56
57
58
# File 'lib/praxis/restful_doc_generator.rb', line 56

def reachable_types
  @reachable_types
end

#versionObject

Returns the value of attribute version.



56
57
58
# File 'lib/praxis/restful_doc_generator.rb', line 56

def version
  @version
end

Instance Method Details

#add_to_reachable(found) ⇒ Object



122
123
124
125
# File 'lib/praxis/restful_doc_generator.rb', line 122

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

#friendly_nameObject



111
112
113
114
115
116
117
118
119
120
# File 'lib/praxis/restful_doc_generator.rb', line 111

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



93
94
95
96
97
98
99
100
# File 'lib/praxis/restful_doc_generator.rb', line 93

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



102
103
104
105
106
107
108
109
# File 'lib/praxis/restful_doc_generator.rb', line 102

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