Ruby Sinatra app for downloading a file (as streaming) - ruby

I have a sinatra app, where I want to make a download feature. This download take data from table and make excel to download for user.
require 'csv'
get '/download' do
data = [{:name => "john", :age => 12, :state => 'ca'}, {:name => "tony", :age => 22, :state => 'va'}]
# I want to download this data as excel file and the content of file should be as follows:
# name,age,state
# john,12,ca
# tony,22,va
# I don't want to save data as a temp file on the server and then throw to user for download
# rather I want to stream data for download on the browser. So I used this,
# but it is not working
send_data (data, :type => 'application/csv', :disposition => 'attachment')
end
What am I doing wrong? Or how to achieve, what I am trying to do? I was trying to follow http://sinatra.rubyforge.org/api/classes/Sinatra/Streaming.html
UPDATE:
I am not married to send_data method of sinatra. If streaming blocks my server for that duration, then I am open to alternatives.

get '/download' do
data = [{:name => "john", :age => 12, :state => 'ca'}, {:name => "tony", :age => 22, :state => 'va'}]
content_type 'application/csv'
attachment "myfilename.csv"
data.each{|k, v|
p v
}
end
This works for me. I know it is incomplete as I have to put header and comma in the excel file with line break. But this works.

Related

Parsing remote csv file : No such file or directory # rb_sysopen

I am trying to parse a csv file hosted remotely. I user rails 6 and active storage. The file is stored on the ImportJob model. Its url can be accessed this way :
ImportJob.last.csv_file.url
the file does exist and is downloadable : http://res.cloudinary.com/dockcyr0z/raw/upload/rghn3zi2190nmc28qwbtr24apqxe.csv
However when trying to parse it
CSV.foreach(url, headers: true, header_converters: :symbol, col_sep: ';') do |row|
puts row
end
Im getting Errno::ENOENT: No such file or directory # rb_sysopen - http://res.cloudinary.com/dockcyr0z/raw/upload/rghn3zi2190nmc28qwbtr24apqxe.csv
same thing if I try to open the file first : open(url)
Why am I getting this error ? How can I parse this remote csv file ?
Open url with URI.parse and change CSV.foreach to CSV.parse
CSV.parse(URI.parse(url).read, headers: true, header_converters: :symbol, col_sep: ';') do |row|
puts row
end
# output
{
:first_name => "Souper",
:last_name => "Man",
:email => "dageismar+learner233#gmail.com",
:role => "CEO",
:tags => "sales,marketing",
:avatar_url => "http://res.cloudinary.com/dockcyr0z/image/upload/x3f65o5mepbdhi4fwvww99gjqr7p"
}
{
:first_name => "Gentil",
:last_name => "Keum",
:email => "dageismar+learner234#gmail.com",
:role => "CEO",
:tags => "sales,marketing",
:avatar_url => "http://res.cloudinary.com/dockcyr0z/image/upload/x3f65o5mepbdhi4fwvww99gjqr7p"
}
Update:
Or as Stefan suggests just URI.open(url) instead of URI.parse(url).read

How would I construct a Hash from this scenario in Ruby?

Given I have the following code:
ENDPOINT = 'http://api.eventful.com'
API_KEY = 'PbFVZfjTXJQWrnJp'
def get_xml(url, options={})
compiled_url = "#{ENDPOINT}/rest#{url}" << "?app_key=#{API_KEY}&sort_order=popularity"
options.each { |k, v| compiled_url << "&#{k.to_s}=#{v.to_s}" }
REXML::Document.new((Net::HTTP.get(URI.parse(URI.escape(compiled_url)))))
end
def event_search(location, date)
get_xml('/events/search',
:location => "#{location}, United Kingdom",
:date => date
)
end
And we access the XML data formatted by REXML::Document like this:
events = event_search('London', 'Today').elements
And we can access these elements like this (this prints all the titles in the events):
events.each('search/events/event/title') do |title|
puts title.text
end
The XML I'm using can be found here. I would like this construct a Hash like so:
{"Title1" => {:title => 'Title1', :date => 'Date1', :post_code => 'PostCode1'},
"Title2" => {:title => 'Title2', :date => 'Date2', :post_code => 'PostCode2'}}
When using events.each('search/events/event/title'), events.each('search/events/event/date'), and events.each('search/events/event/post_code').
So I want to create a Hash from the XML provided by the URL I have included above. Thanks!
You should loop over the events themselves, not the titles. Something like this
events_by_title = {}
elements.each('search/events/event') do |event|
title = event.get_elements('title').first.text
events_by_title[title] = {
:title => title,
:date => event.get_elements('start_time').first.text
:post_code => event.get_elements('postal_code').first.text,
}
end
Get the root element using root() on the REXML:Document object then use each_element("search/events/event") to iterate over "event" node. You can then extract the different values out of it using the different methods on element: http://ruby-doc.org/stdlib-1.9.3/libdoc/rexml/rdoc/REXML/Element.html

rake import- only adding one line from csv to database

I am attempting to import a CSV file into my rails database (SQLite in Development) following this tutorial. Data is actually getting inserted into my database but it seems to only insert the first record from the CSV File. the rake seems to run without problem. and a running it with --trace reveals no additional information.
require 'csv'
desc "Import Voters from CSV File"
task :import => [:environment] do
file = "db/GOTV.csv"
CSV.foreach(file, :headers => false) do |row|
Voter.create({
:last_name => row[0],
:first_name => row[1],
:middle_name => row[2],
:name_suffix => row[3],
:primary_address => row[4],
:primary_city => row[5],
:primary_state => row[6],
:primary_zip => row[7],
:primary_zip4 => row[8],
:primary_unit => row[9],
:primary_unit_number => row[10],
:phone_number => row[11],
:phone_code => row[12],
:gender => row[13],
:party_code => row[14],
:voter_score => row[15],
:congressional_district => row[16],
:house_district => row[17],
:senate_district => row[18],
:county_name => row[19],
:voter_key => row[20],
:household_id => row[21],
:client_id => row[22],
:state_voter_id => row[23]
})
end
end
Just ran into this as well - guess you solved it some other way, but still might be useful for others.
In my case, the issue seems to be an incompatible change in the CSV library.
I guess you were using Ruby 1.8, where
CSV.foreach(path, rs = nil, &block)
The docs here are severely lacking, actually no docs at all, so have to guess from source: http://ruby-doc.org/stdlib-1.8.7/libdoc/csv/rdoc/CSV.html#method-c-foreach..
Anyway, 'rs' is clearly not an option hash, it looks like the record separator.
In Ruby 1.9 this is nicer: http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV.html#method-c-foreach
self.foreach(path, options = Hash.new, &block)
so this is the one that supports options such as :headers..

Ruby: Reading Arrays of Hashes from YAML

I have two dads going into my YAML file, but only one family comes out. What happened to Sam? How do I get both out?
## dads.rb
require 'yaml'
require 'pp'
dad=[]
dad[0] = {:name => "Joe", :kids => ["Mary", "John"]}
dad[1] = {:name => "Sam", :kids => ["Sam Jr", "Samantha", "Samizdat"]}
open('dads.yml' , 'w') do |f|
dad.each do |d|
f.write YAML::dump(d)
end
end
family = []
open('dads.yml') do |f|
family << YAML::load(f.read)
end
pp fams
You dump multiple YAML documents but only read back one. Instead, you can just dump and read the whole array:
require 'yaml'
dads = []
dads << {:name => "Joe", :kids => ["Mary", "John"]}
dads << {:name => "Sam", :kids => ["Sam Jr", "Samantha", "Samizdat"]}
open('dads.yml', 'w') { |f| YAML::dump(dads, f) }
family = YAML::load(File.read('dads.yml'))
p family
Your code currently creates separate "documents" within the YAML output. By default, YAML::load will just read in the first document. Niklas' answer is definitely the way you should go, but if you absolutely had to deal with multiple documents, you could use the load_documents method:
family = YAML.load_documents(File.read("dads.yml"))
# => [{:name=>"Joe", :kids=>["Mary", "John"]}, {:name=>"Sam", :kids=>["Sam Jr", "Samantha", "Samizdat"]}]

Multiple Block Parameters with Sinatra

I'm trying to get this Sinatra GET request to work:
get '/:year/:month/:day/:slug' do
end
I know you can get one param to work with block parameters:
get '/:param' do |param|
"Here it is: #{param}."
end
But how can I use multiple block parameters with the first code block? I'm open to other methods.
Multiple placeholders are stored in params as Hash.
# Request to /2009/10/20/post.html
get '/:year/:month/:day/:slug' do
params[:year] # => 2009
params[:month] # => 10
params[:day] # => 20
params[:post] # => post.html
end
Forgive my ignorance of Sinatra, but shouldn't this set named parameters like Rails map.connect?:
get '/:year/:month/:day/:slug
Now the parameters should be accessible in the params hash:
params = { :year => "foo", :month => "bar", :day => "baz", :slug => "etc" }

Resources