Class: GCalendarReader::Core

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cal_url, proxy_host, proxy_port, proxy_user, proxy_pass, nro_dias) ⇒ Core

Returns a new instance of Core.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gcalcore.rb', line 15

def initialize(cal_url, proxy_host, proxy_port, proxy_user, proxy_pass, nro_dias)
  self.events = []
  unless cal_url.empty?
    self.url = cal_url
    self.proxy_host=proxy_host
    self.proxy_port=proxy_port
    self.proxy_user=proxy_user
    self.proxy_pass=proxy_pass
    self.limit_date=Time.now + nro_dias
    self.parse!
  end
end

Instance Attribute Details

#eventsObject

Returns the value of attribute events.



12
13
14
# File 'lib/gcalcore.rb', line 12

def events
  @events
end

#icalObject

Returns the value of attribute ical.



12
13
14
# File 'lib/gcalcore.rb', line 12

def ical
  @ical
end

#limit_dateObject

Returns the value of attribute limit_date.



12
13
14
# File 'lib/gcalcore.rb', line 12

def limit_date
  @limit_date
end

#methodObject

Returns the value of attribute method.



12
13
14
# File 'lib/gcalcore.rb', line 12

def method
  @method
end

#product_idObject

Returns the value of attribute product_id.



12
13
14
# File 'lib/gcalcore.rb', line 12

def product_id
  @product_id
end

#proxy_hostObject

Returns the value of attribute proxy_host.



12
13
14
# File 'lib/gcalcore.rb', line 12

def proxy_host
  @proxy_host
end

#proxy_passObject

Returns the value of attribute proxy_pass.



12
13
14
# File 'lib/gcalcore.rb', line 12

def proxy_pass
  @proxy_pass
end

#proxy_portObject

Returns the value of attribute proxy_port.



12
13
14
# File 'lib/gcalcore.rb', line 12

def proxy_port
  @proxy_port
end

#proxy_userObject

Returns the value of attribute proxy_user.



12
13
14
# File 'lib/gcalcore.rb', line 12

def proxy_user
  @proxy_user
end

#scaleObject

Returns the value of attribute scale.



12
13
14
# File 'lib/gcalcore.rb', line 12

def scale
  @scale
end

#time_zone_nameObject

Returns the value of attribute time_zone_name.



12
13
14
# File 'lib/gcalcore.rb', line 12

def time_zone_name
  @time_zone_name
end

#time_zone_offsetObject

Returns the value of attribute time_zone_offset.



12
13
14
# File 'lib/gcalcore.rb', line 12

def time_zone_offset
  @time_zone_offset
end

#urlObject

Returns the value of attribute url.



12
13
14
# File 'lib/gcalcore.rb', line 12

def url
  @url
end

#versionObject

Returns the value of attribute version.



12
13
14
# File 'lib/gcalcore.rb', line 12

def version
  @version
end

#xmlObject

Returns the value of attribute xml.



12
13
14
# File 'lib/gcalcore.rb', line 12

def xml
  @xml
end

Class Method Details

.parse(cal_url) ⇒ Object



35
36
37
# File 'lib/gcalcore.rb', line 35

def self.parse(cal_url)
  self.new(cal_url)
end

Instance Method Details

#add_event(event, sortit = true) ⇒ Object



28
29
30
31
32
33
# File 'lib/gcalcore.rb', line 28

def add_event(event, sortit=true)
  self.events = [] unless self.events.is_a?(Array)
  self.events << event
  @events.sort! {|a,b| a.start_time <=> b.start_time } if sortit
  event
end

#calendar_raw_dataObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/gcalcore.rb', line 91

def calendar_raw_data
  # If you need to use a proxy:
  unless self.proxy_host.nil?
    response = Net::HTTP::Proxy(self.proxy_host, self.proxy_port,
    self.proxy_user, self.proxy_pass).get_response(URI.parse(self.url)) 
    case response
    when Net::HTTPSuccess, Net::HTTPRedirection
      #puts "Data #{data}"
      return response.body 
    else
      response.error!
      return nil
    end
  else
    response = Net::HTTP.get_response(URI.parse(self.url))
    case response
    when Net::HTTPSuccess, Net::HTTPRedirection
      #puts "Data #{data}"
      return response.body
    else
      response.error!
      return nil
    end      
  end
end

#events_in_range(start_time, end_time) ⇒ Object



87
88
89
# File 'lib/gcalcore.rb', line 87

def events_in_range(start_time, end_time)
  events.inject([]) {|in_range,e| e.start_time < end_time && e.finish_time > start_time ? in_range.push(e) : in_range}
end

#future_eventsObject



77
78
79
80
# File 'lib/gcalcore.rb', line 77

def future_events
  t = DateTime.now
  events.inject([]) {|future,e| e.start_time > t ? future.push(e) : future}
end

#parseObject



42
43
44
# File 'lib/gcalcore.rb', line 42

def parse
  self.dup.parse!
end

#parse!Object



39
40
41
# File 'lib/gcalcore.rb', line 39

def parse!
  self.url =~ /\.ics(?:\?.+)?$/ ? self.parse_from_ical! : self.parse_from_xml!
end

#parse_from_icalObject



69
70
71
# File 'lib/gcalcore.rb', line 69

def parse_from_ical
  self.dup.parse_from_ical
end

#parse_from_ical!Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gcalcore.rb', line 53

def parse_from_ical!
  rawdata = self.calendar_raw_data
  cals = RiCal.parse_string(rawdata)
  cal = cals.first
  cal.events.each do |event|
    unless event.occurrences({:before => self.limit_date}).size > 1
      self.add_event(event, false) # (disable sorting until done)
    else
      event.occurrences({:before => self.limit_date}).each do |ev|  
        self.add_event(ev, false) # (disable sorting until done)
      end
    end
  end
  @events.reject! {|e| e.start_time.nil?}
  @events.sort! {|a,b| a.start_time <=> b.start_time }
end

#parse_from_xmlObject



49
50
51
# File 'lib/gcalcore.rb', line 49

def parse_from_xml
  self.dup.parse_from_xml!
end

#parse_from_xml!Object



46
47
48
# File 'lib/gcalcore.rb', line 46

def parse_from_xml!
  return false # THIS IS NOT IMPLEMENTED YET!!
end

#past_eventsObject



82
83
84
85
# File 'lib/gcalcore.rb', line 82

def past_events
  t = DateTime.now
  events.inject([]) {|past,e| e.start_time < t ? past.push(e) : past}
end

#source_formatObject



73
74
75
# File 'lib/gcalcore.rb', line 73

def source_format
  self.ical ? 'ical' : (self.xml ? 'xml' : nil)
end