Class: PollEverywhere::Models::Poll

Inherits:
Object
  • Object
show all
Includes:
Serializable
Defined in:
lib/polleverywhere/models.rb

Overview

Poll is an abstract base class for multiple choice and free text polls

Direct Known Subclasses

FreeTextPoll, MultipleChoicePoll

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Serializable

included

Constructor Details

#initialize(http = PollEverywhere.http) ⇒ Poll

Returns a new instance of Poll.


121
122
123
# File 'lib/polleverywhere/models.rb', line 121

def initialize(http=PollEverywhere.http)
  self.http = http
end

Instance Attribute Details

#httpObject

Returns the value of attribute http


119
120
121
# File 'lib/polleverywhere/models.rb', line 119

def http
  @http
end

Class Method Details

.allObject


157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/polleverywhere/models.rb', line 157

def self.all
  PollEverywhere.http.get.from("/my/polls").as(:json).response do |response|
    ::JSON.parse(response.body).map do |hash|
      case hash.keys.first.to_sym
      when MCP.root_key
        MCP.from_hash(hash)
      when FTP.root_key
        MCP.from_hash(hash)
      end
    end
  end
end

.get(permalink) ⇒ Object


153
154
155
# File 'lib/polleverywhere/models.rb', line 153

def self.get(permalink)
  from_hash(:permalink => permalink).fetch
end

Instance Method Details

#archiveObject


177
178
179
180
181
182
183
184
185
186
187
# File 'lib/polleverywhere/models.rb', line 177

def archive
  if persisted?
    http.delete.from(path + "/results/archive").response do |response|
      return true
    end

    return false
  else
    false
  end
end

#clearObject


189
190
191
192
193
194
195
196
197
198
199
# File 'lib/polleverywhere/models.rb', line 189

def clear
  if persisted?
    http.delete.from(path + "/results").response do |response|
      return true
    end

    return false
  else
    false
  end
end

#destroyObject


201
202
203
204
205
# File 'lib/polleverywhere/models.rb', line 201

def destroy
  http.delete.from(path).response do |response|
    self.id = self.permalink = nil
  end
end

#fetchObject


170
171
172
173
174
# File 'lib/polleverywhere/models.rb', line 170

def fetch
  http.get.from(path).as(:json).response do |response|
    from_json response.body
  end
end

#pathObject


219
220
221
# File 'lib/polleverywhere/models.rb', line 219

def path
  "/#{self.class.root_key}s/#{permalink}" if persisted?
end

#persisted?Boolean

Returns:

  • (Boolean)

137
138
139
# File 'lib/polleverywhere/models.rb', line 137

def persisted?
  !!permalink
end

#possible_statesObject


223
224
225
# File 'lib/polleverywhere/models.rb', line 223

def possible_states
  [:opened, :closed, :maxed_out]
end

#resultsObject


207
208
209
210
211
212
213
214
215
216
217
# File 'lib/polleverywhere/models.rb', line 207

def results
  if persisted?
    http.get.to(path + '/results').response do |response|
      return response.body
    end

    return false
  else
    false
  end
end

#saveObject


141
142
143
144
145
146
147
148
149
150
151
# File 'lib/polleverywhere/models.rb', line 141

def save
  if persisted?
    http.put(to_json).as(:json).to(path).response do |response|
      from_json response.body
    end
  else
    http.post(to_json).as(:json).to("/#{self.class.root_key}s").response do |response|
      from_json response.body
    end
  end
end

#startObject

Start the poll so that it may receive audience responses


126
127
128
129
# File 'lib/polleverywhere/models.rb', line 126

def start
  self.state = "opened"
  save
end

#stopObject

Stop the poll so that it stops receieving responses


132
133
134
135
# File 'lib/polleverywhere/models.rb', line 132

def stop
  self.state = "closed"
  save
end