Module: SurveyGizmo::Resource

Extended by:
ActiveSupport::Concern
Included in:
API::Contact, API::EmailMessage, API::Option, API::Page, API::Question, API::Response, API::Survey, API::SurveyCampaign
Defined in:
lib/survey_gizmo/resource.rb

Defined Under Namespace

Modules: ClassMethods Classes: Response

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.descendantsSet

Returns Every class that includes SurveyGizmo::Resource.

Returns:

  • (Set)

    Every class that includes SurveyGizmo::Resource



16
17
18
# File 'lib/survey_gizmo/resource.rb', line 16

def self.descendants
  @descendants ||= Set.new
end

Instance Method Details

#destroyBoolean

Deleted the Resource from Survey Gizmo

Returns:

  • (Boolean)


187
188
189
190
191
192
# File 'lib/survey_gizmo/resource.rb', line 187

def destroy
  return false if new? || destroyed?
  handle_response SurveyGizmo.delete(handle_route(:delete)) do
    _response.ok? ? destroyed! : false
  end
end

#errorsArray

Any errors returned by Survey Gizmo

Returns:

  • (Array)


229
230
231
# File 'lib/survey_gizmo/resource.rb', line 229

def errors
  @errors ||= []
end

#new?Boolean

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.

The state of the current Resource

Returns:

  • (Boolean)


196
197
198
# File 'lib/survey_gizmo/resource.rb', line 196

def new?
  @_state.nil?
end

#raw_responseHash

Returns The raw JSON returned by Survey Gizmo.

Returns:

  • (Hash)

    The raw JSON returned by Survey Gizmo



234
235
236
# File 'lib/survey_gizmo/resource.rb', line 234

def raw_response
  _response.response if _response
end

#reloadself, false

fetch resource from SurveyGizmo and reload the attributes

Returns:

  • (self, false)

    Returns the object, if saved. Otherwise returns false.



174
175
176
177
178
179
180
181
182
183
# File 'lib/survey_gizmo/resource.rb', line 174

def reload
  handle_response SurveyGizmo.get(handle_route(:get)) do
    if _response.ok?
      self.attributes = _response.data
      clean!
    else
      false
    end
  end
end

#saveBoolean

Save the instance to Survey Gizmo

Returns:

  • (Boolean)

    true if Resource instance is saved



161
162
163
164
165
166
167
168
169
# File 'lib/survey_gizmo/resource.rb', line 161

def save
  if new?
    _create
  else
    handle_response SurveyGizmo.post(handle_route(:update), :query => self.attributes_without_blanks) do
      _response.ok? ? saved! : false
    end
  end
end

#to_param_optionsHash

Sets the hash that will be used to interpolate values in routes. It needs to be defined per model.

Returns:

  • (Hash)

    a hash of the values needed in routing



223
224
225
# File 'lib/survey_gizmo/resource.rb', line 223

def to_param_options
  raise "Define #to_param_options in #{self.class.name}"
end

#update(attributes = {}) ⇒ Boolean

Updates attributes and saves this Resource instance

Parameters:

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

    attributes to be updated

Returns:

  • (Boolean)

    true if resource is saved



152
153
154
155
# File 'lib/survey_gizmo/resource.rb', line 152

def update(attributes = {})
  self.attributes = attributes
  self.save
end