Class: ShiftNote
- Inherits:
-
Object
- Object
- ShiftNote
- Defined in:
- lib/shiftnote.rb
Overview
All ShiftNote functionality, whether extended or just here.
Defined Under Namespace
Modules: Errors Classes: DaysOfWeekShift, DaysOfWeekShifts, Employee, ScheduleThisWeek, Shift, User
Instance Method Summary collapse
-
#cookie ⇒ String
This is basically an API token.
-
#employee(id) ⇒ Object
Get an employee (by id).
-
#generate_cookie(username, password) ⇒ String
Generate a cookie, based on login credentials.
-
#get_next_week_shifts(day = "Monday") ⇒ ScheduleThisWeek
The schedule for next week.
-
#initialize(username: nil, password: nil) ⇒ ShiftNote
constructor
Initialize a new ShiftNote variable, via login credentials.
-
#send_message(recipients, subject, body, urgent) ⇒ Object
Send a message to a user (or group of them).
-
#user ⇒ User
Get the information of the currently logged in user.
Constructor Details
#initialize(username: nil, password: nil) ⇒ ShiftNote
Initialize a new ShiftNote variable, via login credentials.
14 15 16 17 |
# File 'lib/shiftnote.rb', line 14 def initialize(username: nil, password: nil) @credentials = { username: username, password: password } (username, password) end |
Instance Method Details
#cookie ⇒ String
This is basically an API token.
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/shiftnote.rb', line 67 def shiftnote = RestClient.get('https://ww1.shiftnote.com/BulletinBoard/', Cookie: ) doc = Nokogiri::HTML.parse(shiftnote.body) begin doc.search('div#MyScheduleDiv').at('script').text rescue NoMethodError (@credentials[:username], @credentials[:password]) end end |
#employee(id) ⇒ Object
Get an employee (by id)
101 102 103 |
# File 'lib/shiftnote.rb', line 101 def employee(id) Employee.new(id: id) end |
#generate_cookie(username, password) ⇒ String
Generate a cookie, based on login credentials.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/shiftnote.rb', line 23 def (username, password) uri = URI.parse('https://ww1.shiftnote.com/login') request = Net::HTTP::Post.new(uri) request.content_type = 'application/x-www-form-urlencoded' request.set_form_data( 'FullSite' => 'False', 'ReturnUrl' => '', 'username' => username, 'Password' => password, 'Submit' => 'Submit', 'remember' => 'true' ) = { use_ssl: uri.scheme == 'https' } response = Net::HTTP.start(uri.hostname, uri.port, ) do |http| http.request(request) end # response.code raise Errors::InvalidCredentials, 'The provided credentials are invalid, please try again.' if response.body.include?("There was no match for the username and password provided, please try again!") = response.header['Set-Cookie'] end |
#get_next_week_shifts(day = "Monday") ⇒ ScheduleThisWeek
Returns the schedule for next week.
107 108 109 110 111 112 113 114 115 |
# File 'lib/shiftnote.rb', line 107 def get_next_week_shifts(day = "Monday") nextmonday = date_of_next(day) week2 = RestClient.get("https://ww1.shiftnote.com/Schedules/ScheduleMine/?startDate=#{nextmonday.month}%2F#{nextmonday.day}%2F#{nextmonday.year}&noContainer=true&_=#{Time.now.to_i * 1000}", Cookie: ) week2doc = Nokogiri::HTML.parse(week2.body) dataweek2 = JSON.parse(scrapejunk(week2doc.at('script').text)) User.new(dataweek2).schedule_this_week end |
#send_message(recipients, subject, body, urgent) ⇒ Object
Send a message to a user (or group of them)
119 120 121 122 123 124 125 126 127 |
# File 'lib/shiftnote.rb', line 119 def (recipients, subject, body, urgent) data = { "ToUserIDs" => recipients, "Subject" => subject, "Body" => body, "Priority" => urgent } RestClient.post('https://ww1.shiftnote.com/Message/SendMessages', URI.encode_www_form(data), Cookie: ) end |
#user ⇒ User
Get the information of the currently logged in user.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/shiftnote.rb', line 82 def user shiftnote = RestClient.get('https://ww1.shiftnote.com/BulletinBoard/', Cookie: ) doc = Nokogiri::HTML.parse(shiftnote.body) begin data = doc.search('div#MyScheduleDiv').at('script').text rescue NoMethodError (@credentials[:username], @credentials[:password]) retry end data = data.gsub('<script>', '').delete(';').gsub('</script>', '').gsub('window.scheduleMinebindings = ShiftNote.Bind(window.scheduleMinemodel)', '').gsub('window.scheduleMinemodel = ', '') User.new(JSON.parse(data)) end |