Comments (10)
Ran into a similar problem when we wanted to use the same model with two different endpoints (depending on a user selection). Did some nasty things with class unloading to make this work. It would be nice to have an official solution.
from her.
Did some nasty things with class unloading to make this work.
Can you share this particular solution? I just want to be sure of what youโre talking about.
from her.
I have something like this in my Rails controller:
before_filter :initialize_foo_models
# ...
# FooPlugin.setup does the Her::API.new and .setup(...) things
def initialize_foo_models
if foo?
FooPlugin.setup configs[:foo]
reload_foo_model_classes
elsif bar?
FooPlugin.setup configs[:bar]
reload_foo_model_classes
end
end
def reload_foo_model_classes
foo_lib_path = Gem::Specification.find_by_name('foo_plugin').gem_dir + '/lib/foo_plugin'
[:User, :Address].each do |class_name|
if FooPlugin.const_defined?(class_name)
FooPlugin.send(:remove_const, class_name)
load "#{foo_lib_path}/#{class_name.downcase}.rb"
end
end
end
from her.
Iโm gonna close this issue now for lack of activity. If you guys want to submit a pull request for this feature, youโre more than welcome!
from her.
I'm in the need of something like this now. I would like to be able to do:
admin_api = Her::API.new do |c|
c.use FaradayMiddleware::OAuth2, @admin_token
end
user_api = Her::API.new do |c|
c.use FaradayMiddleware::OAuth2, @user_token
end
User.scoped_api(admin_api).all.first # uses the admin token
User.scoped_api(user_api).all.first # uses the user token
# it should also support relationships:
User.scoped_api(user_api).find(1).profiles.first # /profiles should be retrieved using the scoped api
I've been trying to make this change in Her but apparently this might require a major refactor that separates the models from the apis. Are there any plans to add this feature?
from her.
Another option might be to introduce the idea of a Connection, untying the Faraday options from the Api object. A Connection could then be specified on the same way as my previous comment. Note that this will require an important refactor as well.
from her.
As a workaround you could use the newly introduced Proc
support of use_api
(#166)
from her.
@pencil thanks! That helps, although the app I'm dealing with makes the two types of requests (admin and current user) and I can't find a way to send that information down to the proc :\
from her.
@alfonsocora You would want to have some kind of class attribute on the model or a global variable. In your Proc you would check this to decide what API to use. It's an ugly workaround, but it could work ๐
from her.
That's almost the precise example for using a proc - the need to
dynamically choose an API for use_api.
On Aug 17, 2013 2:28 AM, "Nils Caspar" [email protected] wrote:
@alfonsocora https://github.com/alfonsocora You would want to have some
kind of class attribute on the model or a global variable. In your Proc you
would check this to decide what API to use. It's an ugly workaround, but it
could work [image: ๐]โ
Reply to this email directly or view it on GitHubhttps://github.com//issues/25#issuecomment-22808610
.
from her.
Related Issues (20)
- path_prefix option is not used when belongs_to association is set. HOT 5
- add ability to wrap "where" clause HOT 4
- Support for first, limit, group and order HOT 6
- Support for update_attributes missing? HOT 6
- Support for association extensions HOT 1
- undefined method `map' for nil:NilClass for association HOT 6
- Class-level response parsing? HOT 1
- Scopes on JsonApi model
- Find or Create by ID HOT 2
- Have we considered making this gem more SEO friendly? HOT 4
- Attributes don't play nicely with ActiveModel::Dirty in Rails 6
- Safe Navigation Operator &. don't works on nil objects
- I would like to have find_by! in order to raise an exception instead of a nil HOT 1
- HashWithIndifferentUsage is slow, and requires large ActiveSupport dependency
- ruby 2.7, rails 6.0 and faraday 1.0 HOT 3
- Release version 1.1.1 HOT 1
- her-rb.org website inaccessible. HOT 1
- Organizing a fork of her HOT 1
- Execute rescue_from plus her? HOT 1
- Model.all returns empty array, but response has data HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from her.