From f0b3174e613d7600fa349bccac785878d49b4b4c Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Sat, 18 Oct 2014 02:35:19 -0700 Subject: [PATCH] add HarpBlog#months and expose as GET /months --- server/harp_blog.rb | 7 +++++++ server/server.rb | 10 ++++++++++ server/spec/harp_blog_spec.rb | 16 ++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/server/harp_blog.rb b/server/harp_blog.rb index 3e26054..811d605 100644 --- a/server/harp_blog.rb +++ b/server/harp_blog.rb @@ -110,6 +110,13 @@ class HarpBlog Dir[post_path('20*')].map { |x| File.basename(x) }.sort end + def months + years.map do |year| + # hack: month dirs (and only month dirs) are always 2 characters in length + Dir[post_path(year, '??')].map { |x| [year, File.basename(x)] } + end.flatten(1).sort + end + def posts_for_year(year) posts = [] 1.upto(12) do |n| diff --git a/server/server.rb b/server/server.rb index e15ab73..3275722 100755 --- a/server/server.rb +++ b/server/server.rb @@ -77,6 +77,16 @@ get '/years' do JSON.generate(years: blog.years) end +# list months +get '/months' do + unless authenticated?(request['Auth']) + status 403 + return 'forbidden' + end + + JSON.generate(months: blog.months) +end + # list posts get '/posts/:year/?:month?' do |year, month| unless authenticated?(request['Auth']) diff --git a/server/spec/harp_blog_spec.rb b/server/spec/harp_blog_spec.rb index 6c492e5..887d158 100644 --- a/server/spec/harp_blog_spec.rb +++ b/server/spec/harp_blog_spec.rb @@ -169,6 +169,22 @@ RSpec.describe HarpBlog do end end + describe '#months' do + it "should return all of the years and months with posts" do + months = [ + ["2006", "02"], ["2006", "03"], ["2006", "04"], ["2006", "05"], ["2006", "06"], ["2006", "07"], ["2006", "08"], ["2006", "09"], ["2006", "12"], + ["2007", "03"], ["2007", "04"], ["2007", "05"], ["2007", "06"], ["2007", "07"], ["2007", "08"], ["2007", "09"], ["2007", "10"], + ["2008", "01"], ["2008", "02"], ["2008", "03"], + ["2009", "11"], + ["2010", "01"], ["2010", "11"], + ["2011", "11"], ["2011", "12"], + ["2012", "01"], + ["2013", "03"], ["2013", "09"], + ] + expect(@blog.months.first(months.length)).to eq(months) + end + end + describe '#posts_for_month' do it "should return the correct number of posts" do expect(@blog.posts_for_month('2006', '02').length).to eq(12)