json_or_ruby_to_csv 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +2 -2
- data/README.md +138 -91
- data/lib/json_or_ruby_to_csv/helper.rb +0 -2
- data/lib/json_or_ruby_to_csv/version.rb +1 -1
- data/lib/json_or_ruby_to_csv.rb +20 -1
- data/spec/helper_spec.rb +13 -6
- data/spec/json_or_ruby_to_csv_spec.rb +29 -0
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 48510a6c840fb0b34b5414b50cd342f7d2481e9cbdf42f7408517d108ed6e2da
|
4
|
+
data.tar.gz: 50b7e0b189c61e5d1f944fad7eaa6cc6c713c2c1e8ad83c006079d3a2ee40831
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d86fe5e254ef049cdeac58dc14a83f11bd900064ecf94d715bc05a7858dfac13da72e9cdeae08fb0b207839ba703738c869ed77ff51eca5183390dbd0e9291e
|
7
|
+
data.tar.gz: 92537631b911438d4ad142ae3857e5f9fcdb34490498afba41ff1b839bc2531246102d24f48d2693adb4a4b08bbf5c94f4e3a89fffb0ce144e3484c948eb70c6
|
data/Gemfile
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
gemspec
|
3
|
+
gemspec
|
data/README.md
CHANGED
@@ -1,138 +1,183 @@
|
|
1
1
|
|
2
2
|
<a name="readme-top"></a>
|
3
3
|
|
4
|
-
|
5
4
|
# 📗 Table of Contents
|
5
|
+
[](https://rubygems.org/gems/json_or_ruby_to_csv)
|
6
6
|
|
7
7
|
- [📗 Table of Contents](#-table-of-contents)
|
8
|
-
- [📖
|
9
|
-
- [
|
10
|
-
- [Tech Stack ](#tech-stack-)
|
11
|
-
- [Key Features ](#key-features-)
|
12
|
-
- [💻 Getting Started ](#-getting-started-)
|
13
|
-
- [Prerequisites](#prerequisites)
|
14
|
-
- [Setup](#setup)
|
8
|
+
- [📖 json\_or\_ruby\_to\_csv ](#-json_or_ruby_to_csv-)
|
9
|
+
- [Installation ](#installation-)
|
15
10
|
- [Usage](#usage)
|
16
|
-
- [Using
|
17
|
-
- [Using
|
18
|
-
|
11
|
+
- [Using array\_or\_hash\_to\_csv](#using-array_or_hash_to_csv)
|
12
|
+
- [Using activerecord\_to\_csv](#using-activerecord_to_csv)
|
13
|
+
- [json\_or\_ruby\_to\_csv usage with rails controller](#json_or_ruby_to_csv-usage-with-rails-controller)
|
19
14
|
- [👥 Authors ](#-authors-)
|
20
|
-
- [
|
15
|
+
- [🤝 Contributing ](#-contributing-)
|
16
|
+
- [⭐️ Show your support ](#️-show-your-support-)
|
21
17
|
|
22
|
-
# 📖
|
18
|
+
# 📖 json_or_ruby_to_csv <a name="about-project"></a>
|
23
19
|
|
24
|
-
|
20
|
+
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.
|
25
21
|
|
26
|
-
|
22
|
+
## Installation <a name="tech-stack"></a>
|
27
23
|
|
28
|
-
|
24
|
+
Add the following code to you Gemfile
|
29
25
|
|
30
|
-
|
26
|
+
```
|
27
|
+
gem 'json_or_ruby_to_csv'
|
28
|
+
```
|
29
|
+
or
|
31
30
|
|
32
|
-
|
33
|
-
To allow users to view previous data, we use a SQLite database to persist the data when we convert it for the first time. So user can view persisted data using `/convert/previous` endpoint.
|
34
|
-
|
35
|
-
## Tech Stack <a name="tech-stack"></a>
|
31
|
+
install the gem on your terminal
|
36
32
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
- SQLite Database
|
33
|
+
```
|
34
|
+
gem install json_or_ruby_to_csv
|
35
|
+
```
|
41
36
|
|
42
|
-
## Key Features <a name="key-features"></a>
|
43
|
-
The key features of this API
|
44
|
-
|
45
|
-
- Converts JSON-formatted arrays of objects to CSV-formatted data
|
46
|
-
- Persists the data for future use
|
47
|
-
- Allows users to view previously converted data
|
48
|
-
## 💻 Getting Started <a name="getting-started"></a>
|
49
|
-
|
50
|
-
To get a local copy up and running, follow these steps
|
51
|
-
|
52
|
-
### Prerequisites
|
53
|
-
|
54
|
-
- Install [Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
|
55
|
-
- Install [Ruby version 3.1.2(2022-04-12 revision)](https://rubyinstaller.org/)
|
56
|
-
- Open your terminal and install rails using `gem install rails`
|
57
|
-
- Install [Postman](https://www.postman.com/downloads/) or other API testing tool
|
58
|
-
- Install [Visual Studio Code](https://code.visualstudio.com/download) or other code editor
|
59
|
-
|
60
|
-
### Setup
|
61
|
-
|
62
|
-
Clone this repository to your desired folder:
|
63
37
|
|
64
|
-
|
65
|
-
|
66
|
-
cd `json_to_csv`
|
38
|
+
### Usage
|
67
39
|
|
68
|
-
|
40
|
+
`require` this gem on the top of your ruby code.
|
69
41
|
|
70
|
-
|
42
|
+
#### Using array_or_hash_to_csv
|
71
43
|
|
72
|
-
Run `rails server` or `rails s`
|
73
44
|
|
74
|
-
|
45
|
+
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:-
|
75
46
|
|
76
|
-
|
47
|
+
> Don't forget to include `JsonOrRubyToCsv` module in your ruby class.
|
77
48
|
|
78
|
-
|
49
|
+
<i>For example </i>
|
79
50
|
|
80
|
-
|
51
|
+
```
|
81
52
|
|
82
|
-
|
53
|
+
require 'json_or_ruby_to_csv'
|
54
|
+
class Employee
|
55
|
+
include JsonOrRubyToCsv
|
83
56
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
57
|
+
def get_csv_data(data_list)
|
58
|
+
array_or_hash_to_csv(data_list)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
```
|
62
|
+
Let's create `Employee` object and send array of object to `get_csv_data` method.
|
63
|
+
|
64
|
+
```
|
65
|
+
employee = Employee.new
|
66
|
+
data_list = [
|
67
|
+
{ "id": 200,
|
68
|
+
"fname": "Meshu",
|
69
|
+
"lname": "Amare",
|
90
70
|
"salary": 4500,
|
91
|
-
"company": "
|
71
|
+
"company": "XYZ.com",
|
92
72
|
"position": "DevOps IT Operations"
|
93
73
|
},
|
94
74
|
|
95
|
-
{ "id":
|
96
|
-
"fname": "
|
97
|
-
"lname": "
|
75
|
+
{ "id": 201,
|
76
|
+
"fname": "Solomon",
|
77
|
+
"lname": "Alebachew",
|
98
78
|
"salary": 5000,
|
99
|
-
"company": "
|
79
|
+
"company": "XYZ.com",
|
100
80
|
"position": "Rails Developer"
|
101
81
|
},
|
102
82
|
{
|
103
|
-
"id":
|
104
|
-
"fname": "
|
105
|
-
"lname": "
|
83
|
+
"id": 202,
|
84
|
+
"fname": "Bini",
|
85
|
+
"lname": "Ew",
|
106
86
|
"salary": 5000,
|
107
|
-
"company": "
|
87
|
+
"company": "XYZ.com",
|
108
88
|
"position": "Senior Sofware Developer"
|
109
89
|
}
|
110
90
|
]
|
111
|
-
}
|
112
91
|
|
113
|
-
|
114
|
-
|
92
|
+
print employee.get_csv_data(data_list)
|
93
|
+
|
94
|
+
```
|
95
|
+
|
96
|
+
**The output looks like below**
|
97
|
+
|
98
|
+
```
|
99
|
+
id,fname,lname,company,position,salary
|
100
|
+
200,Meshu,Amare,XYZ.com,DevOps IT Operations,4500
|
101
|
+
201,Solomon,Alebachew,XYZ.com,Rails Developer,5000
|
102
|
+
203,Bini,Ew,XYZ.com,Senior Sofware Developer,5000
|
103
|
+
```
|
104
|
+
|
105
|
+
#### Using activerecord_to_csv
|
115
106
|
|
116
|
-
|
107
|
+
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.
|
117
108
|
|
118
|
-
|
109
|
+
*For example*
|
119
110
|
|
120
|
-
|
111
|
+
```
|
121
112
|
|
122
|
-
|
113
|
+
require 'json_or_ruby_to_csv'
|
114
|
+
class Employee
|
115
|
+
include JsonOrRubyToCsv
|
123
116
|
|
124
|
-
|
117
|
+
def get_data_as_csv
|
118
|
+
all_employee = Employee.select(:id, :fname, :lname, :salary, :company, :position)
|
119
|
+
activerecord_to_csv(all_employee)
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
|
124
|
+
```
|
125
|
+
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.
|
126
|
+
|
127
|
+
Let's create `Employee` object and call `get_data_as_csv` method.
|
128
|
+
|
129
|
+
```
|
130
|
+
employee = Employee.new
|
131
|
+
employee.get_data_as_csv
|
132
|
+
|
133
|
+
```
|
134
|
+
|
135
|
+
**The output looks like below**
|
136
|
+
|
137
|
+
```
|
138
|
+
id,fname,lname,company,position,salary
|
139
|
+
200,Meshu,Amare,XYZ.com,DevOps IT Operations,4500
|
140
|
+
201,Solomon,Alebachew,XYZ.com,Rails Developer,5000
|
141
|
+
203,Bini,Ew,XYZ.com,Senior Sofware Developer,5000
|
142
|
+
```
|
143
|
+
|
144
|
+
#### json_or_ruby_to_csv usage with rails controller
|
125
145
|
|
126
|
-
|
146
|
+
You can include the functionality of this gem in your controller class.
|
127
147
|
|
128
|
-
|
148
|
+
*for example*
|
129
149
|
|
130
|
-
|
150
|
+
If you want your controller action to return a CSV formated data after fetching data from the database, you can do it as follow.
|
131
151
|
|
132
|
-
|
152
|
+
```
|
153
|
+
require 'json_or_ruby_to_csv'
|
154
|
+
class ConvertsController < ApplicationController
|
155
|
+
include JsonOrRubyToCsv
|
156
|
+
def index
|
157
|
+
input = params_value[:data]
|
158
|
+
result = array_or_hash_to_csv(input)
|
159
|
+
render plain: result, status: :ok
|
160
|
+
end
|
133
161
|
|
134
|
-
|
135
|
-
|
162
|
+
|
163
|
+
def previous
|
164
|
+
all_employee = Employee.select(:id, :fname, :lname, :salary, :company, :position)
|
165
|
+
result = activerecord_to_csv(all_employee)
|
166
|
+
render plain: result, status: :ok
|
167
|
+
end
|
168
|
+
|
169
|
+
private
|
170
|
+
|
171
|
+
def params_value
|
172
|
+
params.require(:data) # there must be data
|
173
|
+
params.permit(data: %i[id fname lname company position salary]) # list of optional attributes
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
|
178
|
+
```
|
179
|
+
|
180
|
+
`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.
|
136
181
|
|
137
182
|
## 👥 Authors <a name="authors"></a>
|
138
183
|
|
@@ -142,12 +187,14 @@ Run
|
|
142
187
|
- Twitter: [@meshu102](https://twitter.com/meshu102)
|
143
188
|
- LinkedIn: [Melashu Amare](https://www.linkedin.com/in/melashu-amare/)
|
144
189
|
|
145
|
-
##
|
190
|
+
## 🤝 Contributing <a name="contributing"></a>
|
191
|
+
|
192
|
+
This repo is open for contributions. Issues, and feature requests are welcome!
|
193
|
+
|
194
|
+
Feel free to check the [issues page.](https://github.com/melashu/json_or_ruby_to_csv/issues)
|
146
195
|
|
147
|
-
|
196
|
+
## ⭐️ Show your support <a name="support"></a>
|
148
197
|
|
149
|
-
|
150
|
-
- [ ] Allow the API to convert any json formated object
|
151
|
-
- [ ] Add API version
|
198
|
+
Give a star if you like this project!
|
152
199
|
|
153
200
|
<p align="right">(<a href="#readme-top">back to top</a>)</p>
|
data/lib/json_or_ruby_to_csv.rb
CHANGED
@@ -1,10 +1,17 @@
|
|
1
1
|
require_relative './json_or_ruby_to_csv/helper'
|
2
|
+
|
2
3
|
module JsonOrRubyToCsv
|
3
4
|
include Helper
|
4
5
|
def array_or_hash_to_csv(data)
|
6
|
+
|
5
7
|
case data
|
6
8
|
when Array
|
7
|
-
|
9
|
+
begin
|
10
|
+
new_data = data.map(&:to_h)
|
11
|
+
convert_array_to_csv(new_data)
|
12
|
+
rescue StandardError
|
13
|
+
'Invalid format!'
|
14
|
+
end
|
8
15
|
when Hash
|
9
16
|
convert_hash_to_csv(data)
|
10
17
|
else
|
@@ -12,11 +19,23 @@ module JsonOrRubyToCsv
|
|
12
19
|
end
|
13
20
|
end
|
14
21
|
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
15
26
|
def activerecord_to_csv(data)
|
27
|
+
|
16
28
|
if data.respond_to?(:each)
|
29
|
+
|
17
30
|
relation_to_array(data)
|
31
|
+
|
18
32
|
else
|
33
|
+
|
19
34
|
object_to_hash(data)
|
35
|
+
|
20
36
|
end
|
37
|
+
|
21
38
|
end
|
39
|
+
|
22
40
|
end
|
41
|
+
|
data/spec/helper_spec.rb
CHANGED
@@ -1,16 +1,23 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
require 'json_or_ruby_to_csv'
|
3
3
|
require 'spec_helper'
|
4
|
-
describe
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
describe Helper do
|
5
|
+
let(:dummy){Dummy.new}
|
6
|
+
context 'When we pass array of hash' do
|
7
|
+
let(:data){[{name: 'Meshu', age: 29}]}
|
8
|
+
it 'convert_array_to_csv should return csv formated string ' do
|
9
|
+
expect(dummy.convert_array_to_csv(data)).to eq("name,age\nMeshu,29")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
context 'When we pass simple hash' do
|
13
|
+
let(:data){{name: 'Meshu', age: 29}}
|
14
|
+
it 'convert_hash_to_csv should return csv formated string ' do
|
15
|
+
expect(dummy.convert_hash_to_csv(data)).to eq("name,age\nMeshu,29")
|
9
16
|
end
|
10
17
|
end
|
11
18
|
end
|
12
19
|
|
13
20
|
class Dummy
|
14
|
-
include
|
21
|
+
include Helper
|
15
22
|
|
16
23
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
describe JsonOrRubyToCsv do
|
4
|
+
let(:dummy){ Dummy.new }
|
5
|
+
context 'When we pass array of string ' do
|
6
|
+
let(:data){["Meshu", "Amare",29]}
|
7
|
+
it 'array_or_hash_to_csv should return invalid format! ' do
|
8
|
+
expect(dummy.array_or_hash_to_csv(data)).to eq('Invalid format!')
|
9
|
+
end
|
10
|
+
end
|
11
|
+
context 'When we pass array of hash' do
|
12
|
+
let(:data){[{name: 'Meshu', age: 29}]}
|
13
|
+
it 'array_or_hash_to_csv should return csv formated string ' do
|
14
|
+
expect(dummy.array_or_hash_to_csv(data)).to eq("name,age\nMeshu,29")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'When we pass simple hash' do
|
19
|
+
let(:data){{name: 'Meshu', age: 29}}
|
20
|
+
it 'array_or_hash_to_csv should return csv formated string ' do
|
21
|
+
expect(dummy.array_or_hash_to_csv(data)).to eq("name,age\nMeshu,29")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Dummy
|
27
|
+
include JsonOrRubyToCsv
|
28
|
+
|
29
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_or_ruby_to_csv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Melashu Amare
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-05-
|
11
|
+
date: 2023-05-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -38,8 +38,9 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
-
description: This gem
|
42
|
-
ActiveRecord
|
41
|
+
description: This gem converts arrays of JSON objects, arrays of hashes, arrays of
|
42
|
+
ActionController params, simple hashes, or ActiveRecord relations and objects to
|
43
|
+
a CSV-formatted string.
|
43
44
|
email: meshu.amare@gmail.com
|
44
45
|
executables: []
|
45
46
|
extensions: []
|