Class: GoogleExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/google_exporter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGoogleExporter

Returns a new instance of GoogleExporter.



47
48
49
50
# File 'lib/google_exporter.rb', line 47

def initialize
  @tz = TZInfo::Timezone.get('America/Montreal')
  @sentEvents = Array.new
end

Instance Attribute Details

#sentEventsObject (readonly)

Returns the value of attribute sentEvents.



45
46
47
# File 'lib/google_exporter.rb', line 45

def sentEvents
  @sentEvents
end

Class Method Details

.from_client(client) ⇒ Object



52
53
54
55
56
# File 'lib/google_exporter.rb', line 52

def self.from_client(client)
  exporter = self.new
  exporter.client = client
  exporter.service = client.service
end

Instance Method Details

#auth_cliObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/google_exporter.rb', line 166

def auth_cli
  oauth_yaml = YAML.load_file(Poly::userConfDir + '/google-api.yaml')
  client = Google::APIClient.new
  client.authorization.client_id = oauth_yaml["client_id"]
  client.authorization.client_secret = oauth_yaml["client_secret"]
  client.authorization.scope = oauth_yaml["scope"]
  client.authorization.refresh_token = oauth_yaml["refresh_token"]
  client.authorization.access_token = oauth_yaml["access_token"]
  
  if client.authorization.refresh_token && client.authorization.expired?
    client.authorization.fetch_access_token!
  end
  
  @service = client.discovered_api('calendar', 'v3')
  
  @client = client
end

#authURIObject



162
163
164
# File 'lib/google_exporter.rb', line 162

def authURI
  @client.authorization.authorization_uri.to_s
end

#authWeb(code, callBackURI, tokenPair = nil) ⇒ Object

Returns access token



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/google_exporter.rb', line 138

def authWeb(code,callBackURI,tokenPair = nil)
  oauth_yaml = YAML.load_file(Poly::userConfDir + '/google-api.yaml')
  
  @client = Google::APIClient.new
  @client.authorization.client_id = oauth_yaml["client_id"]
  @client.authorization.client_secret = oauth_yaml["client_secret"]
  @client.authorization.scope = oauth_yaml["scope"]
  @client.authorization.redirect_uri = callBackURI
  @client.authorization.code = code if code
  
  if tokenPair.set?
    @client.authorization.update_token!(tokenPair.to_hash)
  end
  
  if @client.authorization.refresh_token && @client.authorization.expired?
    @client.authorization.fetch_access_token!
  end
  
  @service = @client.discovered_api('calendar', 'v3')
  
  return @client.authorization.access_token
end

#calendarListObject

return : hash[:calendarID => :calendarName]



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/google_exporter.rb', line 117

def calendarList
  list = Hash.new
  
  page_token = nil
  result = @client.execute(:api_method => @service.calendar_list.list)
  while true
    entries = result.data.items
    entries.each do |e|
      list[e.id] = e.summary
    end
    if !(page_token = result.data.next_page_token)
      break
    end
    result = @client.execute(:api_method => @service.calendar_list.list,
                            :parameters => {'pageToken' => page_token})
  end
  
  list
end

#deleteEvent(eventID, calendarID) ⇒ Object



104
105
106
107
# File 'lib/google_exporter.rb', line 104

def deleteEvent(eventID,calendarID)
  result = @client.execute(:api_method => @service.events.delete,
                          :parameters => {'calendarId' => calendarID, 'eventId' => eventID})
end

#deleteSentEventsObject



109
110
111
112
113
# File 'lib/google_exporter.rb', line 109

def deleteSentEvents
  @sentEvents.each do |event|
    deleteEvent(event.data.id,@sentCalendarID)
  end  
end

#newTokenPairObject



184
185
186
187
188
189
# File 'lib/google_exporter.rb', line 184

def newTokenPair 
  @client.authorization.fetch_access_token!
  token = TokenPair.new(nil)
  token.update_token!(@client.authorization)
  return token
end

#send(schedule, toCalID) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/google_exporter.rb', line 60

def send(schedule,toCalID)
  @sentEvents = Array.new
  events = Array.new
  @sentCalendarID = toCalID
  
  schedule.courses.each do |course|
    course.periods.each do |period|
      
      courseBeginsOn = schedule.trimester.starts + period.weekDay - 1
      courseBeginsOn += 7 if period.week == 2
      event = {
        'summary' => course.acronym + '(' + period.group + ') ' + (period.isLab ? '[Lab]' : '') ,
        'description' => course.description ,
        'location' => period.location,
        'timeZone' => 'America/Montreal',
        'start' => {
          'dateTime' => dateTime(courseBeginsOn, period.from),
          'timeZone' => 'America/Montreal'
        },
        'end' => {
          'dateTime' => dateTime(courseBeginsOn, period.to),
          'timeZone' => 'America/Montreal'
        },
        'recurrence' => [
            'RDATE;VALUE=DATE:' + rDates(schedule.trimester,period)
          ]
      }

      result = @client.execute!(:api_method => @service.events.insert,
                      :parameters => {'calendarId' => toCalID},
                      :body_object => event,
                      :headers => {'Content-Type' => 'application/json'})
      @sentEvents.push(result)
      rEvent = {
        :id => result.data.id,
        :summary => result.data.summary,
        :htmlLink => result.data.htmlLink
      }
      events.push(rEvent)
    end
  end
  return events
end