Class: Gaah::Calendar::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/gaah/calendar/api.rb

Class Method Summary collapse

Class Method Details

.calendars(xoauth_requestor_id, options = {}) ⇒ Object

API: CalendarList: list



11
12
13
14
15
16
17
18
19
20
# File 'lib/gaah/calendar/api.rb', line 11

def calendars(xoauth_requestor_id, options = {})
  url = "https://www.googleapis.com/calendar/v3/users/me/calendarList"
  params = {
    xoauth_requestor_id: xoauth_requestor_id,
    minAccessRole:       options[:min_access_role] || 'writer',
    showHidden:          options[:show_hidden]     || false,
  }
  calendars = JSON.load(ApiClient.instance.get(url, params))
  Calendar.batch_create(calendars['items'])
end

.create_event(xoauth_requestor_id, options) ⇒ Object

API: Events#insert



32
33
34
35
36
37
38
39
# File 'lib/gaah/calendar/api.rb', line 32

def create_event(xoauth_requestor_id, options)
  url    = build_api_url(options.delete(:email))
  params = { xoauth_requestor_id: xoauth_requestor_id }
  body   = build_create_api_body(options)
  json   = ApiClient.instance.post(url, params, body)

  Gaah::Calendar::Event.new(JSON.load(json))
end

.delete_event(xoauth_requestor_id, options) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gaah/calendar/api.rb', line 52

def delete_event(xoauth_requestor_id, options)
  base   = build_api_url(options.delete(:email))
  id     = options.delete(:event_id)
  url    = "#{base}/#{id}"
  params = { xoauth_requestor_id: xoauth_requestor_id }
  ApiClient.instance.delete(url, params)
  { success: true }
rescue Gaah::UnknownHTTPException => exception
  case exception.message
  when '404'
    { success: false, error: "Event was not found." }
  when '410'
    { success: false, error: "Event is already canceled." }
  end
end

.event(xoauth_requestor_id, options) ⇒ Object

API: Events#get



42
43
44
45
46
47
48
49
50
# File 'lib/gaah/calendar/api.rb', line 42

def event(xoauth_requestor_id, options)
  base   = build_api_url(options.delete(:email))
  id     = options.delete(:event_id)
  url    = "#{base}/#{id}"
  params = { xoauth_requestor_id: xoauth_requestor_id }
  json   = ApiClient.instance.get(url, params)

  Gaah::Calendar::Event.new(JSON.load(json))
end

.events(xoauth_requestor_id, options) ⇒ Object

API: Events#list



23
24
25
26
27
28
29
# File 'lib/gaah/calendar/api.rb', line 23

def events(xoauth_requestor_id, options)
  url    = build_api_url(options[:email])
  params = build_events_api_params(xoauth_requestor_id, options)
  json   = ApiClient.instance.get(url, params)
  events = JSON.load(json)
  Event.batch_create(events['items'])
end