Location for XSD file used for validation during RSpec test - ruby-on-rails

I am building XML files inside my application and I would like to validate the generated XML's format against an XSD file. I am not sure where shall I put this schema.xsd file so that it can be easily retrieved inside an RSpec example.
I was thinking about putting it inside spec/support, but I am not sure about it.

I would suggest spec/fixtures, since it is part of your test suite, it's somewhat static data and it can be accessed easily via fixture_path (you can set any other *_path helper in your spec_helper file if you wish to).

I've ended up putting my .xsd file in spec/support/schemas/; it felt more natural for me, since it is a support file.

Related

How to properly include code in .rb files into Rails architecture?

I am currently coding an application in Ruby that does some simple external API calls to Soundcloud's API.
I have developed a bunch of code inside a single .rb file and want to put this into the rails architecture. This Ruby file has the following classes:
class SoundcloudUser
class SoundcloudQuery
class SoundcloudFollowers
Currently I understand that I can put these classes into seperate .rb files, and then just put them into the /models/ folder which then gives me the ability to call these classes from elsewhere in my rails application (using require/include).
My question is simply, is this the correct way to go about this? I am familiar with rails, but I am new to transferring a Ruby developed project into the rails format. I tried searching best practices for this in the Ruby style guide but I didn't really find anything.
On a side note - I wanted to also create another class that acts as a ?service? wherein in checks my local database if an entry already exists in the database, and if not, then it will query new data. My side-question here would be similar - where would this .rb file for this 'service' live?
I hope I explained my question clearly enough, if not, I am happy to add some clarifications. Thank you for your time!
If in Rails, you can put them in either lib/ or somewhere in the main app directory. For example, you can create app/services and put them inside there, and when you restart the Rails server you should be able to call SoundcloudUser (provided you name them app/services/soundcloud_user.rb.
I always look at the Gitlab source code for this. It's a gigantic Rails app but look at this file: https://github.com/gitlabhq/gitlabhq/blob/master/app/services/gravatar_service.rb. Because it's inside an app/services (any name actually), GravatarService can be called from anywhere in Rails. If you want to have some namespacing, you have to put it in app/services/soundcloud/user.rb or lib/soundcloud/user.rb and name the class Soundcloud::User.
For the class that acts as a service, it seems like it orchestrates the logic of "check if (song?) exists, else scrape. Some people put it in a model class, I'd probably put it in a service class a la the Gitlab source code. Hope I helped.

Ruby on Rails - Testing the contents of a CSV file

So I have a link on my rails web page that generates a CSV file. How would I go about writing a functional test to test that the CSV file is created properly and check the contents of it?
There are probably many ways. Here's a version that won't make you jump through browser hoops
Extract the creation of a CSV to another non-controller class
In your controller test, assert your CreatesCSV receives the right parameters
Test your CreatesCSV class by writing its output to a file, then reading it in and verifying the hash is correct.

Why is there Rails.rb files all over the place?

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.

Cucumber to test Email Parsing?

this is my first stackoverflow question.
I am building an app that gets posted an email from SendGrid, which I then want to parse in a delayed job.
My concern is how do I QA this. I have been reading about Cucumber and that sounds like a good solution but I can't figure out the end to end test flow.
Here's where I am so far.
I want to have a large list of TXT files that include various types of email body's
I then iterate through each txt file, and make sure that when passed to a method in my lib directory /mailingjob.rb, that what is returned matches something defined in cucumber.
So what I have so far is something like:
expected = File.open('???/mail1.txt', 'r') do |f|
f.read
end
That's where I'm starting. So if you can, please help me understand the following:
Where should all these TXT files be located in the rails project directory?
One e2e example showing how to grab a local text file, pass it to a method in the /lib directory, and then make sure what is returned equals what is set for that file path.
Thank you thank you for any help you can provide
I would probably store these files somewhere in your test or spec directory (depending on what testing framework you use; you mentioned Cucumber, which I'm not super familiar with, but I think it uses a directory called features). Really, you could put them anywhere you want, but some subdirectory of your test directory makes sense.
If you're not familiar with testing with Cucumber, I recommend Railscast episode 155 and episode 159 to get you started. To answer your direct question, you would read the email from the text file as such
email_text = File.read("#{Rails.root}/test/path/to/email.txt")
Rails.root always refers to the root directory for your project, and makes it easy to build paths to other files or folders.

Rail 3 custom renderer: where do put this code?

I'm following along with Yehuda's example on how to build a custom renderer for Rails 3, according to this post: http://www.engineyard.com/blog/2010/render-options-in-rails-3/
I've got my code working, but I'm having a hard time figuring out where this code should live. Right now, I've got my code stuck right inside of my controller file. Doing this, everything works. When I move the code to the lib folder, though, I have explicitly 'require' my file in the controller that needs the renderer or it won't work. Yes, the file gets loaded when it sits in the lib folder, automatically. but the code to add the renderer isn't working for some reason, until I do a require on it.
where should I put my code to add the renderer and mime type, so that rails 3 will pick it up and register it for me, without me having to manually require the file in my controller?
I'd put it in an initializer, or in lib and require it in application controller.
In Jose Valim's book, Crafting Rails applications, this is the first chapter. He creates a PDF mime type & renderer using Prawn.
In his example, he created lib/pdf_renderer.rb with this:
require "action_controller"
Mime::Type.register "application/pdf", :pdf
Since lib is no longer autoloaded, you'll either have to autoload lib or specifically require this file where you want to use it.
An initializer might also be appropriate here.
i did some more digging around on this based on the suggestions here.
i found a "mime_types" initializer was already in our code base. i think this is created by rails, by default. it had several commented out examples in it. so i added my custom mime type to this file.
i also decided to use an initializer for the custom renderer so that it's automatically loaded and available with the app. this way i don't have to remember to include it in the various places i need it. i can just respond_to the format i created, and send the data down.
thanks for the tips, everyone.

Resources