Class: GrapeApiary::Resource
- Inherits:
-
Object
- Object
- GrapeApiary::Resource
- Defined in:
- lib/grape-apiary/resource.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
-
#sample_generator ⇒ Object
readonly
Returns the value of attribute sample_generator.
Instance Method Summary collapse
- #header ⇒ Object
-
#initialize(key, routes) ⇒ Resource
constructor
A new instance of Resource.
- #namespaced ⇒ Object
- #paths ⇒ Object
- #resource_binding ⇒ Object
- #sample_request ⇒ Object
- #sample_response(route) ⇒ Object
- #title ⇒ Object
- #unique_params ⇒ Object
Constructor Details
#initialize(key, routes) ⇒ Resource
Returns a new instance of Resource.
5 6 7 8 9 10 |
# File 'lib/grape-apiary/resource.rb', line 5 def initialize(key, routes) @key = key @name = key.humanize @routes = routes @sample_generator = SampleGenerator.new(self) end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
3 4 5 |
# File 'lib/grape-apiary/resource.rb', line 3 def key @key end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/grape-apiary/resource.rb', line 3 def name @name end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
3 4 5 |
# File 'lib/grape-apiary/resource.rb', line 3 def routes @routes end |
#sample_generator ⇒ Object (readonly)
Returns the value of attribute sample_generator.
3 4 5 |
# File 'lib/grape-apiary/resource.rb', line 3 def sample_generator @sample_generator end |
Instance Method Details
#header ⇒ Object
28 29 30 31 32 33 |
# File 'lib/grape-apiary/resource.rb', line 28 def header # TODO: ??? route = routes.first "#{title} #{route.route_type} [#{route.path_without_format}]" end |
#namespaced ⇒ Object
16 17 18 19 20 |
# File 'lib/grape-apiary/resource.rb', line 16 def namespaced @namespaced ||= routes.group_by(&:namespace).map do |_, routes| Resource.new(name, routes) end end |
#paths ⇒ Object
22 23 24 25 26 |
# File 'lib/grape-apiary/resource.rb', line 22 def paths @paths ||= routes.group_by(&:path_without_format).map do |_, routes| Resource.new(name, routes) end end |
#resource_binding ⇒ Object
59 60 61 |
# File 'lib/grape-apiary/resource.rb', line 59 def resource_binding binding end |
#sample_request ⇒ Object
35 36 37 |
# File 'lib/grape-apiary/resource.rb', line 35 def sample_request sample_generator.request end |
#sample_response(route) ⇒ Object
39 40 41 |
# File 'lib/grape-apiary/resource.rb', line 39 def sample_response(route) sample_generator.response(route.list?) end |
#title ⇒ Object
12 13 14 |
# File 'lib/grape-apiary/resource.rb', line 12 def title @title ||= name.titleize end |
#unique_params ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/grape-apiary/resource.rb', line 43 def unique_params # TODO: this is a hack, assuming that the resource has a POST or PUT # route that defines all of the parameters that would define the resource methods = %w(POST PUT) potential = routes.select do |route| methods.include?(route.request_method) && route.params.present? end if potential.present? potential.first.params else [] end end |