Gun
Gun

Reputation: 61

Consume HTTPS REST API in Rails via ActiveResource

I am pretty new with Rails and REST. I want to consume REST API in Rails. I found an easy and standard way with ActiveResource.

API which I want to consume is: https://api.pinterest.com/v2/popular

While accessing this API directly, gives response as: {"message": "Please upgrade your app!", "error": "Authentication failed: Please upgrade your app!"}

Hence, I want same result when I consume it from RAILS.

To do the same, I generated one model as Popular and modified its code as:

class Popular < ActiveResource::Base
  self.site = "https://api.pinterest.com/v2/popular"
  self.format = :json
end

Then, I generated a controller as Populars and modified its code as:

class PopularsController < ApplicationController  
  def index
    @popular = Popular.all
    render :json => @popular
  end
end

Then in routes.rb, I added

resources :populars

while running "http://localhost:3000/populars", it shows 'null' in browser. While expected result is {"message": "Please upgrade your app!", "error": "Authentication failed: Please upgrade your app!"}

Please guide, where I am running wrong.

Please provide Specific guidelines to consume HTTPS REST APIs in Rails. How to consume them with OAuth?

Is there any other better way than consuming it via ActiveResource?

Upvotes: 2

Views: 1121

Answers (1)

Fivell
Fivell

Reputation: 11929

if you type in console

1.9.2p320 :041 > Popular.collection_path
 => "/v2/popular/populars.json" 

So by conventions you have wrong routes on server side.

Also you can enable logger in console with

ActiveResource::Base.logger = Logger.new(STDERR)

And you can see next:

1.9.2p320 :042 > Popular.all
I, [2012-08-08T01:51:36.725766 #1371]  INFO -- : GET https://api.pinterest.com:443/v2/popular/populars.json
I, [2012-08-08T01:51:36.725878 #1371]  INFO -- : --> 404 NOT FOUND 17 (1936.9ms)
 => nil 

So that's why result is nil.

https://github.com/albertopq/oauth-activeresource - try this gem to use activeresource with oauth.

Upvotes: 1

Related Questions