Permalink
Browse files

module Caline provided

  • Loading branch information...
melborne committed Dec 9, 2010
1 parent c00fd23 commit 817e948a550826f6eca2e1780766ddcaf5f833cb
Showing with 152 additions and 124 deletions.
  1. +55 −50 lib/gcalendar.rb
  2. +79 −65 lib/month.rb
  3. +2 −2 test/tc_gcalendar.rb
  4. +16 −7 test/tc_month.rb
View
@@ -4,65 +4,70 @@
require "open-uri"
require "nokogiri"
require "cgi"
+require "date"
-class GCalendar
- def initialize(year, month=nil)
- @year, @month = year, month
- end
+module Caline
+ class GCalendar
+ def initialize(year, month=nil)
+ @year, @month = year, month
+ end
- def holidays(country=:ja_ja)
- parse_xml_to_hash get_holidays(country)
- end
+ def holidays(country=:ja_ja)
+ parse_xml_to_hash get_holidays(country)
+ end
- private
- def get_holidays(country)
- open URL(country) + PARAM(*build_date_range)
- rescue => e
- STDERR.puts "Failed to retrieve Holiday data by Google Calendar Data API. #{e}"
- end
+ private
+ def get_holidays(country)
+ open URL(country) + PARAM(*build_date_range)
+ rescue => e
+ STDERR.puts "Failed to retrieve Holiday data by Google Calendar Data API. #{e}"
+ end
- def URL(country)
- "http://www.google.com/calendar/feeds/" +
- CGI.escape("#{COUNTRY_ID(country)}") + "/public/full-noattendees?"
- end
+ def URL(country)
+ "http://www.google.com/calendar/feeds/" +
+ CGI.escape("#{COUNTRY_ID(country)}") + "/public/full-noattendees?"
+ end
- def COUNTRY_ID(country)
- base1 = "@holiday.calendar.google.com"
- base2 = "#holiday@group.v.calendar.google.com"
- {
- ja: "japanese#{base1}",
- us: "usa__en#{base1}",
- au: "australian__en#{base1}",
- ja_ja: "ja.japanese#{base2}",
- cn: "en.china#{base2}",
- fr: "en.french#{base2}",
- de: "en.german#{base2}",
- it: "en.italian#{base2}",
- kr: "en.south_korea#{base2}",
- tw: "en.taiwan#{base2}",
- gb: "en.uk#{base2}"
- }[country]
- end
+ def COUNTRY_ID(country)
+ base1 = "@holiday.calendar.google.com"
+ base2 = "#holiday@group.v.calendar.google.com"
+ {
+ ja: "japanese#{base1}",
+ us: "usa__en#{base1}",
+ au: "australian__en#{base1}",
+ ja_ja: "ja.japanese#{base2}",
+ cn: "en.china#{base2}",
+ fr: "en.french#{base2}",
+ de: "en.german#{base2}",
+ it: "en.italian#{base2}",
+ kr: "en.south_korea#{base2}",
+ tw: "en.taiwan#{base2}",
+ gb: "en.uk#{base2}"
+ }[country]
+ end
- def PARAM(sd, ed)
- "start-min=#{sd}&start-max=#{ed}"
- end
+ def PARAM(sd, ed)
+ "start-min=#{sd}&start-max=#{ed}"
+ end
- def build_date_range
- st, ed = @month ? [format("%02d", @month)] * 2 : ["01", "12"]
- return "#{@year}-#{st}-01", "#{@year}-#{ed}-31"
- end
+ def build_date_range
+ st, ed = @month ? [format("%02d", @month)] * 2 : ["01", "12"]
+ return "#{@year}-#{st}-01", "#{@year}-#{ed}-31"
+ end
- def parse_xml_to_hash(xml)
- data = Nokogiri::HTML(xml)
- h = {}
- data.css("entry").each do |node|
- date = node.css("gd", "when").attr("starttime").value
- h[Time.parse date] = node.css("title").text
+ def parse_xml_to_hash(xml)
+ data = Nokogiri::HTML(xml)
+ h = {}
+ data.css("entry").each do |node|
+ date = node.css("gd", "when").attr("starttime").value
+ h[Date.parse date] = node.css("title").text
+ end
+ h
end
- h
end
end
-g = GCalendar.new(2010, 9)
-g.holidays
+if __FILE__ == $0
+ g = Caline::GCalendar.new(2010, 9)
+ p g.holidays
+end
View
@@ -2,86 +2,100 @@
#-*-encoding: utf-8-*-
require "date"
require "term/ansicolor"
+require_relative "gcalendar"
-String.send(:include, Term::ANSIColor)
+module Caline
+ String.send(:include, Term::ANSIColor)
-class Month
- attr_reader :year, :mon, :last, :colors
- def initialize(year, mon, colors={})
- @year = year || Date.today.year
- @mon = mon || Date.today.mon
- @first = Date.new(@year, @mon, 1)
- @last = last_date(@year, @mon)
- @colors = { title: [:green,:yellow], today: :green,
- saturday: :cyan, sunday: :magenta, holiday: :red }
- @colors.update(colors)
- end
+ class Month
+ attr_reader :year, :month, :last, :colors
+ def initialize(year, month, colors={})
+ @year = year || Date.today.year
+ @month = month || Date.today.mon
+ @first = Date.new(@year, @month, 1)
+ @last = last_date(@year, @month)
+ @colors = { title: [:green,:yellow], today: :green,
+ saturday: :cyan, sunday: :magenta, holiday: :red }
+ @colors.update(colors)
+ @holidays = nil
+ end
- def dates
- (@first..@last).to_a
- end
+ def dates
+ (@first..@last).to_a
+ end
- def dates_by_week(from=0)
- pre = (1..(@first.wday-from)%7).map { |i| @first - i }.reverse
- pos = (1..(6-@last.wday+from)%7).map { |i| @last + i }
- (pre + dates + pos).each_slice(7).to_a
- end
+ def dates_by_week(from=0)
+ pre = (1..(@first.wday-from)%7).map { |i| @first - i }.reverse
+ pos = (1..(6-@last.wday+from)%7).map { |i| @last + i }
+ (pre + dates + pos).each_slice(7).to_a
+ end
- alias formaty format
- def format(style=:week, from=0, form=mono_proc)
- case style
- when :week
- dates_by_week(from).map { |w| form[w] }.join("\n")
- when :month
- form[dates]
- else raise ArgumentError
+ alias formaty format
+ def format(style=:week, from=0, form=mono_proc)
+ case style
+ when :week
+ dates_by_week(from).map { |w| form[w] }.join("\n")
+ when :month
+ form[dates]
+ else raise ArgumentError
+ end
end
- end
- def color_format(style=:week, from=0)
- format(style, from, color_proc)
- end
+ def color_format(style=:week, from=0)
+ format(style, from, color_proc)
+ end
- def colors=(colors)
- @colors.update(colors)
- end
+ def colors=(colors)
+ @colors.update(colors)
+ end
- private
- def last_date(year, mon, day=[28, 29, 30, 31])
- Date.new(year, mon, day.pop) rescue retry
- end
+ def holidays(code=:ja_ja)
+ @holidays = GCalendar.new(@year, @month).holidays(code)
+ end
- def mono_proc
- ->w{ w.map{ |d| formaty "%2d", d.day }.join(" ") }
- end
+ private
+ def last_date(year, month, day=[28, 29, 30, 31])
+ Date.new(year, month, day.pop) rescue retry
+ end
+
+ def mono_proc
+ ->w{ w.map{ |d| formaty "%2d", d.day }.join(" ") }
+ end
- def color_proc
- ->w{
- w.map do |d|
- f = "%2d"
- f =
- case d
- when sunday? then f.send(@colors[:sunday])
- when saturday? then f.send(@colors[:saturday])
- else f
- end
- f = f.send(@colors[:today]) if today?[d]
+ def color_proc
+ ->w{
+ w.map do |d|
+ f = "%2d"
+ f =
+ case d
+ when holiday? then f.send(@colors[:holiday])
+ when sunday? then f.send(@colors[:sunday])
+ when saturday? then f.send(@colors[:saturday])
+ else f
+ end
+ f = f.send(@colors[:today]) if today?[d]
- formaty f, d.day
- end.join(" ")
- }
- end
+ formaty f, d.day
+ end.join(" ")
+ }
+ end
- def sunday?
- ->d{ d.wday == 0 }
- end
+ def sunday?
+ ->d{ d.wday == 0 }
+ end
- def saturday?
- ->d{ d.wday == 6 }
- end
+ def saturday?
+ ->d{ d.wday == 6 }
+ end
- def today?
- ->d{ d == Date.today }
+ def today?
+ ->d{ d == Date.today }
+ end
+
+ def holiday?
+ return nil unless @holidays
+ ->d{ @holidays.has_key?(d) }
+ end
end
end
View
@@ -4,12 +4,12 @@
class TestLibGcalendar < Test::Unit::TestCase
def test_holidays_for_a_month
- gc = GCalendar.new(2010, 12)
+ gc = Caline::GCalendar.new(2010, 12)
assert_equal(23, gc.holidays.keys[0].day)
end
def test_holidays_for_a_year
- gc = GCalendar.new(2010)
+ gc = Caline::GCalendar.new(2010)
puts holidays_ja = gc.holidays.map { |k, v| "#{k.strftime("%m/%d")} => #{v}" }
puts holidays_us = gc.holidays(:us).map { |k, v| "#{k.strftime("%m/%d")} => #{v}" }
end
View
@@ -4,21 +4,21 @@
class TestMonth < Test::Unit::TestCase
def setup
- @m = Month.new(2010, 12)
+ @m = Caline::Month.new(2010, 12)
@lasts2010 = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
end
def test_last_day
- (1..12).each { |m| assert_equal(@lasts2010[m], Month.new(2010, m).last.day) }
+ (1..12).each { |m| assert_equal(@lasts2010[m], Caline::Month.new(2010, m).last.day) }
end
def test_year_month
assert_equal(2010, @m.year)
- assert_equal(12, @m.mon)
+ assert_equal(12, @m.month)
end
def test_dates
- assert_equal((1..28).to_a, Month.new(2011, 2).dates.map(&:day))
+ assert_equal((1..28).to_a, Caline::Month.new(2011, 2).dates.map(&:day))
assert_equal((1..31).to_a, @m.dates.map(&:day))
end
@@ -49,18 +49,27 @@ def test_format
puts
puts @m.format(:month)
end
-
+
def test_color_format
puts @m.color_format(:week, 0)
puts @m.color_format(:week, 1)
puts @m.color_format(:month, 1)
end
-
+
def test_colors
- m = Month.new(2011, 2, sunday: :yellow, saturday: :green)
+ m = Caline::Month.new(2011, 2, sunday: :yellow, saturday: :green)
puts
puts m.color_format(:week, 0)
m.colors = {sunday: :red, saturday: :blue}
puts m.color_format(:month, 1)
end
+
+ def test_holidays
+ months = (1..12).map { |m| Caline::Month.new(2011, m) }
+ months.each do |mon|
+ mon.holidays
+ puts
+ puts mon.color_format
+ end
+ end
end

0 comments on commit 817e948

Please sign in to comment.