polyhoraire 1.0.1 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ = Installation
2
+
3
+ gem install polyhoraire
4
+
1
5
  = Setup
2
6
 
3
7
  rake setup
@@ -3,17 +3,61 @@ require 'google/api_client'
3
3
  require 'nokogiri'
4
4
  require 'yaml'
5
5
  require 'tzinfo'
6
+ require 'data_mapper'
7
+
8
+ require 'polyhoraire'
9
+
10
+
11
+ # Set up our token store
12
+ DataMapper.setup(:default, 'sqlite::memory:')
13
+ class TokenPair
14
+ include DataMapper::Resource
15
+
16
+ property :id, Serial
17
+ property :refresh_token, String, :length => 255
18
+ property :access_token, String, :length => 255
19
+ property :expires_in, Integer
20
+ property :issued_at, Integer
21
+
22
+
23
+ def update_token!(object)
24
+ self.refresh_token = object.refresh_token
25
+ self.access_token = object.access_token
26
+ self.expires_in = object.expires_in
27
+ self.issued_at = object.issued_at
28
+ end
29
+
30
+ def to_hash
31
+ return {
32
+ :refresh_token => refresh_token,
33
+ :access_token => access_token,
34
+ :expires_in => expires_in,
35
+ :issued_at => Time.at(issued_at)
36
+ }
37
+ end
38
+ end
39
+ TokenPair.auto_migrate!
40
+
6
41
 
7
42
  class GoogleExporter
43
+ attr_reader :sentEvents
8
44
 
9
45
  def initialize
10
- auth
11
46
  @tz = TZInfo::Timezone.get('America/Montreal')
12
47
  @sentEvents = Array.new
13
48
  end
49
+
50
+ def self.from_client(client)
51
+ exporter = self.new
52
+ exporter.client = client
53
+ exporter.service = client.service
54
+ end
55
+
56
+
14
57
 
15
58
  def send(schedule,toCalID)
16
59
  @sentEvents = Array.new
60
+ events = Array.new
17
61
  @sentCalendarID = toCalID
18
62
 
19
63
  schedule.courses.each do |course|
@@ -38,13 +82,20 @@ class GoogleExporter
38
82
  ]
39
83
  }
40
84
 
41
- result = @client.execute(:api_method => @service.events.insert,
85
+ result = @client.execute!(:api_method => @service.events.insert,
42
86
  :parameters => {'calendarId' => toCalID},
43
87
  :body_object => event,
44
88
  :headers => {'Content-Type' => 'application/json'})
45
89
  @sentEvents.push(result)
90
+ rEvent = {
91
+ :id => result.data.id,
92
+ :summary => result.data.summary,
93
+ :htmlLink => result.data.htmlLink
94
+ }
95
+ events.push(rEvent)
46
96
  end
47
97
  end
98
+ return events
48
99
  end
49
100
 
50
101
  def deleteEvent(eventID,calendarID)
@@ -57,17 +108,14 @@ class GoogleExporter
57
108
  deleteEvent(event.data.id,@sentCalendarID)
58
109
  end
59
110
  end
60
-
61
- def selectCalendar(idUrl)
62
-
63
- end
111
+
64
112
 
65
113
  # return : hash[:calendarID => :calendarName]
66
114
  def calendarList
67
115
  list = Hash.new
68
116
 
69
117
  page_token = nil
70
- result = client.execute(:api_method => service.calendar_list.list)
118
+ result = @client.execute(:api_method => @service.calendar_list.list)
71
119
  while true
72
120
  entries = result.data.items
73
121
  entries.each do |e|
@@ -76,12 +124,60 @@ class GoogleExporter
76
124
  if !(page_token = result.data.next_page_token)
77
125
  break
78
126
  end
79
- result = client.execute(:api_method => service.calendar_list.list,
127
+ result = @client.execute(:api_method => @service.calendar_list.list,
80
128
  :parameters => {'pageToken' => page_token})
81
129
  end
130
+
131
+ list
132
+ end
133
+
134
+ # Returns access token
135
+ def authWeb(code,tokenID,callBackURI)
136
+ oauth_yaml = YAML.load_file(Poly::userConfDir + '/google-api.yaml')
137
+
138
+ @client = Google::APIClient.new
139
+ @client.authorization.client_id = oauth_yaml["client_id"]
140
+ @client.authorization.client_secret = oauth_yaml["client_secret"]
141
+ @client.authorization.scope = oauth_yaml["scope"]
142
+ @client.authorization.redirect_uri = callBackURI
143
+ @client.authorization.code = code if code
144
+
145
+ if tokenID
146
+ # Load the access token here if it's available
147
+ token_pair = TokenPair.get(tokenID)
148
+ @client.authorization.update_token!(token_pair.to_hash)
149
+ end
150
+
151
+ if @client.authorization.refresh_token && @client.authorization.expired?
152
+ @client.authorization.fetch_access_token!
153
+ end
154
+
155
+ @service = @client.discovered_api('calendar', 'v3')
156
+
157
+ return @client.authorization.access_token
158
+ end
159
+
160
+ # Returns the session token
161
+ def authWebCallback(tokenID)
162
+ @client.authorization.fetch_access_token!
163
+ # Persist the token here
164
+ token_pair = if tokenID
165
+ TokenPair.get(tokenID)
166
+ else
167
+ TokenPair.new
168
+ end
169
+ TokenPair.auto_migrate!
170
+ token_pair.update_token!(@client.authorization)
171
+ token_pair.save
172
+
173
+ return token_pair.id
174
+ end
175
+
176
+ def authURI
177
+ @client.authorization.authorization_uri.to_s
82
178
  end
83
179
 
84
- def auth
180
+ def auth_cli
85
181
  oauth_yaml = YAML.load_file(Poly::userConfDir + '/google-api.yaml')
86
182
  client = Google::APIClient.new
87
183
  client.authorization.client_id = oauth_yaml["client_id"]
@@ -6,6 +6,8 @@ require 'nokogiri'
6
6
 
7
7
 
8
8
  module Poly
9
+ require 'polyhoraire/auth'
10
+ require 'polyhoraire/schedule'
9
11
 
10
12
  URL = {
11
13
  :connection => "https://www4.polymtl.ca/servlet/ValidationServlet",
@@ -1,3 +1,4 @@
1
+ #encoding: UTF-8
1
2
  require 'polyhoraire'
2
3
 
3
4
  class Poly::Auth
@@ -78,7 +79,13 @@ class Poly::Auth
78
79
 
79
80
 
80
81
  def setStatus(response)
82
+ @connected = false
83
+
81
84
  doc = Nokogiri::HTML(response.body.to_s)
85
+
86
+ node = doc.xpath("//font[contains(string(.),'Le système est temporairement hors d')]")
87
+ raise 'Maintenance du dossier etudiant' unless node.empty?
88
+
82
89
  nodes = doc.xpath("//center/font[@color = '#FF0000']")
83
90
  if nodes.empty?
84
91
  @connected = true
@@ -26,4 +26,9 @@ class TestGoogleExporter < Test::Unit::TestCase
26
26
  STDIN.gets.chomp
27
27
  @exporter.deleteSentEvents()
28
28
  end
29
+
30
+ def test_calendarList
31
+ list = @exporter.calendarList
32
+ assert(list.has_key?('charlesbriere.flatzo@gmail.com'))
33
+ end
29
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polyhoraire
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: '1.1'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-10 00:00:00.000000000 Z
12
+ date: 2012-01-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
- requirement: &10452780 !ruby/object:Gem::Requirement
16
+ requirement: &16402240 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *10452780
24
+ version_requirements: *16402240
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: tzinfo
27
- requirement: &10452340 !ruby/object:Gem::Requirement
27
+ requirement: &16401800 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *10452340
35
+ version_requirements: *16401800
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: google-api-client
38
- requirement: &10451920 !ruby/object:Gem::Requirement
38
+ requirement: &16401380 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *10451920
46
+ version_requirements: *16401380
47
47
  description: Retrieve schedule from Polytechnique de Montreal and exports it to different
48
48
  formats(only google calendar is suported at this time)
49
49
  email: charles.briere@polymtl.ca