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 = {}, filters = nil) ⇒ SurveyGizmo::Collection, Array
Get a list of resources.
-
#collection(resource_name, model = nil) ⇒ Collection
Defines a new collection.
- #collections ⇒ Object private
-
#convert_filters_into_query_string(filters = nil) ⇒ String
Convert a [Hash] of filters into a query string.
-
#copy(attributes = {}) ⇒ Resource
Copy a resource.
-
#create(attributes = {}) ⇒ Resource
Create a new resource.
-
#destroy(conditions) ⇒ Boolean
Deleted the Resource from Survey Gizmo.
-
#first(conditions = {}, filters = nil) ⇒ 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
95 96 97 98 99 100 |
# File 'lib/survey_gizmo/resource.rb', line 95 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 = {}, filters = nil) ⇒ SurveyGizmo::Collection, Array
Get a list of resources
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/survey_gizmo/resource.rb', line 38 def all(conditions = {}, filters = nil) response = Response.new SurveyGizmo.get(handle_route(:create, conditions) + convert_filters_into_query_string(filters)) 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.
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/survey_gizmo/resource.rb', line 116 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("def \#{resource_name}\n@\#{resource_name} ||= []\nend\n\ndef \#{resource_name}=(array)\n@\#{resource_name} = SurveyGizmo::Collection.new(\#{self}, :\#{resource_name}, array)\nend\n") 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.
130 131 132 |
# File 'lib/survey_gizmo/resource.rb', line 130 def collections @collections.dup.freeze end |
#convert_filters_into_query_string(filters = nil) ⇒ String
Convert a [Hash] of filters into a query string
27 28 29 30 31 32 |
# File 'lib/survey_gizmo/resource.rb', line 27 def convert_filters_into_query_string(filters = nil) "" unless filters && filters.size > 0 uri = Addressable::URI.new uri.query_values = filters "?#{uri.query}" end |
#copy(attributes = {}) ⇒ Resource
Copy a resource
73 74 75 76 77 78 |
# File 'lib/survey_gizmo/resource.rb', line 73 def copy(attributes = {}) attributes[:copy] = true resource = new(attributes) resource.__send__(:_copy) resource end |
#create(attributes = {}) ⇒ Resource
Create a new resource
62 63 64 65 66 |
# File 'lib/survey_gizmo/resource.rb', line 62 def create(attributes = {}) resource = new(attributes) resource.__send__(:_create) resource end |
#destroy(conditions) ⇒ Boolean
Deleted the Resource from Survey Gizmo
83 84 85 86 |
# File 'lib/survey_gizmo/resource.rb', line 83 def destroy(conditions) response = Response.new SurveyGizmo.delete(handle_route(:delete, conditions)) response.ok? end |
#first(conditions = {}, filters = nil) ⇒ Object?
Get the first resource
53 54 55 56 |
# File 'lib/survey_gizmo/resource.rb', line 53 def first(conditions = {}, filters = nil) response = Response.new SurveyGizmo.get(handle_route(:get, conditions) + convert_filters_into_query_string(filters)) 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.
135 136 137 138 139 140 141 142 |
# File 'lib/survey_gizmo/resource.rb', line 135 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.
103 104 105 106 107 |
# File 'lib/survey_gizmo/resource.rb', line 103 def load(attributes = {}) resource = new(attributes) resource.__send__(:clean!) resource end |