Module: DoodleClient

Defined in:
lib/doodle_client.rb,
lib/doodle_client/version.rb

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.list(url) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/doodle_client.rb', line 35

def DoodleClient.list(url)
  #Initialise empty options array. Could use a hash?
  options = Array.new

  # Get an id to work with.
  id = urltoid(url)

  agent = Mechanize.new
  agent.user_agent = "Windows Mozilla"

  # Use id to build doodle link we can parse on.
  kissURL = "http://doodle.com/kiss/#{id}?participantKey=&locale=en_US&timeZone=Europe%2FLondon"

  page = agent.get(kissURL)
  # Only gets checkboxes atm, should support radio buttons as well.
  page.forms[0].checkboxes.each{ |checkbox| options << checkbox  }

  return options
end

.urltoid(url) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/doodle_client.rb', line 9

def DoodleClient.urltoid(url)
  # If url doesn't have a / in it, assume it's already an id and return it.
  return url if !url.match("/")
  # If URL has http then id is 5th column, otherwise assuming 3rd.
  url.match("http") ? (id = url.split('/')[4]) : (id = url.split('/')[2])
  return id
end

.verify(url) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/doodle_client.rb', line 17

def DoodleClient.verify(url)
  # Get an id to work with.
  id = urltoid(url)

  # Normal url for status code checking, invalid id on kissURL still returns
  # 200 for some reason.
  doodleURL = "http://doodle.com/poll/#{id}"

  # Use id to build doodle link we can parse on.
  kissURL = "http://doodle.com/kiss/#{id}?participantKey=&locale=en_US&timeZone=Europe%2FLondon"

  # Check we've got a valid doodle poll.
  status = Net::HTTP.get_response(URI("#{doodleURL}")).code
  return false if status != "200"
  return true

end

.vote(url, name, options) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/doodle_client.rb', line 55

def DoodleClient.vote(url, name, options)
  # Get an id to work with.
  id = urltoid(url)

  agent = Mechanize.new
  agent.user_agent = "Windows Mozilla"

  # Use id to build doodle link we can parse on.
  kissURL = "http://doodle.com/kiss/#{id}?participantKey=&locale=en_US&timeZone=Europe%2FLondon"

  page = agent.get(kissURL)
  # Use provided name to vote.
  page.forms[0]["name"] = "#{name}"

  # Iterate over checkboxes and apply options array.
    page.forms[0].checkboxes.map{ |checkbox| checkbox.check if options.first == true; options.delete_at(0) }

  # Submit it.
  page.forms[0].submit
  return true
end