2009/12/28

url for blogger-api

Blogger API 用
日付範囲を指定したURLの生成

line 10:
t に2010/12 が指定された場合、翌月は年も変わるので年に1加算し月にも1加算すると2011/1/1 が得られそこから1秒ひくと2010/12/31の23:59:59
になる。

Time だけ使った場合

  1 require 'time'
  2 
  3 def range_current_month(t=nil, bid)
  4   u = "http://www.blogger.com/feeds/#{bid}/posts/default"
  5   begin
  6     t = Time.parse(t) unless t.nil?
  7   rescue
  8     t = Time.now()
  9   end
 10   t.month == 12 ? x = [t.year+1, 1] : x = [t.year, t.month+1]
 11   min = t.strftime("%Y-%m-%dT00:00:00")
 12   max = (Time.local(x[0], x[1], 1) -1).strftime("%Y-%m-%dT%H:%M:%S")
 13   return u + "?published-min=#{min}" + "&published-max=#{max}"
 14 end
 15 
 16 bloggerid = 'xxxxxxxxxxxx'
 17 t = '2010/12'
 18 p range_current_month(t, bloggerid)
 19 t = '2010/2'
 20 p range_current_month(t, bloggerid)

=>
"http://www.blogger.com/feeds/xxxxxxxxxxxx/posts/default?published-min=2010-12-01T00:00:00&published-max=2010-12-31T23:59:59"
"http://www.blogger.com/feeds/xxxxxxxxxxxx/posts/default?published-min=2010-02-01T00:00:00&published-max=2010-02-28T23:59:59"


Date も使った場合

  1 require 'time'
  2 require 'date'
  3 
  4 def range_current_month(t=nil, bid)
  5   u = "http://www.blogger.com/feeds/#{bid}/posts/default"
  6   begin
  7     t = Time.parse(t) unless t.nil?
  8   rescue
  9     t = Time.now()
 10   end
 11   t.month == 12 ? x = [t.year+1, 1] : x = [t.year, t.month+1]
 12   min = t.strftime("%Y-%m-%dT%H:%M:%S")
 13   max = ((Date.new(x[0], x[1], 1)) - 1).strftime("%Y-%m-%dT23:59:59")
 14   return u + "?published-min=#{min}" + "&published-max=#{max}"
 15 end
 16 
 17 bloggerid = 'xxxxxxxxxxxx'
 18 t = '2010/12'
 19 p range_current_month(t, bloggerid)
 20 t = '2010/2'
 21 p range_current_month(t, bloggerid)
 22 

=>
"http://www.blogger.com/feeds/xxxxxxxxxxxx/posts/default?published-min=2010-12-01T00:00:00&published-max=2010-12-31T23:59:59"
"http://www.blogger.com/feeds/xxxxxxxxxxxx/posts/default?published-min=2010-02-01T00:00:00&published-max=2010-02-28T23:59:59"


もう少し短くできないかなと思うんだけど...。 ?

0 件のコメント: