predator_pics/server.rb

52 lines
951 B
Ruby
Raw Normal View History

2024-11-10 13:12:42 +00:00
require 'json'
require 'faraday'
require 'sinatra'
require 'sinatra/namespace'
2024-11-10 13:12:42 +00:00
require_relative 'get_images_by_category.rb'
require_relative 'get_random_image.rb'
set :environment, :production
set :server, %w[thin mongrel webrick]
2024-11-10 13:12:42 +00:00
categories = File.readlines("categories.txt")
# Index
2024-11-10 13:12:42 +00:00
get '/' do
image = get_random_image(get_images_by_category(categories))
erb :index, locals: { image: image }
end
# About page
2024-11-10 13:12:42 +00:00
get '/about' do
erb :about
2024-11-10 17:13:42 +00:00
end
get '/about-api' do
erb :api
end
# API
namespace "/api" do
before do
content_type 'application/json'
end
get '/random' do
image = get_random_image(get_images_by_category(categories))
image.to_json
end
get '/categories' do
content_type :json
categories_trimmed = []
categories.each do |category|
categories_trimmed << category.delete("\n")
end
categories_trimmed.to_json
end
end