whichdigital / active-rest-client

ActiveRestClient API Client
https://rubygems.org/gems/active_rest_client
MIT License
385 stars 44 forks source link

Simple proxy of find #105

Closed jtmkrueger closed 9 years ago

jtmkrueger commented 9 years ago

I'm trying to proxy find:

class ApiCoalitionProxy < ActiveRestClient::ProxyBase
  get "/coalitions" do
    response = passthrough
    translate(response) do |body|
      result = body['result']
      result
    end
  end

  get "/coalitions/:id" do
    response = passthrough
    translate(response) do |body|
      result = body['result']
      result
    end
  end
end

class ApiCoalition < ActiveRestClient::Base
  proxy ApiCoalitionProxy
  base_url 'https://api-url/'

  get :all, '/coalitions'
  get :find, '/coalitions/:id'
end

The all method works just fine, but I'm not sure what I'm doing wrong with my find method. No attempt to use it is successful.


[14] pry(main)> ApiCoalition.find(1).name
  ActiveRestClient ApiCoalition:/coalitions/1 - Trying to read from cache
  ActiveRestClient ApiCoalition#find - Etag cached copy found with etag "939a57315a9a40365f1a850884cc55b2"
  ActiveRestClient ApiCoalition#find - Requesting https://cleancities-stage.nrel.gov/api/v1/coalitions/1
  ActiveRestClient ApiCoalition#find - Etag copy is the same as the server
  ActiveRestClient ApiCoalition:/coalitions/1 - Writing to cache
  ActiveRestClient (509.0ms) ApiCoalition#find
=> nil
[15] pry(main)> ApiCoalition.all.first.name
  ActiveRestClient ApiCoalition:/coalitions - Trying to read from cache
  ActiveRestClient ApiCoalition#all - Etag cached copy found with etag "e56a1d871df7a6f54cb032fef3c46b6c"
  ActiveRestClient ApiCoalition#all - Requesting https://cleancities-stage.nrel.gov/api/v1/coalitions
  ActiveRestClient ApiCoalition#all - Etag copy is the same as the server
  ActiveRestClient ApiCoalition:/coalitions - Writing to cache
  ActiveRestClient (622.9ms) ApiCoalition#all
=> "Clean Cities-Georgia"

Just doing ApiCoalition.first(1) proves that it's still hidden behind result. What am I doing wrong?

Perhaps expanding the example in the README would benefit others that could run into this.

andyjeffries commented 9 years ago

Have you tried reversing your get lines in the proxy? From memory each get registers a handler and the proxying stops with the first match. So this may work better:

class ApiCoalitionProxy < ActiveRestClient::ProxyBase
  get "/coalitions/:id" do
    # ...
  end

  get "/coalitions" do
    # ...
  end
end

Let me know if that resolves it (and if so I'll make it clearer in the README).

Thanks for reporting the issue.

jtmkrueger commented 9 years ago

Unfortunately, no luck. Here's what I tried:

class ApiCoalitionProxy < ActiveRestClient::ProxyBase
  get "/coalitions/:id" do
    response = passthrough
    translate(response) do |body|
      result = body['result']
      result
    end
  end

  get "/coalitions" do
    response = passthrough
    translate(response) do |body|
      result = body['result']
      result
    end
  end
end

class ApiCoalition < ActiveRestClient::Base
  proxy ApiCoalitionProxy
  base_url 'https://cleancities-stage.nrel.gov/api/v1/'

  get :all, '/coalitions'
  get :find, '/coalitions/:id'
end
jtmkrueger commented 9 years ago

Well, it looks like this is my fault. It was caching in my development environment. Once I turned that off, things started looking right!

andyjeffries commented 9 years ago

LOL. No worries, glad you found the answer (I was wondering what on earth was going on and was going to build a test project to replicate it).