Class: GSR

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

Overview

GSR class handles making GSR reservations

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, spike_root_url = "http://spike.wharton.upenn.edu/m/gsr.cfm?logout=true") ⇒ GSR

Returns a new instance of GSR.



49
50
51
52
53
# File 'lib/gsr.rb', line 49

def initialize(username, password, spike_root_url = "http://spike.wharton.upenn.edu/m/gsr.cfm?logout=true")
  @username = username
  @password = password
  @spike_root_url = spike_root_url
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



47
48
49
# File 'lib/gsr.rb', line 47

def password
  @password
end

#spike_root_urlObject

Returns the value of attribute spike_root_url.



47
48
49
# File 'lib/gsr.rb', line 47

def spike_root_url
  @spike_root_url
end

#usernameObject

Returns the value of attribute username.



47
48
49
# File 'lib/gsr.rb', line 47

def username
  @username
end

Instance Method Details

#cancelObject

Cancels most recent GSR reservation



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/gsr.rb', line 112

def cancel()
  agent = ()['agent'] # Mechanize agent at successful login page

  gsr = ()['gsr'] # Mechanize page = successful login page

  
  cancel = gsr.link_with(:text => 'Cancel')
  if (cancel.nil?)
    raise "Error: You have no GSR reservation to cancel."
  else
    gsr = cancel.click
  end
end

#reserve(requirements) ⇒ Object

floor (string [F, G, 2, 3]), start_time (Time), duration (integer, minutes [30,60, or 90])



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/gsr.rb', line 69

def reserve(requirements)   
  #Check if we have a non-kosher floor and revert to default

  requirements['floor'] = "" if !(requirements['floor'].nil?) and 
    !(["", "F", "G", "2", "3"].include? requirements['floor'])
  
  #Default args

  default_options = {'floor' => "", 'start_time' => Time.now, 'duration' => 90}
  args = default_options.merge(requirements)
  
  #Convert start_date and start_time from date and time to correct form format

  start_time = args['start_time'].format_start_time
  start_date = args['start_time'].format_start_date
  
  #Duration logic - round to 30, 60, or 90 (nearest)

  unless requirements['duration'].nil?
    if args['duration'] <= 30
      args['duration'] = 30
    elsif args['duration'] <= 60
      args['duration'] = 60
    else
      args['duration'] = 90
    end
  end
  
  agent = ()['agent'] # Mechanize agent at successful login page

  gsr = ()['gsr'] # Mechanize page = successful login page

  gsrform = gsr.form_with(:action => 'https://spike.wharton.upenn.edu/m/gsr.cfm') #Select GSR form


  #Input GSR info

  gsrform.preferred_floor = args['floor']
  gsrform.start_date = start_date
  gsrform.start_time = start_time
  gsrform.duration = args['duration']
  
  #Submit reservation

  submit = agent.submit(gsrform, gsrform.buttons.first)
  
  #Check if successful

  raise "Error reserving GSR. Check supplied parameters." if submit.link_with(:text => 'Cancel').nil?
  
end

#spike_loginObject

Logs in to spike using mechanize



56
57
58
59
60
61
62
63
64
# File 'lib/gsr.rb', line 56

def ()
  agent = Mechanize.new
   = agent.get(self.spike_root_url) #Go to login page

  loginform = agent.page.forms.first #Select login form

  loginform.username = self.username
  loginform.password = self.password
  gsr = agent.submit(loginform, loginform.buttons.first) #Submit form and log in

  return {'agent' => agent, 'gsr' => gsr}
end