Class: Ical2gcal::Google

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

Defined Under Namespace

Classes: CalendarIdNotDefined

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(calendar_id, store = nil) ⇒ Google

param

String calendar_id

param

String store



17
18
19
20
21
22
23
24
25
26
# File 'lib/google.rb', line 17

def initialize(calendar_id, store = nil)
  raise CalendarIdNotDefined.new unless calendar_id

  @calendar_id = nil
  @calendar    = nil # Calendar API
  @client      = nil # Google API Client
  @events      = nil

  init_and_auth_calendar(calendar_id, store)
end

Instance Attribute Details

#calendarObject (readonly)

Returns the value of attribute calendar.



27
28
29
# File 'lib/google.rb', line 27

def calendar
  @calendar
end

#calendar_idObject (readonly)

Returns the value of attribute calendar_id.



27
28
29
# File 'lib/google.rb', line 27

def calendar_id
  @calendar_id
end

#clientObject (readonly)

Returns the value of attribute client.



27
28
29
# File 'lib/google.rb', line 27

def client
  @client
end

Instance Method Details

#all_day?(event) ⇒ Boolean

param

Rical::Component::Event event

return

Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/google.rb', line 90

def all_day?(event)
  event.dtstart.class == Date
end

#all_eventsObject

return

Array



122
123
124
125
126
127
128
129
130
131
# File 'lib/google.rb', line 122

def all_events
  result = events_request
  events = result['items']
  while ( result['nextPageToken'] )
    result = events_request(result['nextPageToken'])
    events += result['items']
  end

  events
end

#create_event(event) ⇒ Object

param

RiCal::Component::Event

return

String



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/google.rb', line 61

def create_event( event )
  body = {:summary => event.summary.force_encoding('UTF-8')}
  body.merge!(:description => event.description.force_encoding('UTF-8')) if event.description
  body.merge!(:location    => event.location.force_encoding('UTF-8')) if event.location

  if all_day?(event)
    start_date = event.dtstart.to_s
    end_date   = event.dtend.to_s

    body.merge!(
      :start => {:date => start_date},
      :end   => {:date => end_date.size > 0 ? end_date : (Date.parse(start_date) + 1).to_s})
  else
    body.merge!(
      :start => {:dateTime => localtime(event.start_time)},
      :end   => {:dateTime => localtime((event.respond_to? :end_time) ? event.end_time : event.start_time)})
  end

  client.execute(
    :api_method => calendar.events.insert,
    :parameters => {:calendarId  => calendar_id},
    :headers    => {'Content-Type' => 'application/json'},
    :body       => JSON.dump(body)).response.body
end

#events_request(next_page_token = nil) ⇒ Object

param

String next_page_token

return

Hash



137
138
139
140
141
142
143
144
# File 'lib/google.rb', line 137

def events_request(next_page_token = nil)
  params = {:calendarId => calendar_id}
  params.merge!(:pageToken => next_page_token) if next_page_token

  JSON.parse(client.execute(
                    :api_method => calendar.events.list,
                    :parameters => params).response.body)
end

#init_and_auth_calendar(calendar_id, store) ⇒ Object

param

String calendar_id

param

String dir

return

Object Calendar API



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/google.rb', line 34

def init_and_auth_calendar(calendar_id, store)
  @client = ::Google::APIClient.new(
                              :application_name    => :ical2gcal,
                              :application_version => Version)

  credential = ::Google::APIClient::FileStorage.new(store)
  secrets    = ::Google::APIClient::ClientSecrets.load(File.dirname(store))

  if credential.authorization.nil?
    flow = ::Google::APIClient::InstalledAppFlow.new(
      :client_id     => secrets.client_id,
      :client_secret => secrets.client_secret,
      :scope         => 'https://www.googleapis.com/auth/calendar')
    client.authorization = flow.authorize
    credential.write_credentials(client.authorization)
  else
    client.authorization = credential.authorization
  end

  @calendar_id = calendar_id
  @calendar    = client.discovered_api('calendar', 'v3')
end

#localtime(datetime) ⇒ Object

create Time object with local timezone

param

String datetime

return

String



100
101
102
# File 'lib/google.rb', line 100

def localtime(datetime)
  Time.parse(datetime.iso8601.sub(/(\+.*)\z/, '')).iso8601
end

#remove_all_eventsObject



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

def remove_all_events
  all_events.each {|e| remove_one_event(e)}
end

#remove_one_event(event) ⇒ Object

remove one event with retry

param

GoogleCalendar::Event



113
114
115
116
117
# File 'lib/google.rb', line 113

def remove_one_event(event)
  client.execute(
    :api_method => calendar.events.delete,
    :parameters => {:calendarId => calendar_id, :eventId => event['id']})
end