Class: StrawPoll
- Inherits:
-
Object
- Object
- StrawPoll
- 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
-
#api ⇒ Object
readonly
These attributes are internally use to track state and provide some quickyl accesible features from online polls.
-
#id ⇒ Object
readonly
These attributes are internally use to track state and provide some quickyl accesible features from online polls.
-
#leader ⇒ Object
readonly
These attributes are internally use to track state and provide some quickyl accesible features from online polls.
-
#link ⇒ Object
readonly
These attributes are internally use to track state and provide some quickyl accesible features from online polls.
-
#multi ⇒ Object
These attributes can be changed in the same manner that can be done on the strawpoll.me website.
-
#options ⇒ Object
These attributes can be changed in the same manner that can be done on the strawpoll.me website.
-
#perm ⇒ Object
These attributes can be changed in the same manner that can be done on the strawpoll.me website.
-
#results ⇒ Object
readonly
These attributes are internally use to track state and provide some quickyl accesible features from online polls.
-
#title ⇒ Object
These attributes can be changed in the same manner that can be done on the strawpoll.me website.
-
#votes ⇒ Object
readonly
These attributes are internally use to track state and provide some quickyl accesible features from online polls.
Instance Method Summary collapse
-
#create! ⇒ Object
Publishes poll online and setups links for distribution.
-
#initialize(title, *options) ⇒ StrawPoll
constructor
Creates a new poll instance.
-
#view(id = @id) ⇒ Object
Retrieves generic poll info.
-
#winner(id = @id) ⇒ Object
Retrieves options with most votes.
Constructor Details
#initialize(title, *options) ⇒ StrawPoll
Creates a new poll instance
Returns StrawPoll Object
An ArgumentError is raised if passed less than two options
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/strawpoll_api.rb', line 31 def initialize title, * raise ArgumentError unless .length >= 2 @title = title @multi = false @perm = true @options = @api = 'http://strawpoll.me/api/v2/polls/' @id = nil @leader = nil @votes = nil @link = nil @results = nil end |
Instance Attribute Details
#api ⇒ Object (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 |
#id ⇒ Object (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 |
#leader ⇒ Object (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 |
#link ⇒ Object (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 link @link end |
#multi ⇒ Object
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 |
#options ⇒ Object
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 end |
#perm ⇒ Object
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 |
#results ⇒ Object (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 |
#title ⇒ Object
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 |
#votes ⇒ Object (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
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
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
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 |