Skip to content

Instantly share code, notes, and snippets.

@hails
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save hails/0b915770cd89c3d0073d to your computer and use it in GitHub Desktop.

Select an option

Save hails/0b915770cd89c3d0073d to your computer and use it in GitHub Desktop.
API implementation
#!/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
#!/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]
@hails

hails commented May 1, 2015

Copy link
Copy Markdown
Author

gem install grape

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment