1

I've created a gem. You can see the full source code here: https://github.com/agilie/instagram_api_gem (in any case, not advertising)

However, when I use it in the Rails project

# Gemfile
gem 'instagram_api_client'

it doesn't work untill a manually require it somewhere in the initializers like

require 'instagram_api'

Why does this happen? I want it to work without any manual requires like other rails gems work.

Sergey Mell
  • 7,780
  • 1
  • 26
  • 50
  • Where this gem is located? Have you uploaded it to rubygems? Have you re-run `bundle` after you have the gem added to it? – Aleksei Matiushkin Dec 01 '17 at 09:42
  • Yes, it is on rubygems (https://rubygems.org/gems/instagram_api_client). I've tried both ways - from rubygems and directly from repository. The result is the same. Sure I did bundle after adding. – Sergey Mell Dec 01 '17 at 09:43
  • 2
    You should follow RubyGems' naming conventions. From the [make your own gem](http://guides.rubygems.org/make-your-own-gem/) guide: _"Code for your package is placed within the **lib** directory. The convention is to have **one** Ruby file with the **same name** as your gem, since that gets loaded [...]"_ – Stefan Dec 01 '17 at 10:08
  • Thanks a lot. Seems I've missed this recommendation – Sergey Mell Dec 01 '17 at 10:11

1 Answers1

3

It does not conform Rails autoloading rules. Create a file instagram_api_client.rb with a content

require 'instagram_api'

in the top level of lib folder. That should do the trick.

For gems, the name of the file to be loaded automatically should be exactly equal to the name of the gem.


Or, as suggested by @TomLord, one might simply specify

gem 'instagram_api_client', require: 'instagram_api'

in the Gemfile itself.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160