Getting started consuming web services in a Ruby on Rails 3 application - ruby-on-rails

So I'm getting started learning Rails. Now that Rails 3 is out, I want to stick to learning the Rails 3 way of doing things. One of the things I want to learn how to do is how to consume web services / work with third party REST APIs / create "mashup" applications. I've only done minimal work like this with PHP and pre-built libraries.
Can someone please lead me to some resources, best practices, or give me a quick 101 lesson on how to start working with these types of APIs? What gems should I use? Some sample code to get me started would be much appreciated.
Update: I am specifically trying to use the Google Books API (non-authenticated). Since there is no client library for this API, I'm wondering how other Ruby/Rails developers are working with APIs that don't come with their own Ruby library. That's why I'm looking for a more generic solution to working with "fill in the blank" API.
Update: After some research, I noticed the Net::HTTP library. All the tutorials that talked about it were fairly old. Is this still the best library to use for consuming RESTful web services? Or is there a newer Gem that makes this easier? I just want to know which gem/library I should use, and how to work with the XML/JSON result that is returned.
Update: This presentation was really helpful. It mentions a bunch of different libraries and shows examples of some of the libraries mentioned below: http://www.slideshare.net/pengwynn/json-and-the-apinauts

I'm a pretty big fan of HTTParty.
It's an abstraction layer on top of Net::HTTP with a nice little DSL for consuming web services. Here's a good example of how easy it is to use.
It's not without some warts (lots of dependencies) but it's really the way to go if you're on the consuming side.

I'd recommend REST with Nokogiri:
http://railscasts.com/episodes/190-screen-scraping-with-nokogiri
Nokogiri works well with xml too, not just HTML.

Weary is a really neat DSL for consuming RESTful services.
Clearly inspired by HTTParty but a bit newer and a bit more concise.

Related

Angular 2 and Rails as a separate applications

Who can tell to me how to create a 2 separate applications: rails backend and Angular 2 frontend and config the data exchange between it. Rails app need to work as a json api. How Angular 2 understand recieved data from rails and how we can send the concrete data to concrete component?
Please some links, articles and so on.
Thank you so much!
The Server
I'm not an expert in the field of ruby, but this tutorial looked good to me:
https://www.codementor.io/ruby-on-rails/tutorial/creating-simple-api-with-rails
The Client
Angular 2 belongs more to my strengths:
Follow up to the official tutorial from google to develope an angular2 app. here is the link:
https://angular.io/guide/quickstart
If you really understand how that starter-tutorial works, you might use a quick-start npm package for a faster start. Be warned! You only use a quick-start package, if you understand the plain way angular 2 should be installed!
The Clients API engine
For the API queries between your client (angular2) and your server (ruby) use this tutorial:
https://angular.io/docs/ts/latest/guide/server-communication.html
I would recommend you to develope only with TypeScript. Dont use EcmaScript (JavaScript) for your client! All the tutorials and the whole community are real hardcore typescript fanatics, so don't even try to convert them :D Just follow them! (To explain why, would take me another post...)
Good luck and have a nice start!
This is really at least two questions: 1) how can a static angularjs app consume json data 2) how can a rails server return json data. Both questions are best answered by simply learning the two frameworks. The harder questions are around designing the api and how to authenticate. All that is really too broad for a single answer except to say go and learn about those four concerns.

how to organize outgoing api request urls in rails

I have an application in rails that make request to other microservices using httparty. Other microservices are also written in Rails. Right now, i hard coded all the api urls used in httparty, what will be a more elegant way to organize them?
This is a great question, although it's way too broad to be answered meaningfully here without more specifics on the problem you're solving.
However, if I were you, I'd start by reading the source code for existing API libraries. It's a fantastic way to learn about API design principles from very practical examples in products you likely use every day.
Personally, I've found that the Stripe Ruby SDK (https://github.com/stripe/stripe-ruby specifically, lib/stripe/api_operations), the Slack Ruby SDK (https://github.com/dblock/slack-ruby-client), AWS Ruby SDK (https://github.com/aws/aws-sdk-ruby though this one's pretty big), and Github Ruby SDK (https://github.com/aws/aws-sdk-ruby) are all well designed and worth studying.

RESTful API and rails

Not entirely sure if this is a beginner question, but I can't seem to find any detail information regarding how to use APIs with rails.
For the past months I've been to a few hackathons / interviews where one of the tasks is to use APIs to return some meaingful data and make something cool. I looked online for documentations on how to integrate API to websites using rails, but can't seem to find any good ones.
How exactly do I integrate APIs with rails and where should I put them? Some examples of APIs that I would like to play around with are Zappos and Parse, both are REST APIs
https://parse.com/docs/rest
http://developer.zappos.com/docs/api-documentation/
If there are some links that you would like to share, that would be awesome!!
Anything that lets you send http requests to a remote URL can be used to implement your own wrapper for a restful API, but I personally prefer to use the rest-client gem:
https://github.com/rest-client/rest-client
If you wanted to take a more hands on approach, you can just go straight for the Net:HTTP library in Ruby:
http://ruby-doc.org/stdlib-2.1.3/libdoc/net/http/rdoc/Net/HTTP.html

Using Photobucket api with Ruby on Rails

I am trying to use Photobucket(API) as a image uploading option in my website.
Is there any available gem to do this? or should i use things like REST to achieve this?
Please suggest.
Thanks,
Balan
The only thing I really found was this https://bitbucket.org/photobucket/api-ruby/src/7b69e473fb9fd5b4d37e2ad7f266500e45ef524b/example.rb?at=default. They are using the oauth rubygem to talk to photobucket, still a lot of manual process involved. It is also still using REST.
I would say the easiest way it going ahead and using REST following http://pic.photobucket.com/dev_help/WebHelpPublic/PhotobucketPublicHelp.htm and http://pic.photobucket.com/dev_help/WebHelpPublic/Content/FAQ/FAQOverview.htm to get you started.
I haven't done a REST client in ruby but this gem looks pretty good https://github.com/rest-client/rest-client.
I would recommend, assuming you have the time, making a photobucket ruby gem and wrapping the REST calls in useful class abstractions. It is great for the community, forces you to follow good practices(to a certain extent) and will provide great experience to put on a resume/talk about in an interview.

What's the benefit of using Sinatra instead of RoR if I'm only need a DB and an API

I need to build a web service, for a mobile game, to manage the states of multiplayer games. I need a database and an RESTful API to access it. I'm very familiar with Ruby On Rails and was thinking of using that since I can throw together the DB and API pretty quickly. However, since RoR is a framework for building web pages and I'm not actually building any web pages, it naturally seems like the wrong technology to use even though it would work. As such, I'm considering using Ruby on Sinatra, but I've never used it before and I'll have to kill some time learning it. For you Ruby gurus, is there an advantage to using Sinatra or a disadvantage to using RoR for what I'm trying to accomplish?
Thanks so much in advance for your wisdom!
You know Rails, you don't know Sinatra. Personally I prefer the latter for things like building APIs, but there's nothing stopping you from doing it in Rails, and there's nothing intrinsically wrong with it either. Unless you want to see this as a learning opportunity for getting into Sinatra, I'd say stick with Rails. Here's some links that might be useful btw:
Building APIs With Rails
Building a Platform API on Rails
It probably depends on your API. If you need more than just a bunch of routes then you will have to come up with your own solutions (authentication, ...).
If all you need is some RESTfulness without the added weight, Sinatra is great. All you need to know is what happens in what route and you're fine. See the Sinatra Readme which has all the information to get started.

Resources