uqam_grade_notification 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 97e755342551caf2762a98a11889f81b56f3addd
4
+ data.tar.gz: e028c7df8d5b381306942ea2334de26a3a5e2fb9
5
+ SHA512:
6
+ metadata.gz: 0509ebd51bcdded0330be624a91166928d9e3eab496f6246afcb3317e42ed1fad49cd330610fbef5254bdef0ca71ed43aefae41a427f41b153c83cfb41b69606
7
+ data.tar.gz: 5dd65951a86789cc07a76279bfc6538e3d109ae6d94ef3670c8db364309d6d31d0087a1c78390e4a8d4b4dabd782a5a56a4939646c3d650852e8b38e0d282f90
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in uqam_grade_notification.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Alexandre Croteau-Pothier
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,20 @@
1
+ # Uqam Grade Notification
2
+
3
+ This utility will send you an sms alert when your grades have been updated.
4
+
5
+ ## Installation
6
+
7
+ Install it with:
8
+
9
+ $ gem install uqam_grade_notification
10
+
11
+ ## Usage
12
+
13
+ The first time you execute you will be prompt to enter these informations:
14
+
15
+ * Code Permanent
16
+ * NIP
17
+ * Phone Number
18
+ * Twilio Account ID
19
+ * Twilio Auth Token
20
+ * Twilio Phone Number
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "uqam_grade_notification"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'uqam_grade_notification'
4
+
5
+ UqamGradeNotification.perform
@@ -0,0 +1,71 @@
1
+ require 'fileutils'
2
+ require 'json'
3
+ require 'highline'
4
+ require 'digest'
5
+ require 'uqam_grade_notification/version'
6
+ require 'uqam_grade_notification/student'
7
+ require 'uqam_grade_notification/notifier'
8
+
9
+ module UqamGradeNotification
10
+ APP_DIR = "#{ENV["HOME"]}/.uqam_grade_notification"
11
+ USER_FILE = "#{APP_DIR}/user.json"
12
+ GRADES_FILE = "#{APP_DIR}/grades.json"
13
+
14
+ def self.perform
15
+ current_grades = student.grades.to_json
16
+ if grades_are_equals?(current_grades)
17
+ p "Grades have not changed since last checked."
18
+ else
19
+ message = "Your grades have been updated!"
20
+ p message
21
+ File.write(GRADES_FILE, current_grades)
22
+ Notifier.new(user_info).sms(message)
23
+ end
24
+ end
25
+
26
+ def self.student
27
+ UqamGradeNotification::Student.new(user_info)
28
+ end
29
+
30
+ def self.grades_are_equals?(grades)
31
+ md5 = Digest::MD5.new
32
+ md5.hexdigest(grades) == md5.hexdigest(last_grades)
33
+ end
34
+
35
+ def self.last_grades
36
+ @last_grades ||=
37
+ if File.exist?(GRADES_FILE)
38
+ File.open(GRADES_FILE, "rb").read
39
+ else
40
+ ""
41
+ end
42
+ end
43
+
44
+ def self.user_info
45
+ @user_info ||=
46
+ if File.exist?(USER_FILE)
47
+ JSON.parse(File.open(USER_FILE).read)
48
+ else
49
+ setup_user_data
50
+ end
51
+ end
52
+
53
+ def self.setup_user_data
54
+ unless Dir.exists?(APP_DIR)
55
+ p "Running first time setup."
56
+ Dir.mkdir(APP_DIR)
57
+ end
58
+
59
+ cli = HighLine.new
60
+ code = cli.ask("Code Permanent")
61
+ password = cli.ask("NIP")
62
+ phone = cli.ask("Phone Number")
63
+ twilio_account_id = cli.ask("Twilio Account ID")
64
+ twilio_token = cli.ask("Twilio Token")
65
+ twilio_phone = cli.ask("Twilio Phone Number")
66
+
67
+ info = {"code" => code, "password" => password, "phone" => phone, "twilio_account_id" => twilio_account_id, "twilio_token" => twilio_token, "twilio_phone" => twilio_phone}
68
+ File.write(USER_FILE, info.to_json)
69
+ info
70
+ end
71
+ end
@@ -0,0 +1,24 @@
1
+ require 'twilio-ruby'
2
+
3
+ module UqamGradeNotification
4
+ class Notifier
5
+ attr_reader :phone
6
+
7
+ def initialize user_info
8
+ @phone = user_info.fetch("phone")
9
+ @twilio_account_id = user_info.fetch("twilio_account_id")
10
+ @twilio_token = user_info.fetch("twilio_token")
11
+ @twilio_phone = user_info.fetch("twilio_phone")
12
+ end
13
+
14
+ def sms(message)
15
+ client = Twilio::REST::Client.new @twilio_account_id, @twilio_token
16
+
17
+ client.account.messages.create({
18
+ from: @twilio_phone,
19
+ to: @phone,
20
+ body: message
21
+ })
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,75 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+ require 'faraday-cookie_jar'
4
+
5
+ module UqamGradeNotification
6
+ class Student
7
+ attr_reader :code, :password
8
+
9
+ def initialize user_info
10
+ @code = user_info.fetch("code")
11
+ @password = user_info.fetch("password")
12
+ end
13
+
14
+ def grades
15
+ @grades ||= semesters[0..1].map do |semester|
16
+ courses = semester["programmes"][0]["activites"]
17
+ courses.map do |course|
18
+ get_results(course)
19
+ end
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def semesters
26
+ @semesters ||= connection.get do |req|
27
+ req.url "/apis/resumeResultat/identifiant"
28
+ req.headers['Authorization'] = "Bearer #{token}"
29
+ end.body["data"]["resultats"]
30
+ end
31
+
32
+ def connection
33
+ Faraday.new(url: "https://portailetudiant.uqam.ca") do |c|
34
+ c.request :json
35
+ c.response :json, content_type: /\bjson$/
36
+ c.use :cookie_jar
37
+ c.adapter :net_http
38
+ end
39
+ end
40
+
41
+ def get_results course
42
+ data = connection.get do |req|
43
+ req.url "/apis/resultatActivite/identifiant/#{course['trimestre']}/#{course['sigle']}/#{course['groupe']}"
44
+ req.headers['Authorization'] = "Bearer #{token}"
45
+ end.body["data"]["resultats"][0]["programmes"][0]["activites"][0]
46
+
47
+ {
48
+ "name" => data["titreActivite"],
49
+ "note" => data["note"],
50
+ "evaluations" => data["evaluations"]
51
+ }
52
+ end
53
+
54
+ def informations
55
+ @informations ||= connection.get do |req|
56
+ req.url "/apis/resume/identifiant"
57
+ req.headers['Authorization'] = "Bearer #{token}"
58
+ end.body["data"]["resume"]
59
+ end
60
+
61
+ def token
62
+ @token ||= login.body["token"]
63
+ end
64
+
65
+ def login
66
+ connection.post do |req|
67
+ req.url "/authentification"
68
+ req.headers['Content-Type'] = 'application/json;charset=UTF-8'
69
+ req.headers['Origin'] = 'https://portailetudiant.uqam.ca'
70
+ req.headers['Referer'] = 'https://portailetudiant.uqam.ca'
71
+ req.body = {identifiant: code, motDePasse: password}.to_json
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,3 @@
1
+ module UqamGradeNotification
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'uqam_grade_notification/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "uqam_grade_notification"
8
+ spec.version = UqamGradeNotification::VERSION
9
+ spec.authors = ["Alexandre Croteau-Pothier"]
10
+ spec.email = ["alex@alexcp.com"]
11
+ spec.homepage = "https://github.com/alexcp/Uqam-Grade-Notification"
12
+
13
+ spec.summary = %q{Sends notification when notes are added.}
14
+ spec.license = "MIT"
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = ["uqam_grade_notification"]
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.9"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_dependency "nokogiri"
23
+ spec.add_dependency "faraday"
24
+ spec.add_dependency "faraday_middleware"
25
+ spec.add_dependency "faraday-cookie_jar"
26
+ spec.add_dependency "highline"
27
+ spec.add_dependency "twilio-ruby"
28
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uqam_grade_notification
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexandre Croteau-Pothier
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-01-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: nokogiri
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday_middleware
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: faraday-cookie_jar
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: highline
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: twilio-ruby
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email:
127
+ - alex@alexcp.com
128
+ executables:
129
+ - uqam_grade_notification
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".travis.yml"
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - bin/console
141
+ - bin/setup
142
+ - exe/uqam_grade_notification
143
+ - lib/uqam_grade_notification.rb
144
+ - lib/uqam_grade_notification/notifier.rb
145
+ - lib/uqam_grade_notification/student.rb
146
+ - lib/uqam_grade_notification/version.rb
147
+ - uqam_grade_notification.gemspec
148
+ homepage: https://github.com/alexcp/Uqam-Grade-Notification
149
+ licenses:
150
+ - MIT
151
+ metadata: {}
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ requirements: []
167
+ rubyforge_project:
168
+ rubygems_version: 2.4.7
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: Sends notification when notes are added.
172
+ test_files: []