2

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?

Gun
  • 61
  • 1
  • 9
  • It seems that it is not even requesting for the resource. I tried with a local API of my own, which is a simple http request in the json format. It shows them same response as null. Please let me know, where is the fault in the implementation. Its used as it is given in examples of ActiveResource. – Gun Jul 20 '12 at 23:10

1 Answers1

1

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.

Fivell
  • 11,829
  • 3
  • 61
  • 99