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



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

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.



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

def calendar
  @calendar
end

#calendar_idObject (readonly)

Returns the value of attribute calendar_id.



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

def calendar_id
  @calendar_id
end

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

Instance Method Details

#all_day?(event) ⇒ Boolean

param

Rical::Component::Event event

return

Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/google.rb', line 93

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

#all_eventsObject

return

Array



125
126
127
128
129
130
131
132
133
134
# File 'lib/google.rb', line 125

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



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

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



140
141
142
143
144
145
146
147
# File 'lib/google.rb', line 140

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



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

def init_and_auth_calendar(calendar_id, store)
  @client = ::Google::APIClient.new(
                              :application_name    => :ical2gcal,
                              :application_version => Ical2gcal::VERSION,
                              :user_agent => "ical2gcal-#{Ical2gcal::VERSION} (#{RUBY_PLATFORM})"
  )

  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



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

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

#remove_all_eventsObject



107
108
109
# File 'lib/google.rb', line 107

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



116
117
118
119
120
# File 'lib/google.rb', line 116

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