Skip to content

Commit

Permalink
Add a temporary backwards compatibility module for ::Ruby::OpenAI. Th…
Browse files Browse the repository at this point in the history
…is commit can be removed when backwards compatibility is no longer needed.
  • Loading branch information
kmcphillips committed Feb 14, 2023
1 parent 8572b69 commit 3eb5ce2
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ and require with:
require "openai"
```

## Upgrading

The `::Ruby::OpenAI` module has been removed and all classes have been moved under the top level `::OpenAI` module.

However, a transitional `::Ruby::OpenAI` module has been added with aliases to the new location of the classes and constants. For now this compatibility layer is available when the gem is loaded with `require 'ruby/openai'` or by default with bundler, but are _not_ available when the gem is loaded with `require 'openai'` or `gem 'ruby-openai', require: 'openai'`. This will be removed in future versions.


## Usage

- Get your API key from [https://beta.openai.com/account/api-keys](https://beta.openai.com/account/api-keys)
Expand Down
9 changes: 9 additions & 0 deletions lib/openai/compatibility.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Ruby
module OpenAI
VERSION = ::OpenAI::VERSION

Error = ::OpenAI::Error
ConfigurationError = ::OpenAI::ConfigurationError
Configuration = ::OpenAI::Configuration
end
end
1 change: 1 addition & 0 deletions lib/ruby/openai.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
require_relative "../openai"
require_relative "../openai/compatibility"
33 changes: 33 additions & 0 deletions spec/compatibility_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
RSpec.describe "compatibility" do
context "for moved constants" do
describe "::Ruby::OpenAI::VERSION" do
it "is mapped to ::OpenAI::VERSION" do
expect(::Ruby::OpenAI::VERSION).to eq(::OpenAI::VERSION)
end
end

describe "::Ruby::OpenAI::Error" do
it "is mapped to ::OpenAI::Error" do
expect(::Ruby::OpenAI::Error).to eq(::OpenAI::Error)
expect(::Ruby::OpenAI::Error.new).to be_a(::OpenAI::Error)
expect(::OpenAI::Error.new).to be_a(::Ruby::OpenAI::Error)
end
end

describe "::Ruby::OpenAI::ConfigurationError" do
it "is mapped to ::OpenAI::ConfigurationError" do
expect(::Ruby::OpenAI::ConfigurationError).to eq(::OpenAI::ConfigurationError)
expect(::Ruby::OpenAI::ConfigurationError.new).to be_a(::OpenAI::ConfigurationError)
expect(::OpenAI::ConfigurationError.new).to be_a(::Ruby::OpenAI::ConfigurationError)
end
end

describe "::Ruby::OpenAI::Configuration" do
it "is mapped to ::OpenAI::Configuration" do
expect(::Ruby::OpenAI::Configuration).to eq(::OpenAI::Configuration)
expect(::Ruby::OpenAI::Configuration.new).to be_a(::OpenAI::Configuration)
expect(::OpenAI::Configuration.new).to be_a(::Ruby::OpenAI::Configuration)
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "bundler/setup"
require "dotenv/load"
require "openai"
require "openai/compatibility"
require "vcr"

Dir[File.expand_path("spec/support/**/*.rb")].sort.each { |f| require f }
Expand Down

0 comments on commit 3eb5ce2

Please sign in to comment.