Class: GData

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

Defined Under Namespace

Classes: HTTPFailure

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ GData

Returns a new instance of GData.



47
48
49
# File 'lib/gcal.rb', line 47

def initialize(uri)
  @uri = uri
end

Instance Method Details

#calendarObject



80
81
82
83
84
85
86
87
# File 'lib/gcal.rb', line 80

def calendar
  unless @calendar
    data = get_calendar
    @calendar = parse_calendar(data)
    @calendar << parse_events(data)
  end
  @calendar
end

#get_calendarObject

Raises:



89
90
91
92
93
94
# File 'lib/gcal.rb', line 89

def get_calendar
  http = Net::HTTP.new('www.google.com', 80)
  response, data = http.get(@uri)
  raise HTTPFailure unless response.is_a? Net::HTTPSuccess
  data
end

#new_event(event = {}) ⇒ Object

Raises:



51
52
53
54
55
56
57
# File 'lib/gcal.rb', line 51

def new_event(event={})
  entry = new_xml(event)
  http = Net::HTTP.new('www.google.com', 80)
  response, data = http.post(@uri, entry)
  raise HTTPFailure unless response.is_a? Net::HTTPSuccess
  return data
end

#new_xml(event = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gcal.rb', line 59

def new_xml(event={})
  content = <<-EOF
  <?xml version="1.0"?>
  <entry xmlns='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005'>
    <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/g/2005#event'></category>
    <title type='text'>#{event[:title]}</title>
    <content type='text'>#{event[:content]}</content>
    <author>
      <name>#{event[:author]}</name>
      <email>#{event[:email]}</email>
    </author>
    
    <gd:transparency value='http://schemas.google.com/g/2005#event.opaque'></gd:transparency>
    <gd:eventStatus value='http://schemas.google.com/g/2005#event.confirmed'> </gd:eventStatus>
    
    <gd:where valueString='#{event[:where]}'></gd:where>
    <gd:when startTime='#{event[:startTime]}' endTime='#{event[:endTime]}'></gd:when>
  </entry>
  EOF
end

#parse_calendar(data) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/gcal.rb', line 96

def parse_calendar(data)
  cal = Hpricot.XML(data)
  hash = {}
  hash[:title] = cal.search("//feed/title").inner_html
  hash[:subtitle] = cal.search("//feed/subtitle").inner_html
  hash[:id] = cal.search("//feed/id").inner_html
  hash[:link] = cal.search("//feed/link[@rel='self']").attr('href')
  hash[:author_name] = cal.search("//feed/author/name").inner_html
  hash[:author_email] = cal.search("//feed/author/email").inner_html
  hash[:version] = cal.search("//feed/generator").attr('version')
  hash[:where] = (cal/"gd:where").select {|x| x.parent.name == "feed" }[0].attributes['valueString']
  return GCalendar.new(hash)
end

#parse_events(data) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/gcal.rb', line 110

def parse_events(data)
  cal = Hpricot.XML(data)
  (cal/:entry).map do |event|
    hash = {}
    hash[:title] = (event/:title).inner_html
    hash[:content] = (event/:content).inner_html
    hash[:id] = (event/:id).inner_html
    hash[:author_name] = (event/:author/:name).inner_html
    hash[:author_email] = (event/:author/:email).inner_html
    hash[:start_time] = (event/'gd:when').attr('startTime')
    hash[:end_time] = (event/'gd:when').attr('endTime')
    hash[:where] = (event/"gd:where").inner_html
    GEvent.new(hash)
  end
end