Use two stones in one namespace in Rails

I have an application that uses the LinkedIn gem, but I need to move on to using the LinkedIn implementation of OAuth2, which comes in another gem .

I need to support oAuth1 requests in the source stone for existing users, but I need to support OAuth2 for new users heading forward.

The problem is that both of these gems use the LinkedIn namespace for their module names, and depending on the order I include them in my Gemfile, one clobbers the other.

I tried adding require: false to the gemfile like this:

 gem 'linkedin', require: false gem 'linkedin-oauth2', require: false 

But, oddly enough, when I go to the console, the first is still required, and the second is not:

 irb(main):001:0> require 'linkedin' => false irb(main):002:0> require 'linkedin-oauth2' => true 

Is this related to how you work? Is it possible to load only one of these gems each in separate lib files without knocking each other together?

EDIT

I realized that I needed LinkedIn in one of my specification files, which caused it to autoload, but this still did not fix the problem of dumped things.

When I have both stones installed, and I run:

 irb(main):001:0> require 'linkedin' => true irb(main):002:0> ::LinkedIn::Client.new NameError: uninitialized constant Api::QueryHelpers from /Users/me/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/linkedin-1.1.0/lib/linked_in/client.rb:8:in `<class:Client>' irb(main):004:0> require 'linkedin-oauth2' => true 

But if I remove linkedin-oauth2 from my gemfile, it works fine:

 irb(main):002:0> require 'linkedin' => true irb(main):004:0> ::LinkedIn::Client.new => #<LinkedIn::Client:0x007f9eef6d72a8 @consumer_token=nil, @consumer_secret=nil, @consumer_options={}> 

Why is this, especially since linkedin-oauth2 not required in the first example, but an error occurs. Maybe due to the fact that they require support for files in the LinkedIn stone? It seems all the same that this should not affect it.

+5
source share
1 answer

I would not recommend doing this, as it may have some strange side effects, for example, if linkedin-oauth2 refers to ::LinkedIn , but the method for overriding the ruby ​​constant is shown in this answer .

I would change this a bit to prevent the constant warning from being renamed ... Object.send(:remove_const, :Stripe) instead of Stripe = Module.new , as shown in the answer. So an example (not verified):

 require 'linkedin-oauth2' LinkedInOauth2 = LinkedIn Object.send(:remove_const, :LinkedIn) require 'linkedin' 
+1
source

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


All Articles