Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
This gem converts data from a variety of sources into a comma-separated value (CSV) format. The gem can convert arrays of JSON objects, arrays of hashes, arrays of ActionController params, simple hashes, or ActiveRecord relations and objects. The CSV format is a common way to store data in a tabular format. It is often used for data exchange between different applications.
Add the following code to you Gemfile
gem 'json_or_ruby_to_csv'
or
install the gem on your terminal
gem install json_or_ruby_to_csv
require
this gem on the top of your ruby code.
If you want to convert arrays of JSON objects
, arrays of hashes
, arrays of ActionController params
, and simple hashes
to csv you can use array_or_hash_to_csv
method from JsonOrRubyToCsv
. Let's see how:-
Don't forget to include
JsonOrRubyToCsv
module in your ruby class.
For example
require 'json_or_ruby_to_csv'
class Employee
include JsonOrRubyToCsv
def get_csv_data(data_list)
array_or_hash_to_csv(data_list)
end
end
Let's create Employee
object and send array of object to get_csv_data
method.
employee = Employee.new
data_list = [
{ "id": 200,
"fname": "Meshu",
"lname": "Amare",
"salary": 4500,
"company": "XYZ.com",
"position": "DevOps IT Operations"
},
{ "id": 201,
"fname": "Solomon",
"lname": "Alebachew",
"salary": 5000,
"company": "XYZ.com",
"position": "Rails Developer"
},
{
"id": 202,
"fname": "Bini",
"lname": "Ew",
"salary": 5000,
"company": "XYZ.com",
"position": "Senior Sofware Developer"
}
]
print employee.get_csv_data(data_list)
The output looks like below
id,fname,lname,company,position,salary
200,Meshu,Amare,XYZ.com,DevOps IT Operations,4500
201,Solomon,Alebachew,XYZ.com,Rails Developer,5000
203,Bini,Ew,XYZ.com,Senior Sofware Developer,5000
If you want to conver ActiveRecord::Relation
(collection of objects fetched by .all
or .where()
) or simple ActiveRecord object to csv formated-string you can use activerecord_to_csv
method from JsonOrRubyToCsv
module.
For example
require 'json_or_ruby_to_csv'
class Employee
include JsonOrRubyToCsv
def get_data_as_csv
all_employee = Employee.select(:id, :fname, :lname, :salary, :company, :position)
activerecord_to_csv(all_employee)
end
end
Here get_data_as_csv
method first retrive data from the database, then pass to activerecord_to_csv
method and return the data as comma-separated csv formated string.
Let's create Employee
object and call get_data_as_csv
method.
employee = Employee.new
employee.get_data_as_csv
The output looks like below
id,fname,lname,company,position,salary
200,Meshu,Amare,XYZ.com,DevOps IT Operations,4500
201,Solomon,Alebachew,XYZ.com,Rails Developer,5000
203,Bini,Ew,XYZ.com,Senior Sofware Developer,5000
You can include the functionality of this gem in your controller class.
for example
If you want your controller action to return a CSV formated data after fetching data from the database, you can do it as follow.
require 'json_or_ruby_to_csv'
class ConvertsController < ApplicationController
include JsonOrRubyToCsv
def index
input = params_value[:data]
result = array_or_hash_to_csv(input)
render plain: result, status: :ok
end
def previous
all_employee = Employee.select(:id, :fname, :lname, :salary, :company, :position)
result = activerecord_to_csv(all_employee)
render plain: result, status: :ok
end
private
def params_value
params.require(:data) # there must be data
params.permit(data: %i[id fname lname company position salary]) # list of optional attributes
end
end
index
action controller method will convert array of params data to csv formated string where as previous
will return csv formated string after fetching data from database.
👤 Melashu Amare
This repo is open for contributions. Issues, and feature requests are welcome!
Feel free to check the issues page.
Give a star if you like this project!
FAQs
Unknown package
We found that json_or_ruby_to_csv demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.