# Proper Factoring for a Parser Class

**URL:** <https://www.ruby-forum.com/t/proper-factoring-for-a-parser-class/114450>\
**Category:** Rails\
**Created:** [September 12, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/proper-factoring-for-a-parser-class/114450 "2007-09-12T20:20:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![evronm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f1d935/32.png) [@evronm](https://www.ruby-forum.com/u/evronm)\
**Post date:** [September 12, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/proper-factoring-for-a-parser-class/114450/1 "2007-09-12T20:20:26Z")

</div>

Hello Railers!

I’m developing an application where I need to extract text from a number  
of different file formats. To this end I would like to create a Parser  
class that will be reusable and handle all of the heavy lifting.  
Ideally I would like to do something like this (sorry for any code  
mistakes):

my\_rtf\_parser = Parser.new(‘path/to/rtf\_file.rtf’)  
my\_text = my\_rtf\_parser.to\_text

my\_pdf\_parser = Parser.new(‘path/to/pdf\_file.pdf’)  
my\_text = my\_pdf\_parser.to\_text

The Parser class should be able to determine the correct type for the  
file being passed to it and then load up the “to\_text” method  
appropriate to that file type.

What is the best way to separate out the classes that will perform these  
action and where should I locate the files in the Rails directory  
structure?

Thanks in advance for the assistance.

Mike

---

<div class="post-metadata">

**Author:** ![evronm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f1d935/32.png) [@evronm](https://www.ruby-forum.com/u/evronm)\
**Post date:** [September 12, 2007, 8:27pm UTC](https://www.ruby-forum.com/t/proper-factoring-for-a-parser-class/114450/2 "2007-09-12T20:27:57Z")

</div>

Mike E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The Parser class should be able to determine the correct type for the  
> file being passed to it and then load up the “to\_text” method  
> appropriate to that file type.
> 
> What is the best way to separate out the classes that will perform these  
> action and where should I locate the files in the Rails directory  
> structure?

How to separate them is really a matter of taste… I’d probably make  
them  
Parser::pdf , Parser::rdf , etc… I dont know where your files are  
coming  
from, but from a security/bulletproofing perspective I would not trust  
the  
extension, I’d use mime magic to determine the type instead.

As for where to locate them in the rails structure… this operation  
is  
pretty low level and doesnt appear to involve activerecord, etc at all…  
Depending on how big this class ends up, I’d suggest making a gem out of  
it  
instead. If you are sure you’re just going to use it in one project and  
never distribute it elsewhere, then I’d shove it into lib/.

Cheers,  
Tyler

---

<div class="post-metadata">

**Author:** ![evronm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f1d935/32.png) [@evronm](https://www.ruby-forum.com/u/evronm)\
**Post date:** [September 12, 2007, 8:44pm UTC](https://www.ruby-forum.com/t/proper-factoring-for-a-parser-class/114450/3 "2007-09-12T20:44:20Z")

</div>

Tyler MacDonald wrote:

> extension, I’d use mime magic to determine the type instead.

Tyler, I’m unfamiliar with mime magic. What steps would I take to  
integrate it into my app.

> As for where to locate them in the rails structure… this operation  
> is  
> pretty low level and doesnt appear to involve activerecord, etc at all…  
> Depending on how big this class ends up, I’d suggest making a gem out of  
> it  
> instead. If you are sure you’re just going to use it in one project and  
> never distribute it elsewhere, then I’d shove it into lib/.

Good advice and much appreciated. Thanks.

Mike

---

<div class="post-metadata">

**Author:** ![evronm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f1d935/32.png) [@evronm](https://www.ruby-forum.com/u/evronm)\
**Post date:** [September 12, 2007, 8:52pm UTC](https://www.ruby-forum.com/t/proper-factoring-for-a-parser-class/114450/4 "2007-09-12T20:52:00Z")

</div>

Mike E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > extension, I’d use mime magic to determine the type instead.
> 
> Tyler, I’m unfamiliar with mime magic. What steps would I take to  
> integrate it into my app.

[http://shared-mime.rubyforge.org/](http://shared-mime.rubyforge.org/) – check\_magics is what you’re  
looking  
for.

Cheers,  
Tyler
