Class: StrawPoll

Inherits:
Object
  • Object
show all
Defined in:
lib/strawpoll_api.rb

Overview

This class stores a poll instance that can interact with www.strawpoll.me Creates a poll object which you can customize and then publish online.

An ArgumentError is raised if you have less than two options in your poll

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, *options) ⇒ StrawPoll

Creates a new poll instance

Returns StrawPoll Object

An ArgumentError is raised if passed less than two options

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/strawpoll_api.rb', line 31

def initialize title, *options
	raise ArgumentError unless options.length >= 2
	@title = title
	@multi = false
	@perm = true
	@options = options
	@api = 'http://strawpoll.me/api/v2/polls/'
	@id = nil
	@leader = nil
	@votes = nil
	@link = nil
	@results = nil
end

Instance Attribute Details

#apiObject (readonly)

These attributes are internally use to track state and provide some quickyl accesible features from online polls



22
23
24
# File 'lib/strawpoll_api.rb', line 22

def api
  @api
end

#idObject (readonly)

These attributes are internally use to track state and provide some quickyl accesible features from online polls



22
23
24
# File 'lib/strawpoll_api.rb', line 22

def id
  @id
end

#leaderObject (readonly)

These attributes are internally use to track state and provide some quickyl accesible features from online polls



22
23
24
# File 'lib/strawpoll_api.rb', line 22

def leader
  @leader
end

These attributes are internally use to track state and provide some quickyl accesible features from online polls



22
23
24
# File 'lib/strawpoll_api.rb', line 22

def link
  @link
end

#multiObject

These attributes can be changed in the same manner that can be done on the strawpoll.me website



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

def multi
  @multi
end

#optionsObject

These attributes can be changed in the same manner that can be done on the strawpoll.me website



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

def options
  @options
end

#permObject

These attributes can be changed in the same manner that can be done on the strawpoll.me website



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

def perm
  @perm
end

#resultsObject (readonly)

These attributes are internally use to track state and provide some quickyl accesible features from online polls



22
23
24
# File 'lib/strawpoll_api.rb', line 22

def results
  @results
end

#titleObject

These attributes can be changed in the same manner that can be done on the strawpoll.me website



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

def title
  @title
end

#votesObject (readonly)

These attributes are internally use to track state and provide some quickyl accesible features from online polls



22
23
24
# File 'lib/strawpoll_api.rb', line 22

def votes
  @votes
end

Instance Method Details

#create!Object

Publishes poll online and setups links for distribution

Returns raw HTTP body

An ArgumentError is raised if title is remove or options are reduced below 2

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/strawpoll_api.rb', line 53

def create!
	raise ArgumentError unless !@title.empty? && @options.length >= 2
	params = {
		"title" => @title,
		"options" => @options,
		"multi" => @multi,
		"permissive" => @perm,
	}
	url = URI @api
	resp = Net::HTTP.post_form(url, params)
	@id = JSON.parse(resp.body)['id']
	@link = "http://strawpoll.me/#{@id}" 
	@results = "http://strawpoll.me/#{@id}/r"
	resp.body
end

#view(id = @id) ⇒ Object

Retrieves generic poll info. Defaults to poll instance but can be pointed at any poll by using it’s ID number

Returns raw HTTP body

An ArgumentError is raised if ID is not valid type

Raises:

  • (ArgumentError)


77
78
79
80
81
# File 'lib/strawpoll_api.rb', line 77

def view id=@id
	raise ArgumentError unless id.is_a? Fixnum
	url = URI "#{@api}#{id}"
	resp = Net::HTTP.get(url)
end

#winner(id = @id) ⇒ Object

Retrieves options with most votes. Defaults to poll instance but can be pointed at any poll by using it’s ID number

Returns String of winning option, setups up internal states for instance

An ArgumentError is raised if ID is not valid type

Raises:

  • (ArgumentError)


91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/strawpoll_api.rb', line 91

def winner id=@id
	raise ArgumentError unless id.is_a? Fixnum
	url = URI "#{@api}#{id}"
	resp = Net::HTTP.get(url)
	result = JSON.parse(resp)['votes']
	winner = JSON.parse(resp)['options'][result.index(result.max)]
	if id == @id
		@leader = winner
		@votes = result
	end
	winner
end