55 lines
991 B
Ruby
55 lines
991 B
Ruby
require 'bundler/setup'
|
|
|
|
require 'json'
|
|
require 'faraday'
|
|
require 'sinatra'
|
|
require 'sinatra/namespace'
|
|
require 'thin'
|
|
|
|
require_relative 'get_images_by_category.rb'
|
|
require_relative 'get_random_image.rb'
|
|
|
|
set :environment, :production
|
|
set :server, %w[thin mongrel webrick]
|
|
|
|
categories = File.readlines("categories.txt")
|
|
|
|
# Index
|
|
get '/' do
|
|
image = get_random_image(get_images_by_category(categories))
|
|
|
|
erb :index, locals: { image: image }
|
|
end
|
|
|
|
# About page
|
|
get '/about' do
|
|
erb :about
|
|
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
|