https://github.com/melashu/json_or_ruby_to_csv
This gem converts arrays of JSON objects, arrays of hashes, arrays of ActionController params, simple hashes, or ActiveRecord relations and objects to a CSV-formatted string.
https://github.com/melashu/json_or_ruby_to_csv
ruby ruby-gem
Last synced: about 1 year ago
JSON representation
This gem converts arrays of JSON objects, arrays of hashes, arrays of ActionController params, simple hashes, or ActiveRecord relations and objects to a CSV-formatted string.
- Host: GitHub
- URL: https://github.com/melashu/json_or_ruby_to_csv
- Owner: melashu
- License: mit
- Created: 2023-05-21T12:13:16.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-06-08T19:53:21.000Z (about 3 years ago)
- Last Synced: 2025-03-30T08:07:46.156Z (about 1 year ago)
- Topics: ruby, ruby-gem
- Language: Ruby
- Homepage:
- Size: 13.7 KB
- Stars: 5
- Watchers: 2
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 📗 Table of Contents
[](https://rubygems.org/gems/json_or_ruby_to_csv)
- [📗 Table of Contents](#-table-of-contents)
- [📖 json\_or\_ruby\_to\_csv ](#-json_or_ruby_to_csv-)
- [Installation ](#installation-)
- [Usage](#usage)
- [Using array\_or\_hash\_to\_csv](#using-array_or_hash_to_csv)
- [Using activerecord\_to\_csv](#using-activerecord_to_csv)
- [json\_or\_ruby\_to\_csv usage with rails controller](#json_or_ruby_to_csv-usage-with-rails-controller)
- [👥 Authors ](#-authors-)
- [🤝 Contributing ](#-contributing-)
- [⭐️ Show your support ](#️-show-your-support-)
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
```
### Usage
`require` this gem on the top of your ruby code.
#### Using array_or_hash_to_csv
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
```ruby
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.
```ruby
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**
```csv
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
```
#### Using activerecord_to_csv
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*
```ruby
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.
```ruby
employee = Employee.new
employee.get_data_as_csv
```
**The output looks like below**
```csv
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
```
#### json_or_ruby_to_csv usage with rails controller
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.
```ruby
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
- GitHub: [@melashu](https://github.com/melashu)
- Twitter: [@meshu102](https://twitter.com/meshu102)
- LinkedIn: [Melashu Amare](https://www.linkedin.com/in/melashu-amare/)
This repo is open for contributions. Issues, and feature requests are welcome!
Feel free to check the [issues page.](https://github.com/melashu/json_or_ruby_to_csv/issues)
Give a star if you like this project!