Last active
August 29, 2015 14:20
-
-
Save hails/0b915770cd89c3d0073d to your computer and use it in GitHub Desktop.
API implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'grape' | |
require 'sequel' | |
Encoding.default_external = 'UTF-8' | |
module API | |
class ReadOnly < Grape::API | |
DB = Sequel.connect(eval( | |
File.open("config/db.rb", 'r') { |f| f.read } | |
) | |
) | |
get :board do | |
DB[:boards].all.to_json | |
end | |
resource :board do | |
params do | |
requires :slug, type: String, desc: "Board name" | |
end | |
route_param :slug do | |
get do | |
@board = DB[:boards].where(:slug => params[:slug]).get(:id) | |
DB[:threads].where(:board_id => @board).all | |
end | |
resource :thread do | |
params do | |
requires :id, type: Integer, desc: "Thread id" | |
end | |
route_param :id do | |
get do | |
@thread = DB[:threads].where(:id => params[:id]).get(:id) | |
@board = DB[:boards].where(:slug => params[:slug]).get(:id) | |
DB[:posts].where(:board_id => @board, :thread_id => @thread).all | |
end | |
end | |
end | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require File.expand_path(File.dirname(__FILE__) + '/hive.rb') | |
require File.expand_path(File.dirname(__FILE__) + '/api.rb') | |
Hive::BBS::DB.disconnect | |
run Rack::Cascade.new [API::ReadOnly, Hive::BBS] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gem install grape