Skip to content

Instantly share code, notes, and snippets.

@johnhebron
Last active January 19, 2024 23:27
Show Gist options
  • Save johnhebron/3c6779b1d10d29015013e2fd03db2360 to your computer and use it in GitHub Desktop.
Save johnhebron/3c6779b1d10d29015013e2fd03db2360 to your computer and use it in GitHub Desktop.
Testing plex-api-spec changes via generated Ruby gem

How I test my plex-api-spec changes in Ruby

When making updates to the spec file at https://github.com/LukeHagar/plex-api-spec, I like to test those changes by generating a Ruby gem and using it in a little Ruby script.

This gist is to document how I perform that testing. You can also check out my first pull request which is the basis for this gist.

Step 1: I Make a PR In plex-api-spec Repo

A. Update plex-api-spec

First, I edit the /pms/paths/xxx file to add a new response format, etc.

B. Dereference spec

I then use the swagger-cli bundle command to dereference the pms paths.

swagger-cli bundle --dereference pms/pms-spec.yaml -t yaml -o plex-media-server-spec-dereferenced.yaml

Step 2: I Generate a Ruby SDK and Test Changes

To prove that the Ruby SDK generation is working and includes the new changes, I am building a Ruby gem and using it in a test project.

Setup Test Ruby Project with Built Gem Files

A. Build Ruby gem files using the command:

openapi-generator generate -i plex-media-server-spec-dereferenced.yaml -g ruby -o ../pms-api-ruby

B. Create a test project folder with a Gemfile and a main.rb C. Reference the gem files in your test project Gemfile

source 'https://rubygems.org'

gem 'openapi_client', path: '/path/to/pms-api-ruby'

D. Include gem in project

# Load the gem
require 'bundler/setup'
require 'openapi_client'

E. Set up the OpenapiClient configuration

OpenapiClient.configure do |config|
  config.host = '192.168.x.x:32400'
  config.debugging = true
  # Configure API key authorization: accessToken
  config.api_key['accessToken'] = 'xxxxxxxxxxxxxxxxxxxxxx'
end

Testing Changes

This part will vary from PR to PR, depending on what feature is being tested.

An example for checking the getLibraries endpoing might look like:

api_instance = OpenapiClient::LibraryApi.new

begin
  libraries = api_instance.get_libraries
rescue OpenapiClient::ApiError => e
  puts "Exception when calling LibraryApi->get_libraries: #{e}"
end

Step 3: I Show Proof Of Changes

To show proof of changes, I will copy/paste before/after output of the code to show that, after the changes, the response using the Ruby gem is now updated.

An example of adding the response format for the getLibraries endpoint might look like:

Before Changes Before the change, the output showed nil for the Data value and a status code of 200

D, [2024-01-18T15:23:33.968923 #30434] DEBUG -- : Calling API: LibraryApi.get_libraries ...
GET /library/sections HTTP/1.1

HTTP/1.1 200 OK
D, [2024-01-18T15:23:34.034092 #30434] DEBUG -- : API called: LibraryApi#get_libraries
Data: nil
Status code: 200

After Changes After the change, the output showed the expected body data for the Data value and a status code of 200

D, [2024-01-18T15:23:33.968923 #30434] DEBUG -- : Calling API: LibraryApi.get_libraries ...
GET /library/sections HTTP/1.1

HTTP/1.1 200 OK
D, [2024-01-18T15:23:34.034092 #30434] DEBUG -- : API called: LibraryApi#get_libraries
Data: #<OpenapiClient::GetLibraries200Response:0x000000015944d810 @media_container=# ...... shortened for brevity, but the data is here! >
Status code: 200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment