Class: GoogleCalendarApiV2::Calendar

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/google_calendar_api_v2/calendar.rb

Instance Attribute Summary collapse

Attributes included from Base

#connection

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Calendar

Returns a new instance of Calendar.



7
8
9
# File 'lib/google_calendar_api_v2/calendar.rb', line 7

def initialize(connection)
  @connection = connection
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



5
6
7
# File 'lib/google_calendar_api_v2/calendar.rb', line 5

def events
  @events
end

Instance Method Details

#all(url = nil, redirect_count = 0) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/google_calendar_api_v2/calendar.rb', line 25

def all(url = nil, redirect_count = 0)
  url ||= "https://www.google.com/calendar/feeds/default/allcalendars/full?alt=jsonc"
  response = @connection.get url, Client::HEADERS

  if success? response
    # Response::Event.new(response, @connection, @calendar)
    if items = JSON.parse(response.body)['data']['items']
      items.map {|item| Response::Calendar.new(item, @connection, @calendar) }
    else
      []
    end
  elsif redirect? response
    all(response['location'], redirect_count += 1)
  end
end

#create(params = {}, url = nil, redirect_count = 0) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/google_calendar_api_v2/calendar.rb', line 41

def create(params = {}, url = nil, redirect_count = 0)
  url ||= '/calendar/feeds/default/owncalendars/full?alt=jsonc'
  response = @connection.post url,
  {
    :data => {
      :title => "Unnamed calendar",
      :hidden => false
    }.merge(params)
  }.to_json, Client::HEADERS

  raise 'Redirection Loop' if redirect_count > 3

  if success? response
    item = JSON.parse(response.body)['data']
    Response::Calendar.new(item, @connection)
  elsif redirect?(response)
    create(params, response['location'], redirect_count += 1)
  end
end

#find(calendar_token, url = nil, redirect_count = 0) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/google_calendar_api_v2/calendar.rb', line 11

def find(calendar_token, url = nil, redirect_count = 0)
  url ||= "https://www.google.com/calendar/feeds/default/allcalendars/full/#{calendar_token}?alt=jsonc"
  response = @connection.get url, Client::HEADERS

  raise 'Redirection Loop' if redirect_count > 3

  if success? response
    item = JSON.parse(response.body)['data']
    Response::Calendar.new(item, @connection)
  elsif redirect? response
    find(calendar_token, response['location'], redirect_count += 1)
  end
end