Module: SurveyGizmo::Resource::ClassMethods

Defined in:
lib/survey_gizmo/resource.rb

Overview

These are methods that every API resource has to access resources in Survey Gizmo

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.route(path, options) ⇒ Object

Define the path where a resource is located

Parameters:

  • path (String)

    the path in Survey Gizmo for the resource

  • options (Hash)

Options Hash (options):

  • :via (Array)

    which is ‘:get`, `:create`, `:update`, `:delete`, or `:any`



70
71
72
73
74
75
# File 'lib/survey_gizmo/resource.rb', line 70

def route(path, options)
  methods = options[:via]
  methods = [:get, :create, :update, :delete] if methods == :any
  methods.is_a?(Array) ? methods.each{|m| @paths[m] = path } : (@paths[methods] = path)
  nil
end

Instance Method Details

#all(conditions = {}) ⇒ SurveyGizmo::Collection, Array

Get a list of resources

Parameters:

  • conditions (Hash) (defaults to: {})

Returns:



26
27
28
29
30
31
32
33
34
35
# File 'lib/survey_gizmo/resource.rb', line 26

def all(conditions = {})
  response = Response.new SurveyGizmo.get(handle_route(:create, conditions))
  if response.ok?
    _collection = SurveyGizmo::Collection.new(self, nil, response.data)
    _collection.send(:options=, {:target => self, :parent => self})
    _collection
  else
    []
  end
end

#collection(resource_name, model = nil) ⇒ Collection

Defines a new collection. These are child objects of the resource.

Parameters:

  • resource_name (Symbol)

    the name of the collection, pluralized

  • model (Class) (defaults to: nil)

    and optional class name if the class name does not match the resource_name

Returns:



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/survey_gizmo/resource.rb', line 91

def collection(resource_name, model = nil)
  @collections[resource_name] = {:parent => self, :target => (model ? model : resource_name)} # workaround for weird bug with passing a class to Collection
  class_eval(<<-EOS)
    def #{resource_name}
      @#{resource_name} ||= []
    end
    
    def #{resource_name}=(array)
      @#{resource_name} = SurveyGizmo::Collection.new(#{self}, :#{resource_name}, array)
    end
  EOS
end

#collectionsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



105
106
107
# File 'lib/survey_gizmo/resource.rb', line 105

def collections
  @collections.dup.freeze
end

#create(attributes = {}) ⇒ Resource

Create a new resource

Parameters:

  • attributes (Hash) (defaults to: {})

Returns:

  • (Resource)

    The newly created Resource instance



49
50
51
52
53
# File 'lib/survey_gizmo/resource.rb', line 49

def create(attributes = {})
  resource = new(attributes)
  resource.__send__(:_create)
  resource
end

#destroy(conditions) ⇒ Boolean

Deleted the Resource from Survey Gizmo

Parameters:

  • conditions (Hash)

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/survey_gizmo/resource.rb', line 58

def destroy(conditions)
  response = Response.new SurveyGizmo.delete(handle_route(:delete, conditions))
  response.ok?
end

#first(conditions) ⇒ Object?

Get the first resource

Parameters:

  • conditions (Hash)

Returns:

  • (Object, nil)


40
41
42
43
# File 'lib/survey_gizmo/resource.rb', line 40

def first(conditions)
  response = Response.new SurveyGizmo.get(handle_route(:get, conditions))
  response.ok? ? load(conditions.merge(response.data)) : nil
end

#handle_route(key, *interp) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



110
111
112
113
114
115
116
117
# File 'lib/survey_gizmo/resource.rb', line 110

def handle_route(key, *interp)
  path = @paths[key]
  raise "No routes defined for `#{key}` in #{self.name}" unless path
  options = interp.last.is_a?(Hash) ? interp.pop : path.scan(/:(\w+)/).inject({}){|hash, k| hash.merge(k.to_sym => interp.shift) }
  path.gsub(/:(\w+)/) do |m| 
    options[$1.to_sym].tap{ |result| raise(SurveyGizmo::URLError, "Missing parameters in request: `#{m}`") unless result }
  end
end

#load(attributes = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



78
79
80
81
82
# File 'lib/survey_gizmo/resource.rb', line 78

def load(attributes = {})
  resource = new(attributes)
  resource.__send__(:clean!)
  resource
end