Class: GrapeApiary::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/grape-apiary/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#keyObject (readonly)

Returns the value of attribute key.



3
4
5
# File 'lib/grape-apiary/resource.rb', line 3

def key
  @key
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/grape-apiary/resource.rb', line 3

def name
  @name
end

#routesObject (readonly)

Returns the value of attribute routes.



3
4
5
# File 'lib/grape-apiary/resource.rb', line 3

def routes
  @routes
end

#sample_generatorObject (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

#headerObject



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

#namespacedObject



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

#pathsObject



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_bindingObject



59
60
61
# File 'lib/grape-apiary/resource.rb', line 59

def resource_binding
  binding
end

#sample_requestObject



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

#titleObject



12
13
14
# File 'lib/grape-apiary/resource.rb', line 12

def title
  @title ||= name.titleize
end

#unique_paramsObject



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