Best practice of parsing RESTful WebService response with Rails - ruby-on-rails

I'm developing a JAX-RS application and it's working fine. Now, I want to develop a web service client application using Rails. And my question is which is the best way to parse a RESTful webservice's response in Rails?

It depends on what the service is returning. I'd say that you could use a variety of gems to do it. Lately I've been using HTTParty to get and parse automatically the response of a call to a RESTful API.
Once I get all the data I then map it to a model so that's it's easier to manipulate later on. The way you'll do this mapping will really depend on the type on response you'll get.

Related

How to use Rails as DDP server with Meteor.js client

We have a Rails app that acts HTTP API only. On the client side, Ember.js is currently used. We are not overly impressed by Ember and really like the approach Meteor.js takes. So we'd like to exchange the client side with Meteor.js and communicate with the Rails server via websockets that speak the Data Distribution Protocol (DDP), so we can keep using the models, mailers and controllers in Rails. Implementing server side of DDP should be easy.
However, we're unsure how to make Rails talk websockets. We found Reel, which seems to make it easy to accept websocket requests in a standalone environment. Reel seems great as we'd like to implement the DDP on top of the Celluloid stack anyway. But what about running Reel in the Rails environment? Would we need "rails runner" for that? And we'd like to keep using the existing controllers to dispatch incoming requests (like, to add/change/remove resources). Is that even possible without having the request coming through Rack?
Any input is appreciated.
It's a bit late, but I've implemented DDP in Ruby, you can check it out here:
https://github.com/d-snp/ruby-ddp-server
It includes an implementation of EJSON as well. It's built on top of celluloid-websocket, and can be ran simply as a rack app.
I've made an integration with RethinkDB that can be used as a reference to build your own collections implementation.
https://github.com/d-snp/ruby-ddp-server-rethinkdb
I've also made a sample chat application that can be found here:
https://github.com/d-snp/celluloid-rethinkdb-chat
It's something that I have been longing to do as well, to integrate old "legacy" Rails code. Here is the best way I have found:
Since you would not be using any of Rails router/controller/views, but just the ability to read data and push it to the client, I recommend you use Rails to create JSON apis to the database, and deploy the code, then in Meteor you can consume the data via the http package, this would happen on the server at a regular interval and populate the MongoDB with the normalized data you need, then it would serve the browser client.
I am working on such an application that will keep a normalized version of the data in Mongo, and a relational version of the data in mySql (through Rails) this way I can preserve the legacy Rails functionality that I dont want to rewrite in JS, and get the benefit of Meteor for the one page that I need it most.

Should i use wsdl or json for my ios app?

I am writing an iOS app which will have to fetch some data from a server. Is it better to use WSDL and SOAP to get these data from a web service, or should i use JSON?
JSON is good if you don't have any security stakes. Otherwise, you should use a secured data exchange protocol like SOAP or maybe OAuth.
Honestly where I've worked with JSON alot i would personally recommend it because it's a really easy way to send data and to parse data. If your going to be fetching from a server using mysql or something along those lines I would recommend you go with JSON.

Using a web service in an iOS app

How might I go about using a web service in an iPhone app? For example, if I wanted to use a web service that you can use to convert a value into a different unit, how would I go about doing that? For example: http://www.webqc.org/balance.php
It depends what sort of 'web service' it is. If it is a stateless REST style API, passing data in the URL and/or data encoded Json or XML it couldn't be easier, just use NSURLConnection.
Using examples I found on the web I made an application (server and iOS client) - using the NSURLConnection & NSMutableURLRequest, and encoded/decoded data using YAJL. This was pretty easy to get going.
If you don't want to do this using the core libraries directly- there are some frameworks you can use, e.g. RestKit. I've not used it, but it looks good and comes recommended.
If it is a SOAP style web service, this is a lot more complicated as SOAP services often expose a stateful API.
I should say that the example that you show here is not a web-service, whilst it does come with a way of calling it just using a URL - it returns an html page which makes it hard for you to use the results. I presume that you are more interested in a service that returns results encoded as XML or Json or the like.

Consuming JSON REST API using Active Resource in Rails

I'm new to rails and am in the process of building my first app. The app in question needs to be built on top of an internally built API which is responsible for interfacing with our data layer.
Having read around on Stack Overflow and the web in general it seems as though Active Resource is a pretty neat tool for this however my question is whether it will work for the specific API I am consuming which is:
JSON output
Built in PHP and not Rails (although it is RESTful)
(point number 2 is the one I am most concerned about as, from what I have heard/read so far, Active Resource is primarily designed to consume API's from other Rails apps)
Thanks
JSON output - it will work. Default format is XML, but ActiveResource also supports JSON (PS: you will have to set format to :json somewhere in config).
API is built in PHP - as long as the response is in standardized format so ActiveResource can parse it, it doesn't really care whether the response was generated by Rails, PHP or monkey behind the typewriter :).
As you said, ActiveResource is used primarily as a way for several Rails applications to interact with each other, but it is also meant to be used in the way you intend to.

Designing Web Service Using Ruby on Rails - Mapping ActiveRecord Models

I've put together a RoR application and would now like to publish a RESTful web service for interacting with my application.
I'm not sure where to go exactly, I don't want to simply expose my ActiveRecord models since there is some data on each model that isn't needed or shouldn't be exposed via an API like this. I also don't want to create a SOAP solution.
My application is built using Rails 2.3.5 and I hope to move to Rails 3.0 soon after its released.
I'm basically looking for a way to map my ActiveRecord models to "models" that would be exposed via the web service. Is ActiveResource the correct thing to use? What about ActionWebService?
You can do that through a controller (or controllers). Your RESTful controller actions can define the API for incoming web service requests, and you can render XML or JSON in the response instead of rendering an HTML view.
I'm sure there are more sophisticated ways of doing this, but this is the simple approach.

Resources