martlet 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in martlet.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Alex Coco
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # Martlet
2
+
3
+ Martlet is a Ruby client for McGill's student portal, Minerva. Minerva sucks and constantly checking your grades sucks even more.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'martlet'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install martlet
18
+
19
+ ## Usage
20
+
21
+ $ irb
22
+ >> require 'martlet'
23
+ >> client = Martlet.new('your.name@mail.mcgill.ca', 'topsecret')
24
+ >> pp client.grades
25
+ {"COMP 206"=>"A",
26
+ "COMP 250"=>"A"
27
+ ...
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,48 @@
1
+ module Martlet
2
+ class AuthenticationError < StandardError; end
3
+
4
+ class Authenticator
5
+ def initialize(agent)
6
+ @agent = agent
7
+ end
8
+
9
+ def authenticate(email, password)
10
+ # Go to login page
11
+ login_page = @agent.get(login_url)
12
+
13
+ # Find and fill login form
14
+ form = login_form(login_page)
15
+ form['sid'] = email
16
+ form['PIN'] = password
17
+
18
+ submit_login_form(form)
19
+ end
20
+
21
+ private
22
+
23
+ def login_url
24
+ 'https://horizon.mcgill.ca/pban1/twbkwbis.P_WWWLogin'
25
+ end
26
+
27
+ def login_form(page)
28
+ if form = page.form('loginform1')
29
+ form
30
+ else
31
+ raise AuthenticationError.new('Login form not found')
32
+ end
33
+ end
34
+
35
+ def submit_login_form(form)
36
+ page = form.submit
37
+ body = page.body
38
+
39
+ unless body.match(/Welcome,\+(.*),\+to\+Minerva/)
40
+ if body.include?('Authorization Failure')
41
+ raise AuthenticationError.new('Invalid email or password')
42
+ else
43
+ raise AuthenticationError.new('Authentication failed')
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,20 @@
1
+ require 'mechanize'
2
+
3
+ module Martlet
4
+ class Client
5
+ attr_reader :email
6
+
7
+ def initialize(email, password)
8
+ @agent = Mechanize.new
9
+ @email = email
10
+
11
+ authenticator = Authenticator.new(@agent)
12
+ authenticator.authenticate(email, password)
13
+ end
14
+
15
+ def grades
16
+ transcript = Transcript.new(@agent)
17
+ transcript.fetch_grades
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,39 @@
1
+ module Martlet
2
+ class Record
3
+ attr_accessor :number, :name, :section, :credits, :grade, :average
4
+
5
+ def initialize(args)
6
+ @number = args[:number]
7
+ @name = args[:name]
8
+ @section = args[:section]
9
+ @credits = args[:credits].to_i
10
+ @grade = args[:grade]
11
+ @average = args[:average]
12
+ end
13
+
14
+ def gpa
15
+ grade_to_gpa(grade)
16
+ end
17
+
18
+ def average_gpa
19
+ grade_to_gpa(average)
20
+ end
21
+
22
+ private
23
+
24
+ def grade_to_gpa(grade)
25
+ case grade
26
+ when 'A' then 4.0
27
+ when 'A-' then 3.7
28
+ when 'B+' then 3.3
29
+ when 'B' then 3.0
30
+ when 'B-' then 2.7
31
+ when 'C+' then 2.3
32
+ when 'C' then 2.0
33
+ when 'D' then 1.0
34
+ when 'F' then 0.0
35
+ else nil
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,32 @@
1
+ module Martlet
2
+ class Transcript
3
+ def initialize(agent)
4
+ @agent = agent
5
+ @html = fetch_transcript_html
6
+ end
7
+
8
+ def fetch_grades
9
+ records = fetch_records
10
+ records.inject({}) do |hash, record|
11
+ hash[record.number] = record.grade
12
+ hash
13
+ end
14
+ end
15
+
16
+ def fetch_records
17
+ parser = TranscriptParser.new(@html)
18
+ parser.parse_records
19
+ end
20
+
21
+ private
22
+
23
+ def transcript_url
24
+ 'https://horizon.mcgill.ca/pban1/bzsktran.P_Display_Form?user_type=S&tran_type=V'
25
+ end
26
+
27
+ def fetch_transcript_html
28
+ page = @agent.get(transcript_url)
29
+ page.body
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,68 @@
1
+ module Martlet
2
+ class TranscriptParser
3
+ def initialize(html)
4
+ @html = html
5
+ end
6
+
7
+ def parse_records
8
+ records = []
9
+ document = Nokogiri::HTML(@html)
10
+ rows = document.search("table[@class='dataentrytable'] tr")
11
+
12
+ rows.each do |row|
13
+ row_data = row.search('td')
14
+ next unless record_data_row?(row_data) && grade_present?(row_data)
15
+
16
+ record_data = {
17
+ number: extract_record_data_for(row_data, :number),
18
+ name: extract_record_data_for(row_data, :name),
19
+ section: extract_record_data_for(row_data, :section),
20
+ credits: extract_record_data_for(row_data, :credits),
21
+ grade: extract_record_data_for(row_data, :grade),
22
+ average: extract_record_data_for(row_data, :average)
23
+ }
24
+
25
+ records << Record.new(record_data)
26
+
27
+ end
28
+
29
+ records
30
+ end
31
+
32
+ private
33
+
34
+ def index_for(name)
35
+ case name
36
+ when :number then 1
37
+ when :section then 2
38
+ when :name then 3
39
+ when :credits then 4
40
+ when :grade then 6
41
+ when :average then 10
42
+ end
43
+ end
44
+
45
+ def extract_record_data_for(data, name)
46
+ index = index_for(name)
47
+
48
+ if data && data[index]
49
+ span = data[index].search('span')
50
+
51
+ if span
52
+ span.text
53
+ else
54
+ ''
55
+ end
56
+ end
57
+ end
58
+
59
+ def record_data_row?(row_data)
60
+ row_data.length == 11
61
+ end
62
+
63
+ def grade_present?(row_data)
64
+ grade = extract_record_data_for(row_data, :grade)
65
+ grade && !grade.empty?
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,3 @@
1
+ module Martlet
2
+ VERSION = "0.0.2"
3
+ end
data/lib/martlet.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'martlet/client'
2
+ require 'martlet/authenticator'
3
+ require 'martlet/transcript'
4
+ require 'martlet/transcript_parser'
5
+ require 'martlet/record'
6
+ require 'martlet/version'
7
+
8
+ module Martlet
9
+ def self.new(*args)
10
+ Client.new(*args)
11
+ end
12
+ end
data/martlet.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'martlet/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "martlet"
8
+ spec.version = Martlet::VERSION
9
+ spec.authors = ["Alex Coco"]
10
+ spec.email = ["hello@alexcoco.com"]
11
+ spec.description = %q{Ruby client for McGill's student portal, Minerva.}
12
+ spec.summary = %q{Client for McGill's student portal}
13
+ spec.homepage = "https://github.com/alexcoco/martlet"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_dependency "mechanize", "~> 2.7"
25
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: martlet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alex Coco
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-12-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: mechanize
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '2.7'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.7'
62
+ description: Ruby client for McGill's student portal, Minerva.
63
+ email:
64
+ - hello@alexcoco.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - lib/martlet.rb
75
+ - lib/martlet/authenticator.rb
76
+ - lib/martlet/client.rb
77
+ - lib/martlet/record.rb
78
+ - lib/martlet/transcript.rb
79
+ - lib/martlet/transcript_parser.rb
80
+ - lib/martlet/version.rb
81
+ - martlet.gemspec
82
+ homepage: https://github.com/alexcoco/martlet
83
+ licenses:
84
+ - MIT
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 1.8.23
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: Client for McGill's student portal
107
+ test_files: []