Home > OS >  The following error is raised when I try to use twilio-ruby gem "warning: LoadError: cannot loa
The following error is raised when I try to use twilio-ruby gem "warning: LoadError: cannot loa

Time:03-10

I am currently attempting to require "twilio-ruby" in a ruby file after adding it to my gemfile and doing $bundle install, however I keep getting the following error message when attempting to load the file in irb < warning: LoadError: cannot load such file -- rack/media_type >

The error pathing leads back to .rvm/rubies/ruby-3.1.1/lib/ruby/3.1.0/irb/init.rb:397

My gemfile currently looks like

source 'https://rubygems.org'

ruby '3.1.1'


group :test do
  gem 'rspec'
  gem 'simplecov', require: false, group: :test
  gem 'simplecov-console', require: false, group: :test
end

group :development, :test do
  gem 'rubocop', '1.20'
  gem 'twilio-ruby', '~> 5.61', '>= 5.61.1'
end

My code look like this:

require 'rubygems'
require 'twilio-ruby'

class SMS

  def send_sms(message)
    account_sid = ENV["TWILIO_SID"]
    auth_token = ENV["TWILIO_AUTH_TOKEN"]

    @client = Twilio::REST::Client.new account_sid, auth_token

        @client.messages.create(
      from: ENV["TWILIO_PHONE_NUMBER"],
      to: ENV['PHONE_NUMBER'],
      body: message
    )
  end

end

I have tried this with and without require 'rubygems' but it keep coming up with the same error

I have done checked and my gem list does include twilio-ruby (5.65.0)

CodePudding user response:

I think you've found a bug in twilio-ruby! Recently there was an update to the gem changing some behaviour in a rack middleware that is part of the gem. It lead to the gem requiring some behaviour from Rack, but in a situation where Rack isn't loaded, like your code above, this fails because Rack isn't a dependency.

I've just made a PR to fix this. To fix this in the meantime, you can downgrade the version of twilio-ruby to 5.63.0, before the previous change was merged, or you can install rack as a dependency.

The library is released every two weeks and the last release was February 24th. So if this fix is approved quickly, it could make it into this release.

  • Related