Rails 4: CSV to hash passed as hidden field - ruby

I have a CSV import method that renders a confirmation / preview page of the data about to be imported, and I want to pass the data from the preview to the actual import method.
In the preview, the CSV has already been turned into a hash of rows and I want to pass that hash to the import method. I've tried simply doing:
<%= hidden_field_tag "my_hash", #final %>
where #final is the hash of data, but it passes the hash as a string and in the params, the data looks like json.
"wi_hash"=>"{
\"name_fail\"=>[{\"scale_id\"=>\"509\",
\"name\"=>\"John Doe\",
\"date\"=>\"<no data>\",
\"current_weight\"=>\"999\",
\"bmi\"=>\"999\",
\"body_fat\"=>\"999\",
\"visceral_fat\"=>\"999\",
\"tbw\"=>\"999\",
\"muscle_mass\"=>\"999\",
\"basal_metabolic_rate\"=>\"999\"
....
}
How else can I pass #final so that it maintains its hash format?

I found this useful helper in another question:
def hash_to_hidden_fields(hash)
query_string = Rack::Utils.build_nested_query(hash)
pairs = query_string.split(Rack::Utils::DEFAULT_SEP)
tags = pairs.map do |pair|
key, value = pair.split('=', 2).map { |str| Rack::Utils.unescape(str) }
hidden_field_tag(key, value)
end
tags.join("\n").html_safe
end
It allows you to pass the haas as an argument.

Related

How to Export a Ruby Array into a single csv row?

I'm looking to pass an array into a csv as a single row. The code below showcases what I'm currently trying. The array is variable in length and will showcase something like this.
a = ["website url", "page link", "page link", "page link"]
The code essentially goes to a page using open-uri and scrapes out the links of each page. This is used for internal validation and checked against what we are expecting. If the link matches what we expect it is reported into the csv as a page link variable shown above.
I've currently tried using to_csv to format the data but inside the csv everything is in a single row including my comma separations.
def dwrite (array)
CSV.open("filename.csv", "ab") do |csv|
data = array.to_csv(row_sep: nil)
csv << [data]
end
end
This is an example of what the csv looks like in a single column:
https://www.url.com/example-page.html
#,
#,,
#,,,
#,,,,
#,,,,,
#,,,,,,
#,,,,,,,
#,,,,,,,,
#,,,,,,,,,
#,,,,,,,,,,
#,,,,,,,,,,,
#,,,,,,,,,,,,
#,,,,,,,,,,,,,
#,,,,,,,,,,,,,,
#,,,,,,,,,,,,,,,
https://anotherexamplepage.html
I was hoping that each array element passed into the method would showcase on the same line but this doesn't appear to be the case. Any help would be much appreciated.
You don't need to transform the array in any way. Just add it to the CSV:
def dwrite(array)
CSV.open("filename.csv", "ab") do |csv|
csv << array
end
end

Access csv data from htttp request in ruby

I'm trying to access the csv data, which I recive if I make a http-request.
I don't save it to a csv file, so I save it to the variable.
Let's say this is the response I get, how can I print food?
uuid,event_id,category
12,1,food
13,2,cars
And this is the part of the ruby code which is important.
That's something I found, but it was originally used with a file, so it doesn't work.
csvdata = request(action,parameter)
#data_hash = {}
CSV.foreach(csvdata) do |row|
uuid, event_id, category = row
#data_hash[uuid] = event_id
end
Do I really need files for that or is there a easy way I can access the values?
Update
CSV.parse(csvdata,data = Hash.new) do |row|
puts data
end
The hash should look like this so I can use the column names
{"uuid" => "12,13", "event_id" => "323,3243", "category" => "food,cars"}
csv_data = Hash.new{|k, v| k[v] = []}
CSV.parse(csv_string, headers: true) do |row|
row.each{|k, v| csv_data[k] << v}
end
csv_data = Hash[csv_data.map{|k, v| [k, v.join(",")]}]
Update after specification Requested output.
Try this:
csvdata = request(action,parameter)
#data_hash = {}
CSV.parse(csvdata, headers: true) do |row|
#data_hash[row['uuid']] = row['event_id']
end
#data_hash
# => {"12"=>"1", "13"=>"2"}
When you parse a CSV, the seconds parameter (data = Hash.new in your code) is actually an options parameter. You can see the available options here:
:headers
If set to :first_row or true, the initial row of the CSV file will be treated as a row of headers. If set to an Array, the contents will be used as the headers. If set to a String, the String is run through a call of ::parse_line with the same :col_sep, :row_sep, and :quote_char as this instance to produce an Array of headers. This setting causes #shift to return rows as CSV::Row objects instead of Arrays and #read to return CSV::Table objects instead of an Array of Arrays.
When passing headers: true - values are parsed into a Row object, where they can be accessed by name.

How to check if value exists in params.permit?

I can't seem to figure out how to accomplish what I am trying to do here on my create method.
What I have right now works if there are no values, the item is deleted. However, if 1 or more param values exist, it passes and is saved. Not what I needed. I need an all or nothing scenario. I want to save only if all the permitted keys have their value. params.permit(:name, :description, :copyright)
Before an entry is saved using organizations.save!, I need to make sure none of the params that are permitted are nil or empty.
I search all over and can't seem to narrow down on an answer to my exact issue.
Here is my code:
class OrganizationsController < ApplicationController
def index
query_params = params.permit(:id, :name,)
if query_params.blank?
organizations = Organization.all
else
organizations = Organization.where(query_params)
end
render json: organizations, root: "organizations"
end
def create
organizations = Organization.new(organization_params)
if organization_params.present?
organizations.delete
else
organizations.save!
render json: organizations
end
end
private
def organization_params
params.permit(:name, :description, :copyright)
end
end
You should add validations to your model.
From your question i understand that you want to save details only if you get values in all the field, if not you don't want to save, right?. If yes, then adding validations to your model will give you what you wanted.
Add the following to your organization model
validates_presence_of :name
validates_presence_of :description
validates_presence_of :copyright
by doing so, the user won't be allowed to save the details unless and until all three fields have some value in it.
There is no need to use delete as the incomplete information will not be saved.
for more and advanced info click here
To check none of the values of organization_params hash is empty, you can do something like this:
organization_params.values.all? { |x| !x.empty? }
or, this:
organization_params.all? { |k,v| !v.empty? }
You can also check if any param value is empty:
organization_params.any? { |k,v| v.empty? }
So, your create method can be re-written as:
def create
organizations = Organization.new(organization_params)
if organization_params.any? { |k,v| v.empty? }
# at least one param is empty, so delete the record
organizations.delete
else
# all the params values are present, so save the record
organizations.save!
render json: organizations
end
end

How could I add or append to my cucumber Table?

In cucumber, one of the best feature is the Table data passing. However if I want to add additional data to it, or create a Table data in my step_definitions, how could I do that? What type is Table (hash? map? list? array?)?
To illustrate, below is one of my step, accepting a table data from the feature, and pass along to a function. I like to append some data to it. How could I do that?
Then(/^posted JSON should have the below attributes$/) do |table|
## Here I want to append some data to my table. How to do it?
posted_json_attribute_table_check table
end
Then I have a function that use it to compare with a read JSON.
def posted_json_attribute_table_check(table)
json = JSON.parse $post_result.lines.first
data = table.raw
data.each do |entry|
status = entry[0]
value = entry[1]
expect(json[status].to_s).to eq(value)
end
end
Thanks!
The table object is of type Cucumber::Core::Ast::DataTable and can be found here. https://github.com/cucumber/cucumber-ruby-core/blob/master/lib/cucumber/core/ast/data_table.rb
# Creates a new instance. +raw+ should be an Array of Array of String
# or an Array of Hash
# You don't typically create your own DataTable objects - Cucumber will do
# it internally and pass them to your Step Definitions.
#
def initialize(raw, location)
raw = ensure_array_of_array(rubify(raw))
verify_rows_are_same_length(raw)
#raw = raw.freeze
#location = location
end

Ruby: how can use the dump method to output data to a csv file?

I try to use the ruby standard csv lib to dump out the arr of object to a csv.file , called 'a.csv'
http://ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html#method-c-dump
dump(ary_of_objs, io = "", options = Hash.new)
but in this method, how can i dump into a file?
there is no such examples exists and help. I google it no example to do for me...
Also, the docs said that...
The next method you can provide is an instance method called
csv_headers(). This method is expected to return the second line of
the document (again as an Array), which is to be used to give each
column a header. By default, ::load will set an instance variable if
the field header starts with an # character or call send() passing the
header as the method name and the field value as an argument. This
method is only called on the first object of the Array.
Anyone knows how to pass the instance method csv_headers() to this dump function?
I haven't tested this out yet, but it looks like io should be set to a file. According to the doc you linked "The io parameter can be used to serialize to a File"
Something like:
f = File.open("filename")
dump(ary_of_objs, io = f, options = Hash.new)
The accepted answer doesn't really answer the question so I thought I'd give a useful example.
First of all if you look at the docs at http://ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html, if you hover over the method name for dump you see you can click to show source. If you do that you'll see that the dump method attempts to call csv_headers on the first object you pass in from ary_of_objs:
obj_template = ary_of_objs.first
...snip...
headers = obj_template.csv_headers
Then later you see that the method will call csv_dump on each object in ary_of_objs and pass in the headers:
ary_of_objs.each do |obj|
begin
csv << obj.csv_dump(headers)
rescue NoMethodError
csv << headers.map do |var|
if var[0] == #
obj.instance_variable_get(var)
else
obj[var[0..-2]]
end
end
end
end
So we need to augment each entry in array_of_objs to respond to those two methods. Here's an example wrapper class that would take a Hash, and return the hash keys as the CSV headers and then be able to dump each row based on the headers.
class CsvRowDump
def initialize(row_hash)
#row = row_hash
end
def csv_headers
#row.keys
end
def csv_dump(headers)
headers.map { |h| #row[h] }
end
end
There's one more catch though. This dump method wants to write an extra line at the top of the CSV file before the headers, and there's no way to skip that if you call this method due to this code at the top:
# write meta information
begin
csv << obj_template.class.csv_meta
rescue NoMethodError
csv << [:class, obj_template.class]
end
Even if you return '' from CsvRowDump.csv_meta that will still be a blank line where a parse expects the headers. So instead lets let dump write that line and then remove it afterwards when we call dump. This example assumes you have an array of hashes that all have the same keys (which will be the CSV header).
#rows = #hashes.map { |h| CsvRowDump.new(h) }
File.open(#filename, "wb") do |f|
str = CSV::dump(#rows)
f.write(str.split(/\n/)[1..-1].join("\n"))
end

Resources