mirror of
https://github.com/samsonjs/samhuri.net.git
synced 2026-04-27 14:57:40 +00:00
add HarpBlog#months and expose as GET /months
This commit is contained in:
parent
291e40f859
commit
f0b3174e61
3 changed files with 33 additions and 0 deletions
|
|
@ -110,6 +110,13 @@ class HarpBlog
|
||||||
Dir[post_path('20*')].map { |x| File.basename(x) }.sort
|
Dir[post_path('20*')].map { |x| File.basename(x) }.sort
|
||||||
end
|
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)
|
def posts_for_year(year)
|
||||||
posts = []
|
posts = []
|
||||||
1.upto(12) do |n|
|
1.upto(12) do |n|
|
||||||
|
|
|
||||||
|
|
@ -77,6 +77,16 @@ get '/years' do
|
||||||
JSON.generate(years: blog.years)
|
JSON.generate(years: blog.years)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# list months
|
||||||
|
get '/months' do
|
||||||
|
unless authenticated?(request['Auth'])
|
||||||
|
status 403
|
||||||
|
return 'forbidden'
|
||||||
|
end
|
||||||
|
|
||||||
|
JSON.generate(months: blog.months)
|
||||||
|
end
|
||||||
|
|
||||||
# list posts
|
# list posts
|
||||||
get '/posts/:year/?:month?' do |year, month|
|
get '/posts/:year/?:month?' do |year, month|
|
||||||
unless authenticated?(request['Auth'])
|
unless authenticated?(request['Auth'])
|
||||||
|
|
|
||||||
|
|
@ -169,6 +169,22 @@ RSpec.describe HarpBlog do
|
||||||
end
|
end
|
||||||
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
|
describe '#posts_for_month' do
|
||||||
it "should return the correct number of posts" do
|
it "should return the correct number of posts" do
|
||||||
expect(@blog.posts_for_month('2006', '02').length).to eq(12)
|
expect(@blog.posts_for_month('2006', '02').length).to eq(12)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue