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
-
.route(path, options) ⇒ Object
Define the path where a resource is located.
Instance Method Summary collapse
-
#all(conditions = {}) ⇒ SurveyGizmo::Collection, Array
Get a list of resources.
-
#collection(resource_name, model = nil) ⇒ Collection
Defines a new collection.
- #collections ⇒ Object private
-
#create(attributes = {}) ⇒ Resource
Create a new resource.
-
#destroy(conditions) ⇒ Boolean
Deleted the Resource from Survey Gizmo.
-
#first(conditions) ⇒ Object?
Get the first resource.
- #handle_route(key, *interp) ⇒ Object private
- #load(attributes = {}) ⇒ Object private
Class Method Details
.route(path, options) ⇒ Object
Define the path where a resource is located
70 71 72 73 74 75 |
# File 'lib/survey_gizmo/resource.rb', line 70 def route(path, ) methods = [: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
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.
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 |
#collections ⇒ 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.
105 106 107 |
# File 'lib/survey_gizmo/resource.rb', line 105 def collections @collections.dup.freeze end |
#create(attributes = {}) ⇒ Resource
Create a new resource
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
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
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 = 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| [$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 |