remi/her

uninitialized constant Her::Middleware::JsonApiParser in Rails app when including a Gem with a dependency on Her

Closed this issue · 2 comments

Ruby Version: 2.3.1
Rails Version: 5.1.4
Her Version: 0.10.0

I have a Gem that includes Her as a dependency in its gemspec like so:

Gem::Specification.new do |spec|
  spec.add_dependency 'her', '~> 0.10.0'
end

In this Gem, I create an initializer at config/initializers/her.rb like so:

  Her::API.setup url: "https://localhost" do |c|
    # Request
    c.use Faraday::Request::UrlEncoded

    # Response
    c.use Her::Middleware::JsonApiParser

    # Adapter
    c.use Faraday::Adapter::NetHttp
  end

When I try to start my rails server, I get the following error:
'block in load_missing_constant': uninitialized constant Her::Middleware::JsonApiParser (NameError)

When I add the line require 'her/middleware/json_api_parser' at the top of the initializer, the problem goes away. Right now, it seems the json_api_parser is not required, but rather autoloaded (see https://github.com/remiprev/her/blob/master/lib/her/middleware.rb#L10).

Is this something I'm doing incorrectly? Or something that should be resolved in the her gem itself?

I run into the same issue trying to include Her::JsonApi::Model in a model in that Gem. It seems this is loaded the same way with autoload: https://github.com/remiprev/her/blob/master/lib/her.rb#L17

Why are the JsonApi features the only ones that are autoloaded?

I think this just may be a misunderstanding on my part with how requiring dependencies works in gems. I'm going to close this issue for now. Sorry about that!