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
Related
I know I'm going to get downvotes for even asking but I honestly have no clue how to do this and any assistance would be appreciated as I have never done this.
I have a client that migrated their blog to HubSpot and would like to have their blog posts displayed on their website. So how do I connect to HubSpot's API to display the blog in their website that I have in a Rails app?
Do I create MVC just for Blog API? Where do I put the URL to connect to?
If you want to connect to an other API the best way to do this is to create service objects. These are plain old Ruby objects (aka PORO) that represent the API as a Ruby object. You are free to use any location you want, but I would stick them in lib/services or app/services. If the provided API uses the Rails (REST) standards than you might be able to use ActiveResource.
You could also look for gems that provide these service objects. A quick RubyGem search finds multiple (unofficial) gems. From witch hubspot-ruby seems the most used and active.
If the gems don't do what you want you could look through the code to find some inspiration and create you own objects. Another option is to fork a project and add the functionality you want.
Here is a link to answer the broader question you're asking: What is the proper "Rails Way" to consume a RESTful web service on another domain?
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.
I'm new to the rails world and am trying to build a app to simply allow me to search things on Amazon and such sites based on the users input.
I've done a little reasearch and it seems the httparty gem is a good place to start? The documents ive found so far though are not the best. They don't really give me a lot of information (where to put the code etc).
Are there any existing tutorials out there or code examples that I can either use or take a look at to give me a better idea of how this thing works?
I'm working on an app like this right now, so let me offer a few thoughts.
First, if you're a complete newcomer to Rails, then as a first step I'd suggest taking a parallel approach to the problem, with two tracks:
Learn about Rails
Learn about how to interact with an API (make requests and parse responses) in Ruby
These are two separate problems, and although you may end up implementing them together, it's best to think about them in isolation first.
For the first one, I'd suggest writing a couple simple apps first to get a feel for how things work. Even only a simple app will require a certain amount of user interaction, possibly saving records to the DB, etc., problems that are separate from consuming data from an API. There are an endless number of tutorials out there on Rails, but just to mention one, you might try Michael Harti's Learn Web Development with Rails as a starting point.
The second point, consuming API data, is distinct from the problems of designing the app itself. To learn more about this aspect of the problem, I'd suggest using a popular API client gem like (as you mentioned) HTTParty, which I use myself. Rather than immediately try to use the HTTParty methods in a Rails app, as an exercise I'd suggest playing around a bit in a console (irb). If you install the gem (gem install httparty) you can then require it (require 'httparty') from the console and immediately make requests and parse responses from the APIs.
E.g.:
irb(main):001:0> require 'httparty'
=> true
irb(main):002:0> response = HTTParty.get('http://twitter.com/statuses/public_timeline.json')
=> ...
Once you've got a bit more familiar with Rails and with accessing an API in ruby, then you could move on to actually building the app itself. Depending on how complex your requests to the API(s) are going to be, you have a few options as to how to structure your API requests in your Rails app:
Make the requests part of the Rails app itself, directly from controller actions. This is really only okay if you are only going to support a very limited number of request types (e.g. simple search) on a limited number of services. Anything more complex and your controller will get fat, which is a no-no in MVC frameworks.
Create a separate ruby class (usually called an API wrapper) to group together methods for making requests and parsing responses from the API. With a gem like HTTParty you can do this just by adding a line include HTTParty in the class, which adds the module to the class. There are lots of examples out there on how to do this, here is one.
Use a dedicated gem for accessing specific APIs, or create one (or more) yourself. There are gems for most popular services, e.g. Twitter, Linkedin, Flickr, etc. See this list of API client gems for more. This takes away a lot of the pain of interacting with an API, but will only work for a subset of all services.
You mention you're thinking of using HTTParty, which I can recommend as I am using it myself. There are alternatives such as Faraday (see this list of HTTP clients for more), but I find for most tasks HTTParty will do fine. The documentation may be a bit sparse, but there are a bunch of examples that you can work from.
Hope that helps!
I'm creating a JavaScript web app. I want to use Ruby on Rails to provide the API to the data in JSON format. I am writing the front-end entirely in JavaScript that will make Ajax calls to the API. This way, I will have a consistent data API when I can write different apps on different platforms.
I'm new to Ruby on Rails and I'm not sure if this is a good approach. If so, could you help me point to a good tutorial to implement the back-end that I just mentioned? If not, please help suggest a good way.
Api example code : https://github.com/pigon-web-services/api-app/blob/master/app/controllers/location_controller.rb
How to use this api with javascript : http://pigon.ws/examples/jsonp?api=locationapi
I didn't work with it yet, but the Rails-API gem by spastorino might be helpful.
Also there is a Railscast on this, so you might want to check this out as a starting point
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.