2

In my Rails 5 API project I am consuming SOAP services. With the use of the Savon gem, version 2, I parse the response from any SOAP web service to JSON format.

In my Rails project I am creating a facade for multiple SOAP services. Let's say 4 SOAP services that have 90% similar response format/structure. I would like my facade to be accepting and sending out the information from the services in JSON API format.

I found some solutions, but not an ideal and effective one:

  1. Parsing the JSON/SOAP response from the SOAP service into models/objects, and then outputting this to JSON API through existing libraries (e.g. jsonapi-resources gem)
  2. Building a serializer from scratch within Rails
  3. Use an ESB

The first option seems to be overkill and error prone.

The second option seems to be a very time consuming project, and it'll also be prone to a lot of errors if I have to do this by myself in a short amount of time.

The third option is a no go due to budget.

So my question is, do there exist tools out there that can assist me? I have been doing a great deal of research, but have not been able to find anything that seems to be a solution to my problem or fulfilling my needs.

Solution so far

I found it most pragmatic to go with option 1. I am doing something similar to the following:

# app/controllers/api/v1/some_resources_controller.rb
...
# Retrieve response from service
response = fetch_resources_from_service(query)
# Pick out the values from the response that we want to output
resource_params = strip_response(reponse, whitelist: [:id, :title, :date, ...]
# Create a model object from these stripped/whitelisted params
resource = Api::V1::SomeResource.new(resource_params)
# Render in JSON API format (with active_model_serializer)
render json: resource, adapter: :jsonapi, serializer: Api::V1::SomeResourceSerializer
maikovich
  • 368
  • 2
  • 3
  • 16
  • Maybe this [gist](https://gist.github.com/henrik/1621655) can help you as a starting point? I would probably start with creating some test cases, then create a service and work with a hash (which should hopefully cover most of the tests), then add custom functionality as you move on. – Julia Will Aug 02 '16 at 07:48
  • Option 1 seems to be the best one because you can leverage existing tools. If you don't need the models in your database, you can set up a periodic task with [clockwork](http://rubygems.org/gems/clockwork) or [whenever](http://rubygems.org/gems/whenever) that will delete them. – Ilya Vassilevsky Aug 02 '16 at 21:39
  • I'm doing option 1 now actually, it works better than expected and didn't take that much time. I'll edit my post with my progress.. – maikovich Aug 02 '16 at 21:41

1 Answers1

0

I'm not a Ruby person but what about a ruby library for simply converting xml to json? It seems like you're building something like a reverse proxy with a SOAP to json converter built in. If so it could be tough to keep up with the backend API changes and your object models. Using something like https://github.com/jnunemaker/crack could free you from creating object models for every endpoint.

Also found a similar SO question Ruby XML to JSON Converter?

Community
  • 1
  • 1
Shawn K
  • 779
  • 5
  • 13