Module: GoogleCalendarApiV2::Response::Base

Included in:
Calendar, Event
Defined in:
lib/google_calendar_api_v2/response/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



6
7
8
# File 'lib/google_calendar_api_v2/response/base.rb', line 6

def attributes
  @attributes
end

#responseObject (readonly)

Returns the value of attribute response.



6
7
8
# File 'lib/google_calendar_api_v2/response/base.rb', line 6

def response
  @response
end

Instance Method Details

#[](key) ⇒ Object



21
22
23
# File 'lib/google_calendar_api_v2/response/base.rb', line 21

def [](key)
  @attributes[key.to_s]
end

#[]=(key, value) ⇒ Object



17
18
19
# File 'lib/google_calendar_api_v2/response/base.rb', line 17

def []=(key, value)
  @attributes[key.to_s]=value
end

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/google_calendar_api_v2/response/base.rb', line 48

def destroy(url = nil, redirect_count = 0)
  url ||= @attributes['selfLink']
  response = @connection.delete url, GoogleCalendarApiV2::Client::HEADERS.merge({ 'If-Match' => '*' })

  raise 'Redirection Loop' if redirect_count > 3

  if success? response
    true
  elsif redirect? response
    destroy(response['location'], redirect_count += 1)
  else
    false
  end
end

#initialize(item, connection, parent = nil) ⇒ Object



8
9
10
11
# File 'lib/google_calendar_api_v2/response/base.rb', line 8

def initialize(item, connection, parent = nil)
  @response, @connection = response, connection
  @attributes = item
end

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/google_calendar_api_v2/response/base.rb', line 32

def save(url = nil, redirect_count = 0)
  url ||= @attributes['selfLink']
  response = @connection.put url, self.to_json, GoogleCalendarApiV2::Client::HEADERS

  raise 'Redirection Loop' if redirect_count > 3

  if success? response
    @attributes = JSON.parse(res.body)['data'] rescue @attributes
    true
  elsif redirect? response
    save(response['location'], redirect_count += 1)
  else
    false
  end
end

#to_jsonObject



25
26
27
28
29
30
# File 'lib/google_calendar_api_v2/response/base.rb', line 25

def to_json
  {
    "apiVersion" => "2.3",
    :data => @attributes
  }.to_json
end

#tokenObject



63
64
65
# File 'lib/google_calendar_api_v2/response/base.rb', line 63

def token
  @attributes['id'].split('/').last
end

#update_attributes(attributes) ⇒ Object



13
14
15
# File 'lib/google_calendar_api_v2/response/base.rb', line 13

def update_attributes(attributes)
  @attributes.merge!(attributes)
end