Skip to content

Instantly share code, notes, and snippets.

@croaky
Last active December 3, 2022 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save croaky/1892e3ee20b38bd0edce74b92a51c67d to your computer and use it in GitHub Desktop.
Save croaky/1892e3ee20b38bd0edce74b92a51c67d to your computer and use it in GitHub Desktop.
"Heroku Dataclips" pair programmed with https://chat.openai.com/chat
#!/usr/bin/env ruby
# createdb db
# chmod +x dataclips.rb
# DATABASE_URL=postgres:///db ./dataclips.rb
require "bundler/inline"
require "csv"
gemfile do
source "https://rubygems.org"
gem "haml"
gem "pg"
gem "puma"
gem "sinatra"
end
enable :inline_templates
db = PG.connect(ENV.fetch("FOLLOWER_DATABASE_URL"))
get "/" do
haml :index
end
post "/run" do
@rows = db.exec(params["sql"] || "SELECT 1")
haml :index
end
post "/export" do
headers "Content-Type" => "text/csv"
headers "Content-Disposition" => "attachment; filename=dataclip.csv"
stream = StringIO.new
db.copy_data("COPY (#{params["sql"] || "SELECT 1"}) TO STDOUT DELIMITER ',' CSV HEADER FORCE QUOTE *") do
while (row = db.get_copy_data)
stream.write(row)
end
end
stream.string
end
Sinatra::Application.run!
__END__
@@ index
%form(method="POST")
%textarea{name: "sql", rows: 10, cols: 80}= params["sql"]
%br
%button{type: "submit", formaction: "/run"} Run
%button{type: "submit", formaction: "/export"} Export
- if @rows
%table
%thead
%tr
- @rows.fields.each do |field|
%th= field
%tbody
- @rows.each do |row|
%tr
- row.each do |key, value|
%td= value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment