Class: ShiftNote

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(username: nil, password: nil) ⇒ ShiftNote

Initialize a new ShiftNote variable, via login credentials.

Parameters:

  • username (String) (defaults to: nil)

    the username of the user.

  • password (String) (defaults to: nil)

    the password of the user.


14
15
16
17
# File 'lib/shiftnote.rb', line 14

def initialize(username: nil, password: nil)
  @credentials = { username: username, password: password }
  generate_cookie(username, password)
end

Instance Method Details

This is basically an API token.

Returns:

  • (String)

    the cookie of the authenticated user


67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/shiftnote.rb', line 67

def cookie
  shiftnote = RestClient.get('https://ww1.shiftnote.com/BulletinBoard/', Cookie: @cookie)

  doc = Nokogiri::HTML.parse(shiftnote.body)

  begin
    doc.search('div#MyScheduleDiv').at('script').text
  rescue NoMethodError
    generate_cookie(@credentials[:username], @credentials[:password])
  end
  @cookie
end

#employee(id) ⇒ Object

Get an employee (by id)

Parameters:

  • id (Integer)

    the id of the employee


101
102
103
# File 'lib/shiftnote.rb', line 101

def employee(id)
  Employee.new(id: id)
end

Generate a cookie, based on login credentials.

Parameters:

  • username (String)

    the username of the user.

  • password (String)

    the password of the user.

Returns:

  • (String)

    the cookie!

Raises:


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 generate_cookie(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'
  )

  req_options = {
    use_ssl: uri.scheme == 'https'
  }

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) 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!")

  @cookie = response.header['Set-Cookie']
end

#get_next_week_shifts(day = "Monday") ⇒ ScheduleThisWeek

Returns the schedule for next week.

Parameters:

  • day (String) (defaults to: "Monday")

    what day the week starts on.

Returns:


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: 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)

Parameters:

  • recipients (Array<Integer>)

    who will receive this message


119
120
121
122
123
124
125
126
127
# File 'lib/shiftnote.rb', line 119

def send_message(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: @cookie)
end

#userUser

Get the information of the currently logged in user.

Returns:

  • (User)

    the 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: @cookie)

  doc = Nokogiri::HTML.parse(shiftnote.body)

  begin
    data = doc.search('div#MyScheduleDiv').at('script').text
  rescue NoMethodError
    generate_cookie(@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