Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Basics for implementing a REST api

REST API

  • Please log in to reply
1 reply to this topic

#1 tjsherrill

tjsherrill

    Passenger

  • Members
  • 8 posts

Posted 06 May 2014 - 04:54 AM

I am creating an app that interacts with a REST api.  I am new to implementing with external apis.  

 

I have a couple simple questions:

 

Where would I setup the initial uri?  My ideas are the Application Controller, or an initializer.  

 

Next, I have an id and a token that I have include in all of the api calls.  Where should that be set so that it can be called more later.

 

Finally, do I want the actual api calls in the controller or Model?  My thought is to have an Event class that logs each time one of these calls is made so that I can track everything.

 

Just some general advice would be awesome.

 

many thanks



#2 th3mus1cman

th3mus1cman

    Signalman

  • Members
  • 14 posts
  • LocationRichmond, VA

Posted 03 June 2014 - 02:21 PM

Where would I setup the initial uri?  My ideas are the Application Controller, or an initializer.  

I like putting the base uri for API in the config/environments/* files. That way you can point to different places for each env.

 

Next, I have an id and a token that I have include in all of the api calls.  Where should that be set so that it can be called more later.

I like to store these in ENV vars and then use those to define class constants.

class APIService 
  ID = ENV['MYID']
  SECRET = ENV['MYSECRET']
  BASE_URI = AppName::Application.config.my_api_base_uri
  def users(params = {})
     params = {id: ID, secret: SECRET}.merge(params)
     client.get("#{BASE_URI}/users", params)
  end
end

Finally, do I want the actual api calls in the controller or Model?  My thought is to have an Event class that logs each time one of these calls is made so that I can track everything.

I usually create a separate class that acts as a wrapper around all API calls against an API. 


Rails Developer and Partner at Left + Right






Also tagged with one or more of these keywords: REST, API

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users