Module: IcalPunch

Defined in:
lib/ical_punch.rb

Constant Summary collapse

VERSION =

:stopdoc:

'0.5.0'
LIBPATH =
::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
PATH =
::File.dirname(LIBPATH) + ::File::SEPARATOR

Class Method Summary collapse

Class Method Details

.calendarsObject



21
22
23
# File 'lib/ical_punch.rb', line 21

def calendars
  @calendars
end

.calendars=(value) ⇒ Object



25
26
27
# File 'lib/ical_punch.rb', line 25

def calendars=(value)
  @calendars = value
end

.calendars_to_punchObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ical_punch.rb', line 86

def calendars_to_punch
  @data = {}
  @calendars.each do |calendar|
    key = calendar.events.first.summary
    @data[key] = calendar.events.collect do |event|
      if event.description.nil?
        description = ["punch in @ #{event.dtstart}", "punch out @ #{event.dtend}"]
      else
        description = event.description.split("\n")
      end
      {"out" => event.dtend, "in" => event.dtstart, "total" => nil, "log" => description}
    end
  end
  return @data
end

.check_entry(entry) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ical_punch.rb', line 74

def check_entry(entry)
  if entry["in"].nil?
    puts "Error processing start time for #{entry.inspect}"
    return false
  end
  if entry["out"].nil?
     puts "Error processing end time for #{entry.inspect}"
     return false
  end
  return true
end

.dataObject



13
14
15
# File 'lib/ical_punch.rb', line 13

def data
  @data
end

.data=(value) ⇒ Object



17
18
19
# File 'lib/ical_punch.rb', line 17

def data=(value)
  @data = value
end

.from_ical(file_path = "~/punch.ics") ⇒ Object



117
118
119
120
121
# File 'lib/ical_punch.rb', line 117

def from_ical(file_path = "~/punch.ics")
  File.open(File.expand_path(file_path), "r") do |file|
    @calendars = Icalendar.parse(file)
  end
end

.libpath(*args) ⇒ Object

Returns the library path for the module. If any arguments are given, they will be joined to the end of the libray path using File.join.



143
144
145
# File 'lib/ical_punch.rb', line 143

def self.libpath( *args )
  args.empty? ? LIBPATH : ::File.join(LIBPATH, *args)
end

.load(file_path = '~/.punch.yml') ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/ical_punch.rb', line 29

def load(file_path = '~/.punch.yml')
  begin
    raw = File.read(File.expand_path(file_path))
    @data = YAML.load(raw)
  rescue Errno::ENOENT
    return false
  end
  
  true
end

.path(*args) ⇒ Object

Returns the lpath for the module. If any arguments are given, they will be joined to the end of the path using File.join.



151
152
153
# File 'lib/ical_punch.rb', line 151

def self.path( *args )
  args.empty? ? PATH : ::File.join(PATH, *args)
end

.punch_to_calendar(project) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ical_punch.rb', line 54

def punch_to_calendar(project)
  @calendars = []
  value = data[project]
  calendar = Icalendar::Calendar.new
  calendar.prodid += "/#{project}"
  value.each do |entry|
    check_entry(entry) || next
    start_time = entry["in"].strftime("%Y%m%dT%H%M%S")
    end_time   = entry["out"].strftime("%Y%m%dT%H%M%S")
    calendar.event do
      dtstart     start_time
      dtend       end_time
      summary     project
      description entry["log"].join("\n")
    end
  end
  @calendars << calendar
  @calendars
end

.punch_to_calendarsObject



46
47
48
49
50
51
52
# File 'lib/ical_punch.rb', line 46

def punch_to_calendars
  @all_calendars = []
  data.keys.each do |key|
    @all_calendars << punch_to_calendar(key)
  end
  return @all_calendars
end

.require_all_libs_relative_to(fname, dir = nil) ⇒ Object

Utility method used to rquire all files ending in .rb that lie in the directory below this file that has the same name as the filename passed in. Optionally, a specific directory name can be passed in such that the filename does not have to be equivalent to the directory.



160
161
162
163
164
165
166
# File 'lib/ical_punch.rb', line 160

def self.require_all_libs_relative_to( fname, dir = nil )
  dir ||= ::File.basename(fname, '.*')
  search_me = ::File.expand_path(
      ::File.join(::File.dirname(fname), dir, '**', '*.rb'))

  Dir.glob(search_me).sort.each {|rb| require rb}
end

.to_ical(project_name, file_path = "~/punch.ics") ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ical_punch.rb', line 102

def to_ical(project_name, file_path = "~/punch.ics")
  if project_name && project_name.size > 0
    File.open(File.expand_path(file_path), "w") do |file|
      file.write(punch_to_calendar(project_name).to_ical)
    end
  else
    punch_to_calendars.each_with_index do |calendar_data, i|
      file_name = file_path.gsub(".ics", "#{i}.ics")
      File.open(File.expand_path(file_name), "w") do |file|
        file.write(calendar_data.to_ical)
      end
    end        
  end
end

.versionObject

Returns the version string for the library.



135
136
137
# File 'lib/ical_punch.rb', line 135

def self.version
  VERSION
end

.write(file_path = "~/.punch.yml") ⇒ Object



40
41
42
43
44
# File 'lib/ical_punch.rb', line 40

def write(file_path = "~/.punch.yml")
  File.open(File.expand_path(file_path), 'w') do |file|
    file.puts @data.to_yaml
  end
end