susies 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0608b19bce844246bc61e260d3f8aa292b932947
4
- data.tar.gz: c7d13219a9691883ab1b91939e3a4b0f752bca40
3
+ metadata.gz: 231a7208b3e4000328f8260676aa1412a96372af
4
+ data.tar.gz: 24a9607e3371480fc777fc85c7ce936ce3d502cd
5
5
  SHA512:
6
- metadata.gz: e38b896a71269ee1eeb55400bdd3c4ea3236f7fef021267e06ec7ad64f77e9af21f638b5019db9b8f79d1df6062f7d3c61adc65ffafdbcf80b99a9f32803f7aa
7
- data.tar.gz: 9aef30c9f378ac421db0c86adaa6848e5f5c09b1b843b9b4401de252142a6888b84ccfe587a5293d8d1a2ed1e4724a338ecf9219acfdcfb36c133abb877354dd
6
+ metadata.gz: d6db7cadda7408b3aca1d33590ffacf2c3a3c4cc4f796bce18e4a636e8c205282094579abc4304f68709426feb5e4343347c20f9e714439d2ce600b694377c41
7
+ data.tar.gz: a373a9962f28f5c76cd9f897981bbf80c118fe7c11d19cc99dd7761b10a3a487b7181b421f7f4d869ea9a7a1e9e9d41a484be39df003089eacd619adeac679a6
data/README.md CHANGED
@@ -2,34 +2,182 @@
2
2
 
3
3
  ## Description
4
4
 
5
- Susies is a Ruby Gem intended for Epitech Students.
5
+ Susies est une gem destinée aux étudiants d'Epitech.
6
6
 
7
- It's a simple piece of code allowing students to easily find and register to a Susie Class.
7
+ Elle permet de trouver simplement une susie en fonction de quelques critères.
8
8
 
9
9
 
10
- ## Usage
10
+ ## Installation
11
+
12
+ Soit par ligne de commande: `gem install susies`
13
+
14
+ Soit via un `Gemfile`: `gem 'susies'` et `bundle install`
15
+
16
+
17
+ ## Utilisation
18
+
19
+ L'utilisation est assez simple:
20
+
21
+ ```ruby
22
+ require 'Susies'
23
+
24
+ # whiteListFilters: susies MUST match these filters
25
+ whiteListFilters = {
26
+ login: %w[login_x],
27
+ maxStudent: 6,
28
+ minHour: 8,
29
+ maxHour: 17
30
+ }
31
+
32
+ # blackListFilters: susies MUST NOT match these filters
33
+ blackListFilters = {
34
+ type: 'reading',
35
+ title: 'zola'
36
+ }
37
+
38
+ # autologin: intranet authentication
39
+ autologins = {
40
+ autologinPath: 'https://intra.epitech.eu/my_autologin_url',
41
+ buddiesAutologins: %w[/buddy_autologin_url],
42
+ }
43
+
44
+ # mailInfos: mailer configuration
45
+ mailInfos = {
46
+ uname: 'mail@gmail.com',
47
+ passwd: 'password',
48
+ targets: %w[mylogin_x@epitech.eu buddy_login@epitech.eu]
49
+ }
50
+
51
+ Susies.new( autologins, whiteListFilters, blackListFilters, mailInfos ).check!
52
+ ```
53
+
54
+ La gem se connectera alors à l'intra pour vérifier l'existence d'une susie correspondant à vos critères, durant une semaine où vous n'avez pas encore de susie.
55
+
56
+ Si elle trouve une susie, elle vous y inscrira, inscrira vos amis et préviendra tout le monde par mail.
57
+
58
+ ## Filters
59
+
60
+ A utiliser si vous souhaitez filtrer les susies qui vous intéressent.
61
+
62
+ Les whiteListFilters sont les critères que les susies doivent avoir, à l'inverse des blackListFilters.
63
+
64
+ Laissez `nil` si vous ne voullez pas de filtres.
65
+
66
+ ### login
67
+
68
+ * Les logins des susies recherchées.
69
+ * Type: Array of strings
70
+ * Default: nil
71
+
72
+
73
+ ### maxStudent
74
+
75
+ * Nombre d'étudiants déjà inscrit à la susie class (0: personne d'inscrit, 10: plus de places)
76
+ * Type: int
77
+ * Default: nil
78
+
79
+
80
+ ### minHour
81
+
82
+ * Heure minimum du début de la susie class.
83
+ * Type: Int
84
+ * Default: nil
85
+
86
+
87
+ ### maxHour
88
+
89
+ * Heure maximum du début de la susie class.
90
+ * Type: Int
91
+ * Default: nil
92
+
93
+
94
+ ### title
95
+
96
+ * KeyWord que le titre de la susie doit contenir.
97
+ * Type: String
98
+ * Default: nil
99
+
100
+
101
+ ### type
102
+
103
+ * Type de la susie
104
+ * Type: String
105
+ * Default: nil
106
+
107
+
108
+ ## MailInfos
109
+
110
+ A utiliser si vous souhaitez être alerté en cas d'inscription par le script. Fonctionne avec une adresse GMail.
111
+
112
+ Laissez `nil` si vous ne voullez pas envoyer de mails.
113
+
114
+ ### uname
115
+
116
+ * Nom d'utilisateur pour se connecter au serveur mail. cf `example/susies.rb` pour un exemple de configuration basé sur gmail.
117
+ * Type: String
118
+ * Default: nil
119
+
120
+
121
+ ### passwd
122
+
123
+ * Mot de passe pour se connecter au serveur mail. cf `example/susies.rb` pour un exemple de configuration basé sur gmail.
124
+ * Type: String
125
+ * Default: nil
126
+
127
+
128
+ ### targets
129
+
130
+ * Emails des personnes devant être alertés lorsqu'une susie a été trouvée.
131
+ * Type: Array of strings
132
+ * Default: nil
133
+
134
+
135
+ ## Autologins
136
+
137
+ ### autologinPath
138
+
139
+ * Votre autologin. Il s'agit du path et non de l'URL complète. Ce paramètre est OBLIGATOIRE!
140
+ * Type String
141
+ * Default: nil
142
+
143
+ ### buddiesAutologins
144
+
145
+ * Autologins des personnes devant être inscrites à la susie class (en plus de vous). Il s'agit du path et non de l'URL complète.
146
+ * Type: Array of strings
147
+ * Default: []
148
+
11
149
 
12
150
  ## Cron Task
13
151
 
14
- ## Enhancements
152
+ Une bonne utilisation de cette gem serait de l'utiliser via une tache cron qui lancera le script à intervalle régulier.
153
+
154
+ Par exemple, il est intéressant de faire une tache s'exécutant toutes les 5 minutes:
155
+
156
+ ```
157
+ */5 * * * * GEM_HOME=/path/to/gem/home /path/to/script.rb 1>> /path/to/log 2>> /path/to/errors.log
158
+ ```
159
+
15
160
 
16
- ## Doc
161
+ ## Améliorations Possibles
17
162
 
18
- You can generate documentation by executing: `sdoc`
19
- Don't forget to install the sdoc gem (`gem install sdoc`)
163
+ 1. Gestion des jours
164
+ 2. Planning horaire par jour
20
165
 
21
- ## Contributing
166
+
167
+ ## Contribuer
22
168
 
23
169
  1. Fork
24
- 2. Create your branch (`git checkout -b my-branch`)
25
- 3. Commit your new features (`git commit -am 'New features'`)
170
+ 2. Créer votre branche (`git checkout -b my-branch`)
171
+ 3. Commit les nouvelles fonctionnalités (`git commit -am 'New features'`)
26
172
  4. Push (`git push origin my-branch`)
27
- 5. Make a `Pull request`
173
+ 5. Faire une `Pull request`
174
+
28
175
 
29
176
  ## License
30
177
 
31
178
  [MIT License](MIT_LICENSE.txt)
32
179
 
180
+
33
181
  ## Author
34
182
 
35
183
  [Simon Ninon](http://sninon.fr) aka [ninon_s](http://intra.epitech.eu/user/ninon_s) aka [Cylix](http://github.com/Cylix)
data/example/susies.rb CHANGED
@@ -1,17 +1,32 @@
1
1
  #!/usr/bin/ruby
2
2
 
3
- require 'susies'
3
+ require 'Susies'
4
4
 
5
- data = {
6
- login: %w[login_x],
7
- maxStudent: 6,
8
- autologinURL: 'https://intra.epitech.eu/my_autologin_url',
9
- mailServer: 'smtp.gmail.com',
10
- mailPort: 587,
11
- mailUname: 'mail@gmail.com',
12
- mailPasswd: 'password',
13
- mailTargets: %w[mylogin_x@epitech.eu buddy_login@epitech.eu],
14
- buddiesAutologins: %w[https://intra.epitech.eu/buddy_autologin_url]
5
+ # whiteListFilters: susies MUST match these filters
6
+ whiteListFilters = {
7
+ login: %w[login_x],
8
+ maxStudent: 6,
9
+ minHour: 8,
10
+ maxHour: 17
15
11
  }
16
12
 
17
- Susies.new( data ).check!
13
+ # blackListFilters: susies MUST NOT match these filters
14
+ blackListFilters = {
15
+ type: 'reading',
16
+ title: 'zola'
17
+ }
18
+
19
+ # autologin: intranet authentication
20
+ autologins = {
21
+ autologinPath: '/autologin_path',
22
+ buddiesAutologins: %w[/buddy_autologin_path],
23
+ }
24
+
25
+ # mailInfos: mailer configuration
26
+ mailInfos = {
27
+ uname: 'mail@gmail.com',
28
+ passwd: 'password',
29
+ targets: %w[mylogin_x@epitech.eu buddy_login@epitech.eu]
30
+ }
31
+
32
+ Susies.new( autologins, whiteListFilters, blackListFilters, mailInfos ).check!
@@ -0,0 +1,59 @@
1
+ # Dependencies
2
+ require 'Susie.rb'
3
+ require 'net/http'
4
+ require 'json'
5
+
6
+ class IntraRequestsManager
7
+
8
+ INTRA_BASE_URL = 'intra.epitech.eu'
9
+ LIST_SUSIES_PATH = '/planning/1919/events'
10
+ REGISTER_SUSIE_PATH = '/planning/1919'
11
+
12
+
13
+ def initialize
14
+ @http = Net::HTTP.new INTRA_BASE_URL, 443
15
+ @http.use_ssl = true
16
+ end
17
+
18
+
19
+ def authenticate!(autologinPath)
20
+ response = @http.get autologinPath
21
+
22
+ response_cookies = response.get_fields 'set-cookie'
23
+ cookies_array = response_cookies.collect { |cookie| cookie.split('; ').first }
24
+
25
+ @cookies = cookies_array.join('; ')
26
+ end
27
+
28
+
29
+ def getSusies(startDate, endDate)
30
+ path = "#{ LIST_SUSIES_PATH }?start=#{ formatDate startDate }&end=#{ formatDate endDate }&format=json"
31
+ response = @http.get path, { 'Cookie' => @cookies }
32
+
33
+ susiesJSON = JSON.parse response.body
34
+
35
+ unless susiesJSON.nil?
36
+ susiesJSON.collect { |susieJSON| Susie.new susieJSON }
37
+ else
38
+ []
39
+ end
40
+ end
41
+
42
+
43
+ def registerSusie(susie)
44
+ path = "#{ REGISTER_SUSIE_PATH }/#{ susie.id }/subscribe?format=json&registercalendar"
45
+ response = @http.post path, '', { 'Cookie' => @cookies }
46
+
47
+ susie.nb_registered -= 1
48
+ susie
49
+ end
50
+
51
+
52
+ private
53
+
54
+
55
+ def formatDate(date)
56
+ date.strftime '%Y-%m-%d'
57
+ end
58
+
59
+ end
data/lib/Susie.rb ADDED
@@ -0,0 +1,43 @@
1
+ # Dependencies
2
+ require 'time'
3
+
4
+ class Susie
5
+
6
+ attr_accessor :is_registered, :nb_registered, :login, :id, :start, :end, :type, :title, :description, :url
7
+
8
+
9
+ def initialize(susieJSON)
10
+ self.id = susieJSON['id']
11
+ self.title = susieJSON['title']
12
+ self.type = susieJSON['type']
13
+ self.description = susieJSON['description']
14
+ self.login = susieJSON['maker']['login']
15
+ self.start = Time.parse susieJSON['start']
16
+ self.end = Time.parse susieJSON['end']
17
+ self.is_registered = susieJSON['event_registered']
18
+ self.nb_registered = susieJSON['registered']
19
+ self.url = "#{ IntraRequestsManager::INTRA_BASE_URL }#{ IntraRequestsManager::REGISTER_SUSIE_PATH }/#{ self.id }"
20
+ end
21
+
22
+
23
+ def to_s
24
+ <<-SUSIE
25
+ Login: #{ self.login }
26
+
27
+ Title: #{ self.title }
28
+ Type: #{ self.type }
29
+
30
+ Start: #{ self.start }
31
+ End: #{ self.end }
32
+
33
+ Places left: #{ 10 - self.nb_registered }
34
+
35
+ Registration URL: #{ self.url }
36
+
37
+ Description:
38
+ #{ self.description }
39
+
40
+ SUSIE
41
+ end
42
+
43
+ end
data/lib/Susies.rb ADDED
@@ -0,0 +1,136 @@
1
+ # Dependencies
2
+ require 'active_support/time'
3
+ require 'net/smtp'
4
+ require 'Susie.rb'
5
+ require 'IntraRequestsManager.rb'
6
+
7
+ class Susies
8
+
9
+ def initialize(autologins={}, whiteListFilters=nil, blackListFilters=nil, mailInfos=nil)
10
+ @whiteListFilters = whiteListFilters
11
+ @blackListFilters = blackListFilters
12
+ @mailInfos = mailInfos
13
+
14
+ @autologinPath = autologins[:autologinPath]
15
+ @buddiesAutologins = autologins[:buddiesAutologins] || []
16
+ @requestsManager = IntraRequestsManager.new
17
+ end
18
+
19
+
20
+ def check!
21
+ @requestsManager.authenticate! @autologinPath
22
+
23
+ startDate = Date.today.beginning_of_week
24
+ endDate = Date.today.end_of_week
25
+
26
+ loop do
27
+ log "Check for week #{ formatDate startDate } - #{ formatDate endDate }"
28
+ susies = @requestsManager.getSusies startDate, endDate
29
+
30
+ break if susies.empty?
31
+
32
+ findSusie susies unless registeredThisWeek? susies
33
+
34
+ startDate += 1.week
35
+ endDate += 1.week
36
+ end
37
+ end
38
+
39
+
40
+ private
41
+
42
+ def log(message)
43
+ puts "[#{ formatTime Time.now }]> #{ message }"
44
+ end
45
+
46
+
47
+ def findSusie(susies)
48
+ log "Not already registrated: seeking susies"
49
+ susies.each do |susie|
50
+ if !matchCriterias?(susie, @blackListFilters) && matchCriterias?(susie, @whiteListFilters)
51
+ log "Find susie matching criterias"
52
+ log "Registering to susie"
53
+ registerSusie susie
54
+ log "Sending mails"
55
+ informEveryone susie
56
+ log "done"
57
+ break
58
+ end
59
+ end
60
+ end
61
+
62
+
63
+ def formatDate(time)
64
+ time.strftime '%d-%m-%Y'
65
+ end
66
+
67
+ def formatTime(time)
68
+ time.strftime '%H:%M %d-%m-%Y'
69
+ end
70
+
71
+ def getRegisterMail(susie)
72
+ <<-MESSAGE
73
+ Hey,
74
+
75
+ I've just registered to a susie class.
76
+
77
+ #{ susie.to_s }
78
+ MESSAGE
79
+ end
80
+
81
+
82
+ def informEveryone(susie)
83
+ if @mailInfos && @mailInfos[:targets]
84
+ smtp = Net::SMTP.new 'smtp.gmail.com', 587
85
+ smtp.enable_starttls
86
+
87
+ smtp.start('gmail.com', @mailInfos[:uname], @mailInfos[:passwd], :plain) do |smtp|
88
+ smtp.send_message getRegisterMail(susie), @mailInfos[:email], @mailInfos[:targets]
89
+ end
90
+ end
91
+ end
92
+
93
+
94
+ def matchCriterias?(susie, filters)
95
+ return true unless filters
96
+
97
+ min_hour = filters[:minHour].nil? || susie.start.hour >= filters[:minHour]
98
+ max_hour = filters[:maxHour].nil? || susie.end.hour >= filters[:maxHour]
99
+ nb_students = filters[:nb_registered].nil? || susie.nb_registered <= filters[:nb_registered]
100
+ login = filters[:logins].nil? || filters[:logins].include?(susie.login)
101
+ type = filters[:type].nil? || susie.type == filters[:type]
102
+ title = filters[:title].nil? || susie.title.include?(filters[:title])
103
+
104
+ if filters[:logins]
105
+ filters[:logins].each do |login|
106
+ login = true if susie.login == login
107
+ end
108
+ end
109
+
110
+ return min_hour && max_hour && login && nb_students && type && title
111
+ end
112
+
113
+
114
+ def registerSusie(susie)
115
+ @requestsManager.registerSusie susie
116
+
117
+ if @buddiesAutologins
118
+ @buddiesAutologins.each do |autologin|
119
+ buddiesRequestsManager = IntraRequestsManager.new
120
+ buddiesRequestsManager.authenticate! autologin
121
+ buddiesRequestsManager.registerSusie susie
122
+ end
123
+ end
124
+ end
125
+
126
+
127
+ def registeredThisWeek?(susies)
128
+ susies.each do |susie|
129
+ return true if susie.is_registered
130
+ end
131
+
132
+ false
133
+ end
134
+
135
+
136
+ end
data/susies.gemspec CHANGED
@@ -1,14 +1,13 @@
1
1
  Gem::Specification.new do |s|
2
- s.name = 'susies'
3
- s.version = '0.0.2'
4
- s.date = '2013-04-11'
5
- s.summary = 'Epitech Susie Classes Bot'
6
- s.description = 'Susies is a Gem to register easily for Epitech Susie Classes'
7
- s.has_rdoc = true
8
- s.authors = ['Simon Ninon']
9
- s.email = 'simon.ninon@gmail.com'
10
- s.files = `git ls-files`.split("\n")
11
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
12
- s.license = 'MIT'
13
- s.homepage = 'https://github.com/Cylix/Susies'
2
+ s.name = 'susies'
3
+ s.version = '0.1.0'
4
+ s.date = '2014-09-28'
5
+ s.summary = 'Epitech Susie Classes Bot'
6
+ s.description = 'Susies is a Gem to register easily for Epitech Susie Classes'
7
+ s.authors = ['Simon Ninon']
8
+ s.email = 'simon.ninon@gmail.com'
9
+ s.files = `git ls-files`.split("\n")
10
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
11
+ s.license = 'MIT'
12
+ s.homepage = 'https://github.com/Cylix/Susies'
14
13
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: susies
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Ninon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-11 00:00:00.000000000 Z
11
+ date: 2014-09-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Susies is a Gem to register easily for Epitech Susie Classes
14
14
  email: simon.ninon@gmail.com
@@ -20,8 +20,9 @@ files:
20
20
  - MIT_LICENSE.txt
21
21
  - README.md
22
22
  - example/susies.rb
23
- - lib/constants.rb
24
- - lib/susies.rb
23
+ - lib/IntraRequestsManager.rb
24
+ - lib/Susie.rb
25
+ - lib/Susies.rb
25
26
  - susies.gemspec
26
27
  homepage: https://github.com/Cylix/Susies
27
28
  licenses:
@@ -43,8 +44,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
43
44
  version: '0'
44
45
  requirements: []
45
46
  rubyforge_project:
46
- rubygems_version: 2.2.1
47
+ rubygems_version: 2.0.3
47
48
  signing_key:
48
49
  specification_version: 4
49
50
  summary: Epitech Susie Classes Bot
50
51
  test_files: []
52
+ has_rdoc:
data/lib/constants.rb DELETED
@@ -1,57 +0,0 @@
1
- require 'active_support/time'
2
-
3
- class Susies
4
- # Susie login
5
- DEFAULT_LOGIN = "clark_s"
6
- # Maximum already registered students
7
- DEFAULT_MAX_STUDENT = 9
8
- # Minimum start hour
9
- DEFAULT_MIN_HOUR = 17
10
- # Default Autologin
11
- DEFAULT_AUTOLOGIN_URL = ''
12
- # Default mail server
13
- DEFAULT_MAIL_SERVER = ''
14
- # Default mail port
15
- DEFAULT_MAIL_PORT = 0
16
- # Default mail user name
17
- DEFAULT_MAIL_UNAME = ''
18
- # Default mail password
19
- DEFAULT_MAIL_PASSWD = ''
20
- # default start week
21
- DEFAULT_START_WEEK = Date.today.beginning_of_week
22
- # default end week
23
- DEFAULT_END_WEEK = Date.today.end_of_week
24
-
25
- # Susie JSON API
26
- SUSIES_URL = "https://intra.epitech.eu/planning/587/events"
27
- # Susie Register link
28
- REGISTER_URL = "https://intra.epitech.eu/planning/587"
29
-
30
- # Cookie File
31
- COOKIE_FILE = "cookies.txt"
32
- # Buddies Cookie File
33
- BUDDIES_COOKIE_FILE = "buddies_cookie_file.txt"
34
-
35
- # Encapsulation
36
- BASE_JSON = "activities"
37
- # Register status to a Susie Class
38
- EVENT_REGISTERED_JSON = "event_registered"
39
- # Nb of people already registered to a Susie Class
40
- REGISTERED_JSON = "registered"
41
- # Susie data
42
- MAKER_JSON = "maker"
43
- # Susie login
44
- MAKER_LOGIN_JSON = "login"
45
- # Susie Class ID
46
- ID_JSON = "id"
47
- # Susie Class Start
48
- START_JSON = "start"
49
- # Susie Class End
50
- END_JSON = "end"
51
- # Susie Class Type
52
- TYPE_JSON = "type"
53
- # Susie Class Title
54
- TITLE_JSON = "title"
55
- # Susie Class Desc
56
- DESC_JSON = "description"
57
- end
data/lib/susies.rb DELETED
@@ -1,285 +0,0 @@
1
- # Constants
2
- require 'constants.rb'
3
- # Dependencies
4
- require 'active_support/time'
5
- require 'net/smtp'
6
- require 'json'
7
-
8
- ##
9
- # Susies Class
10
- ##
11
- class Susies
12
-
13
- ##
14
- # Initialize
15
- #
16
- # @login Asked Susie Login Default: clark_s
17
- # @maxStudent Maximum already registered students Default: 9
18
- #
19
- # @autologinURL Intranet Autologin URL No default
20
- # @mailServer Mail Server No default
21
- # @mailPort Mail Port No default
22
- # @mailUname Mail UserName No default
23
- # @mailPasswd Mail Password No default
24
- # @mailTargets People to inform by mail about sub. Default: @mailUname
25
- #
26
- # @buddiesAuto. Buddies autologins No default
27
- #
28
- # @startWeek Beginning of week Default: beginning of current week
29
- # @endWeek End of week Default: end of current week
30
- ##
31
- def initialize(data={})
32
- @login = data[:login] || [DEFAULT_LOGIN]
33
- @maxStudent = data[:maxStudent] || DEFAULT_MAX_STUDENT
34
- @minHour = data[:minHour] || DEFAULT_MIN_HOUR
35
- @autologinURL = data[:autologinURL] || DEFAULT_AUTOLOGIN_URL
36
-
37
- @mailServer = data[:mailServer] || DEFAULT_MAIL_SERVER
38
- @mailPort = data[:mailPort] || DEFAULT_MAIL_PORT
39
- @mailUname = data[:mailUname] || DEFAULT_MAIL_UNAME
40
- @mailPasswd = data[:mailPasswd] || DEFAULT_MAIL_PASSWD
41
- @mailTargets = data[:mailTargets] || [@mailUname]
42
-
43
- @buddiesAutologins = data[:buddiesAutologins] || []
44
-
45
- @startWeek = DEFAULT_START_WEEK
46
- @endWeek = DEFAULT_END_WEEK
47
- end
48
-
49
-
50
- ##
51
- # Check!
52
- #
53
- # Iterate throw weeks, until there is no Susie in a week.
54
- # For each week, check if the current user is already registered to a Susie Class.
55
- # If user has not already been registered to a Susie Class, try to find an available Susie following criterias (maxStudent and Susie Login).
56
- # If a Susie class is found, try to register to this Susie Class and send a mail to user's friends.
57
- ##
58
- def check!
59
- log 'Start Checking'
60
- setAuthCookie COOKIE_FILE, @autologinURL
61
-
62
- while true
63
- susiesData = getSusiesData
64
-
65
- break if noSusie? susiesData
66
-
67
- findSusie susiesData unless registeredThisWeek? susiesData
68
-
69
- nextWeek!
70
- end
71
-
72
- log 'End Checking'
73
- end
74
-
75
-
76
- private
77
-
78
-
79
- ##
80
- # findSusie
81
- #
82
- # Try to find and register to a Susie Class.
83
- # Send Mail to user's friends in case of success.
84
- ##
85
- def findSusie(susiesData)
86
- log "Trying to register susie class with #{ @login } for week: #{ formatDate @startWeek } #{ formatDate @endWeek }."
87
-
88
- susiesData[BASE_JSON].each do |susie|
89
- if matchCriterias? susie
90
- registerSusie susie, COOKIE_FILE
91
- registerBuddies susie
92
- informBuddies susie
93
-
94
- return true
95
- end
96
- end
97
-
98
- log "No susie class found following criterias."
99
- return false
100
- end
101
-
102
-
103
- ##
104
- # formatDate
105
- #
106
- # Take Date Object as parameter
107
- # Return formated Date (YYY-MM-DD)
108
- ##
109
- def formatDate(date)
110
- date.strftime '%Y-%m-%d'
111
- end
112
-
113
-
114
- ##
115
- # formatTime
116
- #
117
- # Take Time Object as parameter
118
- # Return formated Time (HH:MM YYY-MM-DD)
119
- ##
120
- def formatTime(time)
121
- time.strftime '%H:%M %Y-%m-%d'
122
- end
123
-
124
-
125
- ##
126
- # getSusiesData
127
- #
128
- # Return Susie Classes JSON Data
129
- #
130
- def getSusiesData
131
- JSON.parse `curl -s -L -b #{ COOKIE_FILE } '#{ SUSIES_URL }?start=#{ formatDate @startWeek }&end=#{ formatDate @endWeek }&format=json'`
132
- end
133
-
134
-
135
- ##
136
- # getRegisterMail
137
- #
138
- # Generate mail which will be send to user's friends.
139
- ##
140
- def getRegisterMail(susie)
141
- <<-MESSAGE
142
- Hey,
143
-
144
- I've just registered to a susie class with #{ susie[MAKER_JSON][MAKER_LOGIN_JSON] }.
145
-
146
- Title: #{ susie[TITLE_JSON] }
147
- Type: #{ susie[TYPE_JSON] }
148
-
149
- Description:
150
- #{ susie[DESC_JSON] }
151
-
152
- Start: #{ susie[START_JSON] }.
153
- End: #{ susie[END_JSON] }.
154
- Place left: #{ 9 - susie[REGISTERED_JSON] }.
155
-
156
- Register here: #{ REGISTER_URL }/#{ susie[ID_JSON] }.
157
-
158
- MESSAGE
159
- end
160
-
161
-
162
- ##
163
- # informBuddies
164
- #
165
- # send mail to user's friends
166
- ##
167
- def informBuddies(susie)
168
- message = getRegisterMail susie
169
-
170
- for email in @mailTargets
171
- log "Send mail to #{ email }"
172
- sendMail email, message
173
- end
174
- end
175
-
176
-
177
- ##
178
- # log
179
- #
180
- # Write log info
181
- ##
182
- def log(message)
183
- puts "[#{ formatTime Time.now }]> #{ message }"
184
- end
185
-
186
-
187
- ##
188
- # matchCriterias
189
- #
190
- # Does Susie match defined criterias?
191
- ##
192
- def matchCriterias?(susie)
193
- @login.each do |login|
194
- return true if susie[MAKER_JSON][MAKER_LOGIN_JSON] == login and susie[REGISTERED_JSON] <= @maxStudent and Time.parse(susie[START_JSON]).hour >= @minHour
195
- end
196
- false
197
- end
198
-
199
-
200
- ##
201
- # nextWeek!
202
- #
203
- # Go to next week
204
- ##
205
- def nextWeek!
206
- @startWeek += 1.week
207
- @endWeek += 1.week
208
- end
209
-
210
-
211
- ##
212
- # noSusie?
213
- #
214
- # Check if there are SusieClasses in the given JSON data
215
- ##
216
- def noSusie?(susiesData)
217
- !susiesData[BASE_JSON] or susiesData[BASE_JSON].length == 0
218
- end
219
-
220
-
221
- ##
222
- # registerBuddies
223
- #
224
- # register buddies with given autologins
225
- ##
226
- def registerBuddies(susie)
227
- @buddiesAutologins.each do |autologin|
228
- setAuthCookie BUDDIES_COOKIE_FILE, autologin
229
- registerSusie susie, BUDDIES_COOKIE_FILE
230
- end
231
- end
232
-
233
-
234
- ##
235
- # registerSusie
236
- #
237
- # register to given SusieClass
238
- ##
239
- def registerSusie(susie, cookie_file)
240
- `curl -s -b #{ cookie_file } -L -X POST #{ REGISTER_URL }/#{ susie[ID_JSON] }/subscribe?format=json`
241
- log "Succesfully registered to susie class."
242
- end
243
-
244
-
245
- ##
246
- # registeredThisWeek?
247
- #
248
- # Determine if user is already registered to a Susie Class during the given week.
249
- ##
250
- def registeredThisWeek?(weekSusies)
251
- weekSusies[BASE_JSON].each do |susie|
252
- if susie[EVENT_REGISTERED_JSON]
253
- log "Already registered to a susie class with #{ susie[MAKER_JSON][MAKER_LOGIN_JSON] } for week: #{ formatDate @startWeek } #{ formatDate @endWeek }."
254
- return true
255
- end
256
- end
257
-
258
- log "Not registered to a susie class for week: #{ formatDate @startWeek } #{ formatDate @endWeek }."
259
- return false
260
- end
261
-
262
-
263
- ##
264
- # sendMail
265
- #
266
- # send mail from user to a buddy.
267
- ##
268
- def sendMail(email, message)
269
- smtp = Net::SMTP.new @mailServer, @mailPort
270
- smtp.enable_starttls_auto
271
- smtp.start @mailServer, @mailUname, @mailPasswd, :plain
272
- smtp.send_message message, @mailUname, email
273
- end
274
-
275
-
276
- ##
277
- # setAuthCookie
278
- #
279
- # Log on the intra and save user's cookie in COOKIE_FILE
280
- ##
281
- def setAuthCookie(cookie_file, autologin)
282
- `curl -s -L -c #{ cookie_file } #{ autologin }`
283
- end
284
-
285
- end