Module: SurveyGizmo::Resource

Extended by:
ActiveSupport::Concern
Included in:
API::AccountTeams, 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

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



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

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

Instance Method Details

#create_record_in_surveygizmo(attributes = {}) ⇒ Object

Returns itself if successfully saved, but with attributes added by SurveyGizmo



145
146
147
148
149
# File 'lib/survey_gizmo/resource.rb', line 145

def create_record_in_surveygizmo(attributes = {})
  rest_response = RestResponse.new(SurveyGizmo.put(handle_route(:create), query: self.attributes_without_blanks))
  self.attributes = rest_response.data
  self
end

#destroyObject

Delete the Resource from Survey Gizmo



133
134
135
136
# File 'lib/survey_gizmo/resource.rb', line 133

def destroy
  fail "No id; can't delete #{self.inspect}!" unless id
  RestResponse.new(SurveyGizmo.delete(handle_route(:delete)))
end

#inspectObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/survey_gizmo/resource.rb', line 151

def inspect
  if ENV['GIZMO_DEBUG']
    ap "CLASS: #{self.class}"
  end

  attribute_strings = self.class.attribute_set.map do |attrib|
    if ENV['GIZMO_DEBUG']
      ap attrib
      ap attrib.name
      ap self.send(attrib.name)
      ap self.send(attrib.name).class
    end

    if self.send(attrib.name).class == Hash
      value = self.send(attrib.name).inspect
    else
      value = self.send(attrib.name).to_s
    end

    "  \"#{attrib.name}\" => \"#{value}\"\n" unless value.strip.blank?
  end.compact

  "#<#{self.class.name}:#{self.object_id}>\n#{attribute_strings.join()}"
end

#reloadObject

Repopulate the attributes based on what is on SurveyGizmo’s servers



127
128
129
130
# File 'lib/survey_gizmo/resource.rb', line 127

def reload
  self.attributes = RestResponse.new(SurveyGizmo.get(handle_route(:get))).data
  self
end

#saveObject

Save the resource to SurveyGizmo



117
118
119
120
121
122
123
124
# File 'lib/survey_gizmo/resource.rb', line 117

def save
  if id
    # Then it's an update, because we already know the surveygizmo assigned id
    RestResponse.new(SurveyGizmo.post(handle_route(:update), query: self.attributes_without_blanks))
  else
    create_record_in_surveygizmo
  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



140
141
142
# File 'lib/survey_gizmo/resource.rb', line 140

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