class Stormpath::Test::ResourceFactory

Constants

URL_PREFIX

Public Class Methods

new() click to toggle source
  # File spec/support/resource_factory.rb
6 def initialize
7   @id_count = 0
8 end

Public Instance Methods

collection(parent, type, depth = 1, associations = []) click to toggle source
   # File spec/support/resource_factory.rb
29 def collection(parent, type, depth = 1, associations = [])
30   id = id_for parent
31   collection = {
32     'href' => "#{URL_PREFIX}/#{parent}s/#{id}/#{type}s",
33     'items' => [
34       resource(type, depth, associations),
35       resource(type, depth, associations)
36     ]
37   }
38 
39   collection
40 end
id_for(type) click to toggle source
   # File spec/support/resource_factory.rb
42 def id_for(type)
43   @id_count += 1
44   "#{type[0, 3]}#{@id_count}"
45 end
resource(type, depth = 1, associations = []) click to toggle source
   # File spec/support/resource_factory.rb
10 def resource(type, depth = 1, associations = [])
11   id = id_for type
12   plural = "#{type}s"
13   resource = { 'href' => "#{URL_PREFIX}/#{plural}/#{id}" }
14 
15   if depth > 0
16     resource['name'] = "#{type} #{id}"
17     associations.each do |assoc|
18       resource[assoc] = if assoc =~ /s$/
19                           collection type, assoc.sub(/s$/, ''), depth - 1
20                         else
21                           resource assoc, depth - 1
22                         end
23     end
24   end
25 
26   resource
27 end