json_or_ruby_to_csv 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8bafab148ce181a519c47c5de25d43585c17b213c2f8d80ec5150ffcbd1d03c2
4
+ data.tar.gz: a6742c8d856291fff8fb44141536d4b6b1f27151185205c5198c0622a03e4525
5
+ SHA512:
6
+ metadata.gz: c544fb68d32881a29b1fee4f2e26cf003c4761d3bff810bae1d7ca73f717c32946192ccef3cceed486ba6eee12f7f783f6a9c11370949be5611600b7300b36f7
7
+ data.tar.gz: fba76649ea7911e085748013064cf8fa61db98aa474009ae9fd232c840ea555c018a129fd58408afdd1c7752489639e5449046d047814b6c2b14bdc9f41c51b3
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Melashu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,153 @@
1
+
2
+ <a name="readme-top"></a>
3
+
4
+
5
+ # 📗 Table of Contents
6
+
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)
15
+ - [Usage](#usage)
16
+ - [Using `/convert` endpoint](#using-convert-endpoint)
17
+ - [Using `/convert/previous` endpoint](#using-convertprevious-endpoint)
18
+ - [Run tests](#run-tests)
19
+ - [👥 Authors ](#-authors-)
20
+ - [🔭 Future Features ](#-future-features-)
21
+
22
+ # 📖 JSON_to_CSV <a name="about-project"></a>
23
+
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`.
25
+
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`.
27
+
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.
29
+
30
+ To access previously converted data, the user need to sends GET request to `/convert/previous` endpoint.
31
+
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>
36
+
37
+ To complete this task I used the following tools
38
+ - Ruby on Rails
39
+ - RSpec
40
+ - SQLite Database
41
+
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
+
64
+ `https://github.com/melashu/json_to_csv.git`
65
+
66
+ cd `json_to_csv`
67
+
68
+ Run `bundler install`
69
+
70
+ Run `rails db:migrate`
71
+
72
+ Run `rails server` or `rails s`
73
+
74
+ ### Usage
75
+
76
+ #### Using `/convert` endpoint
77
+
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`
79
+
80
+ > NB: server_address may be like http://127.0.0.1:3000 e.g. `http://127.0.0.1:3000/convert`
81
+
82
+ Under body section select raw, then copy and put the following JSON formated data as a payload
83
+
84
+ ````
85
+ {
86
+ "data": [
87
+ { "id": 1,
88
+ "fname": "Marek",
89
+ "lname": "Vydareny",
90
+ "salary": 4500,
91
+ "company": "Radar Cyber Security",
92
+ "position": "DevOps IT Operations"
93
+ },
94
+
95
+ { "id": 2,
96
+ "fname": "Norbert",
97
+ "lname": "Szivós",
98
+ "salary": 5000,
99
+ "company": "Radar Cyber Security",
100
+ "position": "Rails Developer"
101
+ },
102
+ {
103
+ "id": 3,
104
+ "fname": "Ákos",
105
+ "lname": "Balogh",
106
+ "salary": 5000,
107
+ "company": "Radar Cyber Security",
108
+ "position": "Senior Sofware Developer"
109
+ }
110
+ ]
111
+ }
112
+
113
+ ````
114
+ Then click `send` button
115
+
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
117
+
118
+ The response looks like
119
+
120
+ ![image](https://user-images.githubusercontent.com/30173722/236670626-56fe50e3-9f8e-4987-a29b-4fa0f852d48c.png)
121
+
122
+ #### Using `/convert/previous` endpoint
123
+
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`
125
+
126
+ > E.g. `http://127.0.0.1:3000/convert/previous`
127
+
128
+ If there is previously converted data, the server will responed it as CSV-formated data, otherwise it will responed `No data avaliable` message.
129
+
130
+ ### Run tests
131
+
132
+ Run
133
+
134
+ `rails db:migrate RAILS_ENV=test`
135
+ to change the enviroment to testing mode, then run `rspec spec `
136
+
137
+ ## 👥 Authors <a name="authors"></a>
138
+
139
+ 👤 Melashu Amare
140
+
141
+ - GitHub: [@melashu](https://github.com/melashu)
142
+ - Twitter: [@meshu102](https://twitter.com/meshu102)
143
+ - LinkedIn: [Melashu Amare](https://www.linkedin.com/in/melashu-amare/)
144
+
145
+ ## 🔭 Future Features <a name="future-features"></a>
146
+
147
+ > I would recommend improvement on the following features.
148
+
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
152
+
153
+ <p align="right">(<a href="#readme-top">back to top</a>)</p>
@@ -0,0 +1,35 @@
1
+ module Helper
2
+ def name; end
3
+
4
+ def convert_array_to_csv(data)
5
+ csv_keys = data[0].keys.join(',')
6
+
7
+ csv_file = data.map do |ele|
8
+ ele.values.join(',')
9
+ end.join("\n")
10
+
11
+ "#{csv_keys}\n#{csv_file}"
12
+ end
13
+
14
+ def convert_hash_to_csv(data)
15
+ csv_keys = data.keys.join(',')
16
+
17
+ csv_values = data.values.join(',')
18
+
19
+ "#{csv_keys}\n#{csv_values}"
20
+ end
21
+
22
+ def relation_to_array(data)
23
+ array_value = data.map(&:attributes)
24
+
25
+ convert_array_to_csv(array_value)
26
+ rescue StandardError
27
+ 'Invalid format!'
28
+ end
29
+
30
+ def object_to_hash(data)
31
+ convert_hash_to_csv(data.attributes)
32
+ rescue StandardError
33
+ 'Invalid format!'
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module Version
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ require_relative './json_or_ruby_to_csv/helper'
2
+ module JsonOrRubyToCsv
3
+ include Helper
4
+ def array_or_hash_to_csv(data)
5
+ case data
6
+ when Array
7
+ data.all? { |ele| ele.is_a? Hash } ? convert_array_to_csv(data) : 'Invalid format!'
8
+ when Hash
9
+ convert_hash_to_csv(data)
10
+ else
11
+ 'Invalid format!'
12
+ end
13
+ end
14
+
15
+ def activerecord_to_csv(data)
16
+ if data.respond_to?(:each)
17
+ relation_to_array(data)
18
+ else
19
+ object_to_hash(data)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+ require 'json_or_ruby_to_csv'
3
+ require 'spec_helper'
4
+ describe JsonOrRubyToCsv do
5
+ describe "" do
6
+ it "" do
7
+
8
+ expect(Dummy.new.name).to eq('meshu')
9
+ end
10
+ end
11
+ end
12
+
13
+ class Dummy
14
+ include JsonOrRubyToCsv
15
+
16
+ end
File without changes
@@ -0,0 +1,98 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
16
+ RSpec.configure do |config|
17
+ # rspec-expectations config goes here. You can use an alternate
18
+ # assertion/expectation library such as wrong or the stdlib/minitest
19
+ # assertions if you prefer.
20
+ config.expect_with :rspec do |expectations|
21
+ # This option will default to `true` in RSpec 4. It makes the `description`
22
+ # and `failure_message` of custom matchers include text for helper methods
23
+ # defined using `chain`, e.g.:
24
+ # be_bigger_than(2).and_smaller_than(4).description
25
+ # # => "be bigger than 2 and smaller than 4"
26
+ # ...rather than:
27
+ # # => "be bigger than 2"
28
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
29
+ end
30
+
31
+ # rspec-mocks config goes here. You can use an alternate test double
32
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
33
+ config.mock_with :rspec do |mocks|
34
+ # Prevents you from mocking or stubbing a method that does not exist on
35
+ # a real object. This is generally recommended, and will default to
36
+ # `true` in RSpec 4.
37
+ mocks.verify_partial_doubles = true
38
+ end
39
+
40
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
41
+ # have no way to turn it off -- the option exists only for backwards
42
+ # compatibility in RSpec 3). It causes shared context metadata to be
43
+ # inherited by the metadata hash of host groups and examples, rather than
44
+ # triggering implicit auto-inclusion in groups with matching metadata.
45
+ config.shared_context_metadata_behavior = :apply_to_host_groups
46
+
47
+ # The settings below are suggested to provide a good initial experience
48
+ # with RSpec, but feel free to customize to your heart's content.
49
+ =begin
50
+ # This allows you to limit a spec run to individual examples or groups
51
+ # you care about by tagging them with `:focus` metadata. When nothing
52
+ # is tagged with `:focus`, all examples get run. RSpec also provides
53
+ # aliases for `it`, `describe`, and `context` that include `:focus`
54
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
55
+ config.filter_run_when_matching :focus
56
+
57
+ # Allows RSpec to persist some state between runs in order to support
58
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
59
+ # you configure your source control system to ignore this file.
60
+ config.example_status_persistence_file_path = "spec/examples.txt"
61
+
62
+ # Limits the available syntax to the non-monkey patched syntax that is
63
+ # recommended. For more details, see:
64
+ # https://relishapp.com/rspec/rspec-core/docs/configuration/zero-monkey-patching-mode
65
+ config.disable_monkey_patching!
66
+
67
+ # This setting enables warnings. It's recommended, but in some cases may
68
+ # be too noisy due to issues in dependencies.
69
+ config.warnings = true
70
+
71
+ # Many RSpec users commonly either run the entire suite or an individual
72
+ # file, and it's useful to allow more verbose output when running an
73
+ # individual spec file.
74
+ if config.files_to_run.one?
75
+ # Use the documentation formatter for detailed output,
76
+ # unless a formatter has already been configured
77
+ # (e.g. via a command-line flag).
78
+ config.default_formatter = "doc"
79
+ end
80
+
81
+ # Print the 10 slowest examples and example groups at the
82
+ # end of the spec run, to help surface which specs are running
83
+ # particularly slow.
84
+ config.profile_examples = 10
85
+
86
+ # Run specs in random order to surface order dependencies. If you find an
87
+ # order dependency and want to debug it, you can fix the order by providing
88
+ # the seed, which is printed after each run.
89
+ # --seed 1234
90
+ config.order = :random
91
+
92
+ # Seed global randomization in this process using the `--seed` CLI option.
93
+ # Setting this allows you to use `--seed` to deterministically reproduce
94
+ # test failures related to randomization by passing the same `--seed` value
95
+ # as the one that triggered the failure.
96
+ Kernel.srand config.seed
97
+ =end
98
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_or_ruby_to_csv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Melashu Amare
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-05-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
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
43
+ email: meshu.amare@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - Gemfile
49
+ - LICENSE
50
+ - README.md
51
+ - lib/json_or_ruby_to_csv.rb
52
+ - lib/json_or_ruby_to_csv/helper.rb
53
+ - lib/json_or_ruby_to_csv/version.rb
54
+ - spec/helper_spec.rb
55
+ - spec/json_or_ruby_to_csv_spec.rb
56
+ - spec/spec_helper.rb
57
+ homepage:
58
+ licenses:
59
+ - MIT
60
+ metadata:
61
+ rubygems_mfa_required: 'true'
62
+ documentation_uri: https://github.com/melashu/json_or_ruby_to_csv
63
+ bug_tracker_uri: https://github.com/melashu/json_or_ruby_to_csv/issues
64
+ homepage_uri: https://rubygems.org/gems/json_or_ruby_to_csv
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: 2.6.0
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubygems_version: 3.3.26
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: This gem convert array of hash and ActiveRecord_Relation to CSV formated
84
+ string
85
+ test_files: []