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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8bafab148ce181a519c47c5de25d43585c17b213c2f8d80ec5150ffcbd1d03c2
4
- data.tar.gz: a6742c8d856291fff8fb44141536d4b6b1f27151185205c5198c0622a03e4525
3
+ metadata.gz: 48510a6c840fb0b34b5414b50cd342f7d2481e9cbdf42f7408517d108ed6e2da
4
+ data.tar.gz: 50b7e0b189c61e5d1f944fad7eaa6cc6c713c2c1e8ad83c006079d3a2ee40831
5
5
  SHA512:
6
- metadata.gz: c544fb68d32881a29b1fee4f2e26cf003c4761d3bff810bae1d7ca73f717c32946192ccef3cceed486ba6eee12f7f783f6a9c11370949be5611600b7300b36f7
7
- data.tar.gz: fba76649ea7911e085748013064cf8fa61db98aa474009ae9fd232c840ea555c018a129fd58408afdd1c7752489639e5449046d047814b6c2b14bdc9f41c51b3
6
+ metadata.gz: 8d86fe5e254ef049cdeac58dc14a83f11bd900064ecf94d715bc05a7858dfac13da72e9cdeae08fb0b207839ba703738c869ed77ff51eca5183390dbd0e9291e
7
+ data.tar.gz: 92537631b911438d4ad142ae3857e5f9fcdb34490498afba41ff1b839bc2531246102d24f48d2693adb4a4b08bbf5c94f4e3a89fffb0ce144e3484c948eb70c6
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
- source "https://rubygems.org"
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
+ [![json_or_ruby_to_ - csv](https://img.shields.io/badge/json__or__ruby__to__-csv-2ea44f)](https://rubygems.org/gems/json_or_ruby_to_csv)
6
6
 
7
7
  - [📗 Table of Contents](#-table-of-contents)
8
- - [📖 JSON\_to\_CSV ](#-json_to_csv-)
9
- - [Why we need to use SQLite database](#why-we-need-to-use-sqlite-database)
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 `/convert` endpoint](#using-convert-endpoint)
17
- - [Using `/convert/previous` endpoint](#using-convertprevious-endpoint)
18
- - [Run tests](#run-tests)
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
- - [🔭 Future Features ](#-future-features-)
15
+ - [🤝 Contributing ](#-contributing-)
16
+ - [⭐️ Show your support ](#️-show-your-support-)
21
17
 
22
- # 📖 JSON_to_CSV <a name="about-project"></a>
18
+ # 📖 json_or_ruby_to_csv <a name="about-project"></a>
23
19
 
24
- JSON_to_CSV is a Rails API that converts an array of objects to a CSV-formatted string. It has two endpoints: `/convert` and `/convert/previous`.
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
- To minimize the error rate when converting an array of objects to CSV-formatted data, the API uses an Employee model with the following attributes: `fname`, `lname`, `salary`, `company`, and `position`.
22
+ ## Installation <a name="tech-stack"></a>
27
23
 
28
- To convert data, the user sends a POST request to the `/convert` endpoint with an array of objects as the payload. The objects must have the above attributes as keys. The `/convert` endpoint persists the data to a SQLite database and responds with a CSV-formatted string.
24
+ Add the following code to you Gemfile
29
25
 
30
- To access previously converted data, the user need to sends GET request to `/convert/previous` endpoint.
26
+ ```
27
+ gem 'json_or_ruby_to_csv'
28
+ ```
29
+ or
31
30
 
32
- ## Why we need to use SQLite database
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
- To complete this task I used the following tools
38
- - Ruby on Rails
39
- - RSpec
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
- `https://github.com/melashu/json_to_csv.git`
65
-
66
- cd `json_to_csv`
38
+ ### Usage
67
39
 
68
- Run `bundler install`
40
+ `require` this gem on the top of your ruby code.
69
41
 
70
- Run `rails db:migrate`
42
+ #### Using array_or_hash_to_csv
71
43
 
72
- Run `rails server` or `rails s`
73
44
 
74
- ### Usage
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
- #### Using `/convert` endpoint
47
+ > Don't forget to include `JsonOrRubyToCsv` module in your ruby class.
77
48
 
78
- To convert data to CSV formated-string, open Postman or other API testing tool, then put `server_address/convert` to the url section and make the request type `POST`
49
+ <i>For example </i>
79
50
 
80
- > NB: server_address may be like http://127.0.0.1:3000 e.g. `http://127.0.0.1:3000/convert`
51
+ ```
81
52
 
82
- Under body section select raw, then copy and put the following JSON formated data as a payload
53
+ require 'json_or_ruby_to_csv'
54
+ class Employee
55
+ include JsonOrRubyToCsv
83
56
 
84
- ````
85
- {
86
- "data": [
87
- { "id": 1,
88
- "fname": "Marek",
89
- "lname": "Vydareny",
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": "Radar Cyber Security",
71
+ "company": "XYZ.com",
92
72
  "position": "DevOps IT Operations"
93
73
  },
94
74
 
95
- { "id": 2,
96
- "fname": "Norbert",
97
- "lname": "Szivós",
75
+ { "id": 201,
76
+ "fname": "Solomon",
77
+ "lname": "Alebachew",
98
78
  "salary": 5000,
99
- "company": "Radar Cyber Security",
79
+ "company": "XYZ.com",
100
80
  "position": "Rails Developer"
101
81
  },
102
82
  {
103
- "id": 3,
104
- "fname": "Ákos",
105
- "lname": "Balogh",
83
+ "id": 202,
84
+ "fname": "Bini",
85
+ "lname": "Ew",
106
86
  "salary": 5000,
107
- "company": "Radar Cyber Security",
87
+ "company": "XYZ.com",
108
88
  "position": "Senior Sofware Developer"
109
89
  }
110
90
  ]
111
- }
112
91
 
113
- ````
114
- Then click `send` button
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
- > NB: Dont forget the objects must have `fname`, `lname`, `salary`, `company`, and `position` attributes as keys. Otherwise, the server will responed `Either the id is duplicted or invalid format,try /convert/previous to see previous data` but you can leave `id` attribute. In addition to this, make sure the request format is JSON
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
- The response looks like
109
+ *For example*
119
110
 
120
- ![image](https://user-images.githubusercontent.com/30173722/236670626-56fe50e3-9f8e-4987-a29b-4fa0f852d48c.png)
111
+ ```
121
112
 
122
- #### Using `/convert/previous` endpoint
113
+ require 'json_or_ruby_to_csv'
114
+ class Employee
115
+ include JsonOrRubyToCsv
123
116
 
124
- To view previously converted data, open Postman or other API testing tool, then put `server_address/convert/previous` to the url section and make the request type `GET`
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
- > E.g. `http://127.0.0.1:3000/convert/previous`
146
+ You can include the functionality of this gem in your controller class.
127
147
 
128
- If there is previously converted data, the server will responed it as CSV-formated data, otherwise it will responed `No data avaliable` message.
148
+ *for example*
129
149
 
130
- ### Run tests
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
- Run
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
- `rails db:migrate RAILS_ENV=test`
135
- to change the enviroment to testing mode, then run `rspec spec `
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
- ## 🔭 Future Features <a name="future-features"></a>
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
- > I would recommend improvement on the following features.
196
+ ## ⭐️ Show your support <a name="support"></a>
148
197
 
149
- - [ ] Add more model validation like checking the length of character, salary must be number
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>
@@ -1,6 +1,4 @@
1
1
  module Helper
2
- def name; end
3
-
4
2
  def convert_array_to_csv(data)
5
3
  csv_keys = data[0].keys.join(',')
6
4
 
@@ -1,3 +1,3 @@
1
1
  module Version
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -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
- data.all? { |ele| ele.is_a? Hash } ? convert_array_to_csv(data) : 'Invalid format!'
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 JsonOrRubyToCsv do
5
- describe "" do
6
- it "" do
7
-
8
- expect(Dummy.new.name).to eq('meshu')
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 JsonOrRubyToCsv
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.1
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-21 00:00:00.000000000 Z
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 convert array of json object, array of hash, simple hash or
42
- ActiveRecord relation and objects to CSV formated string
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: []