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`



95
96
97
98
99
100
# File 'lib/survey_gizmo/resource.rb', line 95

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 = {}, filters = nil) ⇒ SurveyGizmo::Collection, Array

Get a list of resources

Parameters:

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

Returns:



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.

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:



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

#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.



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

Parameters:

  • filters (Hash) (defaults to: nil)

Returns:

  • (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

Parameters:

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

Returns:

  • (Resource)

    The newly created resource instance



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

Parameters:

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

Returns:

  • (Resource)

    The newly created Resource instance



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

Parameters:

  • conditions (Hash)

Returns:

  • (Boolean)


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

Parameters:

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

Returns:

  • (Object, nil)


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
  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.



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

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