Ruby file update notification - ruby-on-rails

I'm working on an Rails app which reads a CSV file on the server. However, the CSV file keeps getting updated.
My SQLite database is populated from the contents of this CSV file. Is there a way to run a Ruby script as soon as the file is modified and make changes in the database?

Sounds like you want the guard gem.
It integrates with stuff like libnotify, fsevent stuff so as to avoid polling, although it does also support polling as a less efficient fallback.

You might want to look into some parts of the Guard gem. Specifically the lib/vendor directory seem to contain the code you need for different platforms.

How is the CSV file updated? If it is updated by some program that you have control over, then you can add a code within the update routine to run the ruby script that upgrades the SQLite database as soon as the CSV file is updated.
Otherwise, you can do polling to check for a change in the CSV file.

Related

How to easily import XML into rails(activerecord) database [duplicate]

This question already has an answer here:
Importing an XML document into a Rails database?
(1 answer)
Closed 8 years ago.
Looking for modules and docs to read about importing xml documents into rails.
What I want to be able to.do.is have a persistent directory to have xml.files put into. Files will be same xml structure. I want files to be easily or automatically updated into the database.
Can I do this with ruby on rails with activercord?
1. When/how to execute process:
Rake task that sync files with DB and exits
Create a rake task that scans through your persistent directory and updates the database. The rake task can be executed manually, or added as a cron job to the system. You can load the rails environment by declaring a task with task :sync => :environment do.
Rake task that keeps running
You could use a gem such as listen to detect file changes to this directory and sync them to the DB. Or your rake task could sleep for a few minutes internally. You could also make this task a system service by creating a upstart job.
Background job library
Use a job library such as resque with resque-scheduler.
2. Code structure
I suggest the rake task/background job only serves as a mechanism to kick off the sync process. Your actual syncing logic/code should still be in normal ruby classes/modules.
3. How to perform xml -> db sync
If you can dictate what the xml looks like, I recommend using activerecord to_xml and from_xml to export/create/update or have a look what they do internally (attributes= method).
If you can not dictate the format, use nokogiri or xmlsimple as mentioned in this post to parse the xml files. Then use the normal active record querying/creating/updating methods to update the database. Use activerecord validations to make sure your data is always consistent in the database. If you want to make sure that no one can insert invalid data with other database connectors (not active record), you could use database indexes.

How to convert an sqlite database to JSON

How do you convert an sqlite database (generated by Rails) to a collection of JSON objects? Is there a set of scripts/ a ruby gem that does that, or is it necessary to code something from scratch?
I'm not sure that I fully understand the question, but here's a stab.
If you have a collection called Widgets, you can call Widgets.all.to_json This is trivial to do in a Rails Console session.
You can then simply output that to where every you like, such as a text file.
Other ways would be to use a db dump task via rake rake -T will show you the available commands.

porting and backing up databases

I am using sqlite3 in development and mysql in production on a Rails 3.2 app.
I'd like to be able to backup the mysql, and also to create a sqlite copy of it for use in the development environment. Anyone know how to do this, or the preferred way to back up mysql at least?
I'm partial to this one, and use it to convert to/from MSSQL, sqlite and MySQL quite a lot:
https://github.com/unixmonkey/rails_db_convert_using_adapters
This may not be feasible if you have a large DB, but I am working with a rather small one (about 10MB).
(1) I back up all of my model classes into a .zip file using a rake task, and then
(2) have a button (with admin authentication) that runs another rake task to reload the data.
So I can backup the data in dev/prod mode, push my files to the other environment, and reload the data from backup (it's in .csv files, so it's DB-independent). This worked for me switching between sqlite3 and mysql2 (I am using Rails 4.0.1 if that's relevant).
I can post code if that would be helpful to people, but it's a little messy so I'll save the eyesore unless someone would find it helpful. I've found the .csv into .zip file backup to be a nice workaround for different SQL systems, if you're working on the order of megabytes.

Editing a YAML file inside a Rails App

I'm trying to use a yaml config file as a very simple flat file db for a small rails app. The app has a single user, so I just need a way to store a username and password. The only thing is that I'd like to be able to edit the username and password while still inside the app and not require an app restart (so I can't load the YAML file inside an initializer...).
Any ideas on how I could accomplish this? I'm not married to the idea of using YAML, so if you have a better suggestion I'm all ears!
You really are better off using a database for this sort of thing, because it's how Rails is designed to work. The Rails default database is SQLite 3, which is a high-performance, reliable single file database.
Don't fight the defaults—use the right tool for the job.
You might wanna try iye for yaml editing on the fly. No DB needed, saves directly to file! Potentially you would just need something that tracks for file changes in your rails app and reloads your yaml file.
Here is the project page for iye: https://github.com/firmafon/iye
http://rubyforge.org/projects/rbyaml
http://yaml4r.sourceforge.net/doc/
(http://www.yaml.org/)

Where to put files that will be read in a Rails app?

I'm developing a Rails application and within that application I developed a Rake task that will read entries from a file and store them into the DB. Producing the code was no problem, but I'd like to know, where do I place the file that is read? Is there a convention for that, if yes, what is it?
I know I could have used the seed.rb file but is it ok, by the standards, to load and read a file from there?
Thanks in advance!
Yes, put the data you wish to load in the db/seeds.rb file and to load it run rake db:seed. This is what this file was designed to do.
I don't think there's a hard and fast Rails convention for this case. When it comes to seed data I put mine in a subfolder of db.
The yaml_db plugin dumps/loads content from/to the database from the file
rails_root/db/data.yml i'm not sure if this is by convention, however, the content is db related making the db folder an appropriate choice
Where to put stuff in Rails is a problem that I've been working around for a while. The question is whether your file can fit into one of the existing concerns. For instance, is it configuration information?
Anyway, perhaps a similar fit would be the schema.rb, which is put in the db directory. Do not modify the schema.rb with your data, of course: I'm merely suggesting that the db directory, or a subdirectory, might be a place to put your file(s).
On the other hand, if you don't see any directories that hold anything similar -- it's not one of the main categories in app, nor any of the main categories above that -- then you can just make up a name and use that.

Resources