Skip to content

Instantly share code, notes, and snippets.

@szabcsee
Last active December 1, 2020 00:10
Show Gist options
  • Select an option

  • Save szabcsee/8523329 to your computer and use it in GitHub Desktop.

Select an option

Save szabcsee/8523329 to your computer and use it in GitHub Desktop.

Revisions

  1. szabcsee revised this gist Jan 29, 2014. 2 changed files with 37 additions and 19 deletions.
    37 changes: 37 additions & 0 deletions data2csv-data2json-data2arr.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    require 'sinatra'
    require 'json'
    require 'csv'

    # Serve data as JSON
    get '/hi/:name' do
    name = params[:name]
    content_type :json
    { :message => name }.to_json
    end

    # Serve data as CSV file
    get '/csv' do
    content_type 'application/csv'
    attachment "myfilename.csv"
    csv_string = CSV.generate do |csv|
    csv << ["row", "of", "CSV", "data"]
    csv << ["another", "row"]
    # ...
    end
    end

    # Upload form
    get '/upload' do
    erb :upload
    end

    # Handle POST-request (Receive and save the uploaded file and line by line load its content to an array.)
    post '/upload' do
    content = params['myfile'][:tempfile].read
    content_arr = []
    content.each_line do |line|
    content_arr << line
    end
    puts content_arr
    return "The operation has been successful! The uploaded csv content is in an array."
    end
    19 changes: 0 additions & 19 deletions data2csv-data2json.rb
    Original file line number Diff line number Diff line change
    @@ -1,19 +0,0 @@
    require 'sinatra'
    require 'json'
    require 'csv'

    get '/hi/:name' do
    name = params[:name]
    content_type :json
    { :message => name }.to_json
    end

    get '/csv' do
    content_type 'application/csv'
    attachment "myfilename.csv"
    csv_string = CSV.generate do |csv|
    csv << ["row", "of", "CSV", "data"]
    csv << ["another", "row"]
    # ...
    end
    end
  2. szabcsee renamed this gist Jan 20, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. szabcsee renamed this gist Jan 20, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. szabcsee renamed this gist Jan 20, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. szabcsee created this gist Jan 20, 2014.
    19 changes: 19 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    require 'sinatra'
    require 'json'
    require 'csv'

    get '/hi/:name' do
    name = params[:name]
    content_type :json
    { :message => name }.to_json
    end

    get '/csv' do
    content_type 'application/csv'
    attachment "myfilename.csv"
    csv_string = CSV.generate do |csv|
    csv << ["row", "of", "CSV", "data"]
    csv << ["another", "row"]
    # ...
    end
    end