add HarpBlog#months and expose as GET /months

This commit is contained in:
Sami Samhuri 2014-10-18 02:35:19 -07:00
parent 291e40f859
commit f0b3174e61
3 changed files with 33 additions and 0 deletions

View file

@ -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|

View file

@ -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'])

View file

@ -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)