ruby-on-railsruby-on-rails-3restactiveresourcefreebase

debugging activeresource


I'm trying to get activeresource (on Rails 3.2) working with the Freebase API and I haven't had much luck yet. How can I debug rails to see what's going on and make sure the request is well formed?

I think the suffixed .json is causing the failure but I don't know how to check what's going on?

Error:

ActiveResource::ResourceNotFound: Failed.  Response code = 404.  Response message = Not Found.

Code:

class Freebase < ActiveResource::Base
  class << self # also tried without this, same result
    def element_path(id, prefix_options = {}, query_options = nil)
      prefix_options, query_options = split_options(prefix_options) if query_options.nil?
      "#{prefix(prefix_options)}#{collection_name}/#{id}#{query_string(query_options)}"
    end

    def collection_path(prefix_options = {}, query_options = nil)
      prefix_options, query_options = split_options(prefix_options) if query_options.nil?
      "#{prefix(prefix_options)}#{collection_name}#{query_string(query_options)}"
    end
  end


  self.site = "https://www.googleapis.com/freebase/v1/"
  self.format = :json

  #https://www.googleapis.com/freebase/v1/search?query=nirvana&indent=true

  #Freebase.get('search', :query => 'nirvana')



end

UPDATE:

Ok so I found two things going wrong...

1) The collection_path I'm trying to supersede doesn't work at all.. it still tacks .json onto every request. 2) https://www.googleapis.com:443/freebase/v1/freebases/search.json?query=nirvana It tacks freebase on afterwards... any ideas?

I also tried this fix:

Remove .xml extension from ActiveResource request

But it didn't remove the JSON suffix either.

UPDATE UPDATE:

Added the suggest update below which gives the correct PATH but now I'm getting

GET https://www.googleapis.com:443/freebase/v1/search/?query=monkey
--> 200 OK 2732 (693.3ms)
NoMethodError: undefined method `collect!' for #<Hash:0x007f9bde674900>

Solution

  • ActiveResource isn't dead, but relative to ActiveRecord I can understand why you'd think so, it is definitely an unloved and underprivileged stepchild.

    You'd probably be better off using something like Faraday_Middleware or HTTPParty. The former is my personal preference. Unless what you're doing is pulling from another Rails app or one that has perfect restful behaviour like Rails (which Freebase doesn't), ActiveResource is usually more trouble than it's worth.

    That being said, you can accomplish what you want without overwriting any class methods by doing:

      self.site = "https://www.googleapis.com/"
      self.format = :json
    
      def self.search(word)
        self.find(:all, :from => "/freebase/v1/search/", :params => { :query => word })
      end