mwunsch / weary

A framework and DSL for building RESTful web service clients
MIT License
482 stars 23 forks source link

Make domain dynamic #43

Open manuelmeurer opened 10 years ago

manuelmeurer commented 10 years ago

I would like to be able to set the domain of my client dynamically because I want to configure it to go to another host in development. I tried this

class MyApp::Client < Weary::Client
  def self.domain
    "http://#{MyApp.configuration.api_host}/#{MyApp.configuration.api_version}"
  end
end

but since the resources are created as soon as the class is loaded, the values of MyApp.configuration.api_host and MyApp.configuration.api_version which are set at that time are used.

Any idea how to fix this? I am happy to implement it myself but it's not immediately obvious to me how to do it without rewriting the whole logic.

mwunsch commented 10 years ago

The domain is really only there in the service a router, and even then, I believe it can be blank. If you want to make any part of the URL dynamic, simply make sure to mark it as such in the client:

get :something, "http://{api_host}/{api_version}/my_resource"

And don't set the domain at all. If that doesn't suit you, then maybe it might be worth being able to pass a lambda to domain...

manuelmeurer commented 10 years ago

Yeah, I figured I could leave out the domain and add it when defining the resources but that gets very repetitive and isn't very DRY. Having something like this would be great:

class MyApp::Client < Weary::Client
  domain { "http://#{MyApp.configuration.api_host}/#{MyApp.configuration.api_version}" }
end
manuelmeurer commented 10 years ago

Would you accept a PR that implements this? If so, I can take a crack at it.

mwunsch commented 10 years ago

I have some inclinations about how this should be implemented, but I'd be open to taking a look at a PR.

manuelmeurer commented 10 years ago

Well, if you have time to work on it, that would be just great! :smile_cat:

manuelmeurer commented 10 years ago

Can you tell me how you would implement this? Then I can prepare a PR.