Debugging activeresource

I am trying to get an activeresource (on Rails 3.2) that works with the Freebase API, and it still fails. How can I debug rails to find out what is happening and make sure the request is well-formed?

I think the .json suffix is ​​crashing, but I don't know how to check what is happening?

Error:

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

the 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 that two things go wrong ...

1) The collection_path I am trying to replace does not work at all. It still writes .json to every request. 2) https://www.googleapis.com:443/freebase/v1/freebases/search.json?query=nirvana ://www.googleapis.com-00-0043/freebase/v1/freebases/search.json? https://www.googleapis.com:443/freebase/v1/freebases/search.json?query=nirvana After that, he frees the base ... any ideas?

I also tried this fix:

Remove .xml extension from ActiveResource request

But he also did not remove the JSON suffix.

UPDATE UPDATE:

Added a tooltip update below that gives the correct PATH, but now I get

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

ActiveResource is not dead, but with respect to ActiveRecord I can understand why you think so, it is definitely an unloved and low-income password.

You should probably use something like Faraday_Middleware or HTTPParty. The first is my personal preference. If you don’t use what you are doing, pulling from another Rails application or one that has great cool behavior like Rails (which is not in Freebase), ActiveResource is usually more of a problem than it is worth.

With this, you can do what you want without overwriting any class methods:

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

Add ActiveResource::Base.logger = Logger.new(STDERR) to your config / application.rb (Rails 3.x).

You will get output, for example:

 POST http://localhost:3000/freebase.json --> 201 Created 0 (15.8ms) 

This shows the method and response code ...

+14
source

ActiveResource expects the endpoint to return data in a very specific format.

For a long time, we used ActiveResource in my company to interact between applications. Recently, however, we have begun to lean toward HTTParty because it performs much less voodoo magic and tends to be a very small hair extension exercise.

Here is an example of how we are now using HTTParty:

 module CoreResources class Job include HTTParty base_uri Rails.configuration.core_resource_uri basic_auth Rails.configuration.core_resource_user, Rails.configuration.core_resource_password def self.search(entity) get("/api/v1/jobs.json", :query => {:entity_id => entity.id}) end def self.find(id) result = get("/api/v1/jobs/#{id}.json") raise CoreResources::JobNotFound.new if result.response.code == "404" raise "Unexpected response from resource job find: #{result.response.code} #{result.response.to_s}" if result.response.code =~ /^(?:4|5)..$/ result end end end 

The problem with ActiveResource is that it will use specially designed json or xml markup and create instances of ActiveResource objects and nested objects based on them. He was having trouble calling collect because something in the json response had to be formatted as an enumeration and wasn’t (probably the parent node had to be an array or something not sure), which caused it to explode.

With HTTParty, you get a json-parsed collection to work with.

It’s easy enough for me to do this:

 jobs = CoreResources::Job.search(my_entity) puts jobs.inspect # [{ # "id" => 4, # "created_by_id" => 12, # "description" => "I like pie" # }, # { # "id" => 5", # "created_by_id" => 12, # "description" => "Mmm, cake" # }] 

Allows me to access jobs through a simple array / hash construct jobs[0].fetch("description") , unlike ActiveResource: jobs[0].description . ActiveResource runs slower to impose these collections, takes up memory with them unnecessarily, and offers you to duplicate code that should just be served by the endpoint in your ActiveResource model (again, if you use a third-party API, which may not be otherwise of choice, but I never got ARes to interact with third-party APIs).

We ran into many other ActiveResource issues where it makes this pointless dynamic generation of class names based on nested resources from your endpoint, but half the time does it wrong ... It's really just a mess.

Moral of the story: now a lot more HTTParty fan. This endpoint probably just doesn't return the data in the correct format, and unless ActiveResource becomes a hackfest to get it to read it correctly.

+1
source

ActiveResource has a pretty narrow use case. You probably want to use a more general interface for working with Freebase.

Some code from LinkTV Platform API FreeBase can help.

+1
source

NoMethodError: undefined method 'collect!' for #<Hash:0x007f9bde674900> NoMethodError: undefined method 'collect!' for #<Hash:0x007f9bde674900> error, it seems the problem is in rails https://github.com/rails/rails/issues/2318 . I had a similar problem, but the provided hacks did not work, so I had to fine-tune them a bit, you can find my answer here .

+1
source

Source: https://habr.com/ru/post/1399853/


All Articles