Rails 7 Service App: Process and Save Fiscal Data

Quick Guide To Handling CSV, XML, and JSON Files in Rails 7 — Part 0 #AppliedRailsSeries

J3
Jungletronics
4 min readSep 6, 2024

--

To create a service app in Rails 7 that reads fiscal files (such as CSV, XML, or JSON) and saves the data to a database, you can follow the steps outlined below.

Let’s Get Started!

Note: if you get stuck, please see my repo.

1. Set Up the Rails Application

First, generate a new Rails application:

rails new fiscal_service_app
cd fiscal_service_app

2. Create the Necessary Models

Assuming you’re reading fiscal data (like invoices, receipts, etc.), you might need models to store this data. Let’s assume you’re reading an invoice file. You can create an Invoice model and edit it’s class:

rails generate model Invoice number:string date:date total:decimal
rails db:migrate

This creates the Invoice model and the associated invoices table in your database.

Edit app/models/invoice.rb:


class Invoice < ApplicationRecord
validates :number, presence: true
validates :date, presence: true
validates :total, presence: true, numericality: true
end

3.Here’s how to generate a test file for your file reader service app:

To set it up, on Terminal type:

. Create the directory (if it doesn’t exist):

mkdir -p spec/fixtures/files

. Create this script on the root directory:

touch generate_invoice.rb

Edit generate_invoice.rb:

File.open(Rails.root.join('spec', 'fixtures', 'files', 'invoice.csv'), 'w') do |file|
file.write("number,date,total\nINV-001,2024-01-15,100.50\nINV-002,2024-02-20,250.75\nINV-003,2024-03-10,175.00\nINV-004,2024-04-05,300.00\nINV-005,2024-05-12,125.25\n")
end

. Run the script on terminal:

rails runner generate_invoice.rb

. The result should be:

spec/fixtures/files/invoice.csv

number,date,total
INV-001,2024-01-15,100.50
INV-002,2024-02-20,250.75
INV-003,2024-03-10,175.00
INV-004,2024-04-05,300.00
INV-005,2024-05-12,125.25

4. Service Object for File Parsing

Create a service object that will handle reading and processing fiscal files. For example, if you’re dealing with CSV files, you can use Ruby’s CSV library.

Create a Service Object:

mkdir app/services
touch app/services/file_reader_service.rb

5. Implement the Service to Handle File Parsing

app/services/file_reader_service.rb:

require 'csv'

class FileReaderService
def initialize(file_path)
@file_path = file_path
end

def call
process_file
end

private

def process_file
CSV.foreach(@file_path, headers: true) do |row|
# Assuming CSV has columns: number, date, total
Invoice.create!(
number: row['number'],
date: Date.parse(row['date']),
total: row['total'].to_f
)
end
rescue StandardError => e
Rails.logger.error "Error processing file: #{e.message}"
# Handle error as needed
ensure
# Clean up the file if needed
File.delete(@file_path) if File.exist?(@file_path)
end
end

This service reads each row in the CSV file and saves the data as an Invoice in the database.

6. Controller to Handle File Upload

Create a controller to handle file upload through a form in your app.

Generate the Controller:

rails generate controller FiscalFiles

7.Implement the File Upload Action

app/controllers/fiscal_files_controller.rb:

class FiscalFilesController < ApplicationController
def new
# Display a form to upload the fiscal file
end

def create
uploaded_file = params[:file]

if uploaded_file
# Save the uploaded file to a temporary location
file_path = Rails.root.join('tmp', uploaded_file.original_filename)
File.open(file_path, 'wb') do |file|
file.write(uploaded_file.read)
end

# Process the file using the service
FileReaderService.new(file_path).call

flash[:notice] = 'File processed successfully!'
redirect_to new_fiscal_file_path
else
flash[:alert] = 'No file selected!'
render :new
end
end
end

8. View for File Upload Form

Create a simple view to upload the file.

app/views/fiscal_files/new.html.erb:

<h1>Upload Fiscal File</h1>

<%= form_with url: fiscal_files_path, local: true, multipart: true do |form| %>
<div class="field">
<%= form.label :file, "Choose a fiscal file (CSV)" %>
<%= form.file_field :file %>
</div>

<div class="actions">
<%= form.submit "Upload File" %>
</div>
<% end %>

9. Routes Configuration

Edit config/routes.rb to add routes for the fiscal file uploads:

Rails.application.routes.draw do
resources :fiscal_files, only: [:new, :create]
root "fiscal_files#new"
end

10. Test the Application

Start the Rails server:

rails server

Visit http://localhost:3000 and upload the fiscal file located in the spec/fixtures/files directory using the form. Once uploaded, the file will be parsed, and the data saved to the database.

This is a simple interface for uploading files.

Next, open the DBeaver App to verify that everything was saved correctly. Congratulations! You now have a fully functioning service app.

DBeaver is an excellent tool for database maintenance!

In the next episode, we’ll implement tests for your Fiscal Service App using Capybara and RSpec.

See you next time!

11. Additional Features (Optional)

  • Handling Different File Types (XML, JSON): You can modify the FileReaderService to handle other file types by checking the file extension and using appropriate parsers for XML (Nokogiri) or JSON files.
  • Error Handling: Ensure robust error handling in the service to catch any file format or validation issues during the file reading process.
  • Background Jobs: If processing large files, consider moving the file processing to a background job using gems like Sidekiq or Delayed Job.

This should give you a complete Rails 7 service app that reads fiscal files, parses the data, and saves it into a database.

👉 GitHub Repo v0

🚀 About Me

I’m a Full Stack Developer with a degree in Computer Engineering. You can explore my work and projects on my GitHub repository:

GitHub Repo

git tag -a v0 -m "Rails_Applied_v0: Go to https://medium.com/jungletronics/rails-7-service-app-process-and-save-fiscal-data-92ba2a05b38d" -m "0- Set Up Your Rails Application;" -m "1- Add Necessary Gems;" -m "2- Generate Models and Controllers;" -m "3- Implement File Upload and Parsing Logic;" -m "4- Add logic in the InvoicesController;" -m "5-Test Your Application;" -m "Thank you for downloading this project 😍️" 
git push origin v0

👉 V0 zip file

--

--

J3
Jungletronics

😎 Gilberto Oliveira Jr | 🖥️ Computer Engineer | 🐍 Python | 🧩 C | 💎 Rails | 🤖 AI & IoT | ✍️