Can someone point me to some documentation/libraries for working with files in a Rails app?
Specifically I need to scan folders for files and for each one read & parse some data from it. I've not done this in Ruby before so not sure where to look.
Thanks!
Parsing files is very easy in Ruby. To do it in Rails, you simply use the Ruby file libraries. Without knowing what you want to do I can't give you any examples, but I can point you to the Ruby API.
File: http://www.ruby-doc.org/core-1.9.3/File.html
Directory: http://www.ruby-doc.org/core-1.9.3/Dir.html
Related
I've been working on a Ruby parser, that fetches data from different API sources, and compile this data into a clear read-to-use JSON file.
For my use case, i need to store the data i'm initially fetching from the different sources as i don't want to fetch them each time I use the code.
For now i'm writing the JSON i'm receiving from the API sources locally into different JSON files stored in a data folder where my ruby script is. Then i read those files again, parse them and generate my new formatted JSON file that i'm gonna use later in a Rails app.
For that matter i want to create a Gem from this ruby script, which i'm currently working on. Nevertheless i'm not sure to fully understand how and where i should store that data (the one i'm fetching and the one i'm generating).
For now i have tried to simply keep the code as is and simply try to write the file like so:
URI.open("path/to/where/i/wanna/store/file.json", "wb") do |file|
file << URI.open(fetched_data_url).read
end
But wherever i try to write the data i get a :
No such file or directory # rb_sysopen path/to/where/i/wanna/store/file.json
Which in a way does not surprise me that much as i expected it to work in different ways in the context of a Gem. But i'm still missing something here about how to handle this. I'm not sure to fully understand how that all works, especially when you use paths in a gem that will ultimately be used in a rails project.
So several questions here:
Whenever you use a path to write a file inside a Gem, is that path relative to the gem or to the project that will ultimately use that Gem? (and consequently will the file be written inside the project that uses the Gem?)
In that precise use case here, what should i do about it? Where and how do i store my data so that i can use it later? knowing that i need to store it as a JSON file and that for now any attempt of writing a file ends up with an error.
Any input on what i'm misunderstanding here would be much appreciated ! Thanks !
Whenever you use a path to write a file inside a Gem, is that path relative to the gem or to the project that will ultimately use that Gem?
There is nothing special about using file paths whether the code is part of a Gem or not.
path/to/where/i/wanna/store/file.json is a relative path, which means it is looked up relative to the current working directory of the user who started the script. That's nothing special about Gems, that's not even anything to do with Ruby. That is just how file paths work. Relative paths are relative to the current working directory, absolute paths are not.
Where and how do i store my data so that i can use it later?
This depends largely on the Operating System Environment. Different OS Environments have different conventions where to store what kind of files. E.g. your files look like they fit the definition of a cache and Windows has a dedicated folder for caches, as does macOS, as do Linux distributions that follow the Linux Standard Base, as do Desktop Environments that follow the Free Desktop Standards, as does Android, as does iOS, …
For example, the Free Desktop Group has the XDG Base Directory Specification, which defines directories for application state, application data, application cache, and many other things for XDG-compliant environments. Microsoft has similar specifications for Windows. The LSB has something to say as well.
Update:
After looking more closely into the issue I think I am understanding the problem wrong. Since epub is essentially a zipped file I have to generate files at some point.
The actual question would be how to do this efficiently in production if the number of files and file size I need to generate become large?
The ebook content will be generated from entries in the database as html files. I am thinking about storing those files with Amazon S3 but I am not sure if that's the best option out there.
Original Question
I am trying to create a web-based epub generation application with Ruby On Rails.
Currently I am looking into the eeepub gem: https://github.com/jugyo/eeepub.
I am wondering if there is a way to feed the epub content from database without declaring files as shown in the example.
files [File.join(dir, 'foo.html'), File.join(dir, 'bar.html')]
There is an open issue regarding this:https://github.com/jugyo/eeepub/issues/17
from years ago....
I know the gem is very old and does not seem to be active at all. I have looked through the source code and still not seeing a solution. If anyone has any pointers on how to achieve this through eeepub or a better tool please help me out! Thanks in advance.
Hi #voidwalker You can check the best gems for e-publishing on Ruby-toolbox, here you can compare gems by their popularity and activity.
from this list I think the Git-scribe is the best gem as per your requirement. Please try it and let me know if it's helpful.
Thanks
Was digging around my Rails applications and noticed that there are rails.rb files all over the place. In my ruby gems directories like:
...gems\devise-2.0.4\lib\devise\rails.rb
...gems\cucumber-rails-1.3.0\lib\cucumber\rails.rb
...gems\railties-3.2.3\lib\rails.rb
I am assuming that there are executed whenever you issue some command like "rails xxx". So all these extra rails.rb files combine with the original rails.rb file to essentially make one big rails.rb file. Essentially, when we type in "rails xxx" it goes thru all them all?
Just looking for some confirmation PLUS a little more knowledge about this. Thanks.
The best way to understand what these rails.rb files are doing, is to read the source code.
ralties
devise
cucumber-rails
As you can see, in any library the file assumes a different scope. The common behaviour is that the file rails.rb normally contains the code required to initialize the library when loaded from a Rails project.
BTW, this has nothing to do with the script/rails command and there is no "big rails.rb" file.
The files are not generated but are simply source files of these libraries you are using.
In this case they are probably rails-related classes that either extend Rails in some way or modify it or make the library interact with Rails.
Rails is a very common framework in Ruby land so most if not all libraries will have some sort of integration with Rails.
By no means are all of those loaded when you run rails XXX but rather when your application loads these libraries their rails.rb files may be executed to provide some sort of integration with Rails.
I was wondering what the best option for generating doc for rails, its plugin and the app in one single file that I can navigate.
I've been using rdoc but that creates multiple files, yard is too slow and hanna gets stuck at random places.
Any help?
If you truly want just the one file, maybe rocco would work well for you:
I was just wondering if anyone knew of any good libraries for parsing .doc files (and similar formats, like .odt) to extract text, yet also keep formatting information where possible for display on a website.
Capability of doing similarly for PDFs would be a bonus, but I'm not looking as much for that.
This is for a Rails project, if that helps at all.
Thanks in advance!
Apache's POI is a very popular way to access Word and Excel documents. There's a Ruby POI binding that might be worth investigating, but it looks like you'll have to build it yourself. And the API doesn't seem very Ruby-like since it's virtually a direct port from the Java code. And it seems to only have been tested against Ruby 1.8.2.