Swagger documentation tools for a Rails API application - swagger-ui

I googled a little bit to find a proper and easy to implement way to generate Swagger documentation for an existing Rails API app. To be short, there are 2 ways of implementation: either via controllers, models or via Rspec controller/request specs. The list is not too long and the choice is not so easy to make:
swagger-docs gem, - the oldest one. Pros: implementation is to be
done in controllers. Cons: does not support Swagger V2 version
and limited to 1.2 only.
swagger-blocks gem, - improved version of swagger-docs, supports Swagger V2 version, implementation is to be done in controllers, models. But I couldn't make it work, Swagger Editor could not parse the generated json file.
rswag gem: extends rspec-rails "request specs" with a Swagger-based DSL. Personally, I found it really difficult to implement in an existing Rails app for 2 reasons:
you will have to modify existing request specs
request specs look really weird and the syntax is not RSpec-ish.
rspec-rails-swagger gem: implementation via request specs. The same cons as above.
Does anybody know other gems to generate Swagger documentation for an existing Rails API app ? Any suggestions are welcome ! Thank you.

The solution I came to is as to use rswag gem and rspec-rails-swagger gem
- install rswag gem by adding the following in your Gemfile:
#Gemfile
gem 'rswag-api'
gem 'rswag-ui'
group :development, :test do
gem 'rspec-rails', '~> 3.8.1'
gem 'rspec-rails-swagger', '~> 0.1.5'
...
end
run `bundle install``
run rails g rswag:install to generate swagger_helper.rb
to create a new request spec with rspec-rails-swagger, run rails generate rspec:swagger PostsController(adapt the name to your won controller you want to write the spec).
write some specs as explained in rspec-rails-swagger README
run bundle exec rake swagger to generate a swagger.json file.
mount RSwag API and RSwag UI engines by adding the following lines to your routes.rb file:
#../config/routes.rb
Rails.application.routes.draw do
mount Rswag::Ui::Engine => '/api-docs'
mount Rswag::Api::Engine => '/api-docs'
...#other routes come here
end
start up your rails server with rails s
navigate to localhost:3000/api-docs to see the generated Swagger documentation.
Note: it works pretty good and replies to the client requirements, i.e.:
generate Swagger 2.0 compatible documentation
be able to execute requests directly from Swagger interface to see the real data
I removed rswag-specs gem from Gemfile because it could not validate response schema returned in JSON API format by active_model_serializers gem I use in my Rails API app. I had always to:
generate the docs
comment out failing tests
then uncomment failing tests in case I need to generate some new documentation, that was really hard to maintain.
Now request specs are validated by RSpec and rspec-rails-swagger at the same time without hassle.
Hope this helps.

Our working solution is to use:
swagger-blocks gem to generate the Swagger JSON
Swagger UI Console Chrome
Extension to provide the Swagger UI

Related

Rails 4.2 - Grape - IOError: Not opened for reading

I'm building an API with Grape on Rails 4.2. Here's a link to the repo on GitHub.
On the frontend, I have a JavaScript application built with EmberJS. Here's the link to the repo on GitHub as well.
I have updated the following gems so that I can format the responses of my API following JSON API standard, which appears to be required to update to Ember 1.13, then to Ember 2.0.
gem "active_model_serializers", '0.10.0.rc2'
gem "grape"
gem "grape-active_model_serializers", :git => 'https://github.com/jrhe/grape-active_model_serializers.git'
After that, I've got the following error when I call the API: IOError: Not opened for reading
According to the information I gathered here and there, I suspect this is a conflict with how I format JSON.
In default.rb, the file that inherit all API controllers, when I comment this line:
formatter :json, Grape::Formatter::ActiveModelSerializers
I don't have any error anymore, but obviously the response isn't serialized.
My questions are:
Do you have any idea how I can solve this?
Do you think it's relevant to use Grape to build the API in my Rails app? With Rails 5 coming, isn't that better to user rails-api and rewrite the whole app? I mean, I'm starting, it's maybe the right time to do that...
Let me know if you need more information.
Thanks in advance for your help.
I ended up rewriting the whole API using jsonapi-resources.

XML-based RESTful API

I've created a basic RESTful service with rails g scaffold and it fails to accept XML request sent by ActiveResource (works fine with JSON POST requests and XML GET requests). It fails with ActionController::ParameterMissing so obviously it has something to do with whitelisting but I can't figure out what exactly I should send. Any suggestions?
It seems that XML support was removed in Rails 4 so actionpack-xml_parser gem is required to deal with XML requests.
Add to Gemfile
gem "actionpack-xml_parser"
execute bundle install and configure the rack middleware by adding the following line to config/application.rb.
config.middleware.insert_after ActionDispatch::ParamsParser, ActionDispatch::XmlParamsParser
As per instruction from sikachu/actionpack-xml_parser
nJoy!

What is the easiest way to work with Google Docs API in Ruby on Rails?

What is the easiest way to work with the Google Docs API in a Ruby on Rails application? Is there a gem available?
I did find this page: Google Data on Rails, is it the most up to date resource?
The google data rails on page is really out of date, at least in a ruby 1.9.3 environment its non useful.
Having a play with the google_drive gem now....
https://github.com/gimite/google-drive-ruby#readme
I was able to get it to work by simply installing it and pasting the readme code into a file. What more can you ask for!?
You can also Bearer universal API client available in Ruby.
This way you can query Google Docs API very easily and don't even have to worry about OAuth or refresh tokens: https://www.bearer.sh/integrations/33/google-docs-api
The Google Docs developers have now released their own gem to work with the API. Here's a guide to getting started: https://developers.google.com/sheets/api/quickstart/ruby
And the gem itself can be downloaded in the normal way from Rubygems: gem install google-api-client
There's more information in the Readme for the gem's source code repository: https://github.com/google/google-api-ruby-client
you can use this gem omniauth-google-oauth2. Make configuration with consumer-key and secret-key. Add gem in Gemfile. Follow github.

regenerating rspec files

how can I generate rspec on-demand?
the thing was my rspec files were already automatically generated by the "rails generate controller" command. Then I manually deleted those files in hope that there should be a command which I can use to regenerate those files.
What do I do to regenerate those deleted rspec files without firing "rails generate controller" again?
I have tried some command I was suggested by some blog:
$ rails generate rspec_controller pages --skip-migration --skip-fixture --skip
Could not find generator rspec_controller.
but it didn't work for me.
any advice would be really appreciated!
In the root folder of your app:
rails generate controller -h
It will show you the usage instructions.
rspec_controller is deprecated in favour of the standard controller generator in Rails. It works now by you including rspec-rails within your Gemfile like this:
group :development, :test do
gem 'rspec-rails'
end
This then will load this file, which customizes what tools the Rails controller generator uses with these two lines.
This is a bad idea, for two reasons:
You normally shouldn't be autogenerating your spec files: you need to think about exactly what you want to specify.
Controller specs should normally not be written at all. Use Cucumber scenarios instead -- they're much less painful and much better at testing behavior (controller specs are too dependent on implementation).

What's your solution for 'contact forms' in Ruby on Rails applications?

With a quick Google search, one can find literally hundreds of examples for contact forms using PHP and/or JavaScript, but there don't seem to be any "ready-made" contact forms for Ruby on Rails. Do they exist? What do you use for contact forms in your Ruby on Rails apps?
Personally, I use an ActiveRecord model for mine as I like to keep a store of messages being sent, and then use an ActionMailer after saving a record to send an email.
Alternatively there is a gem called MailForm which allows you to build a form model without a database table, which will work with other gems such as Formtastic.
As for ready-made contact forms, I don't know of any (though they may well exist) as it's not particularly time-consuming to build one from scratch.
I was running into the same issue with wanting an easy to use out of the box Contact Form, but wasn't able to find one. So I've written ContactUs a Rails Engine that you can easily drop into any Rails 3+ application. I tried to keep it dead simple and as easily configurable as possible. It does require the Formtastic gem since I wanted an easy way to hook into peoples existing form styles though.
To install the Engine add the contact_us gem to your Gemfile:
gem 'contact_us', '~> 0.1.3'
Run bundle and the install rake task:
$ bundle
$ bundle exec rake contact_us:install
Then just modify the generated initializer in /config/initializers/contact_us.rb to have the email you want the form submissions sent to.

Resources