From 179b6ab74077011c35be8a7cb1de4840bbfc9db8 Mon Sep 17 00:00:00 2001 From: Dan Croak Date: Wed, 9 Dec 2020 13:04:22 -0800 Subject: [PATCH] Require JSON (#65) * Require JSON While writing an example program for https://github.com/workos-inc/workos-ruby/pull/64 I noticed an error when not requiring JSON from the standard library: ``` gems/workos-0.9.1/lib/workos/client.rb:65:in `post_request': undefined method `to_json' for # (NoMethodError) ``` Example: ```ruby require 'date' require 'workos' WorkOS.key = ENV.fetch('WORKOS_API_KEY') event = { action_type: 'r', action: 'user.login_succeeded', actor_id: 'user_01DEQWZNQT8Y47HDPSJKQS1J3F', actor_name: 'WorkOS Ruby Gem Test', group: 'foo-corp.com', latitude: '40.676300', longitude: '-73.949200', location: '65.215.8.114', occurred_at: DateTime.now.iso8601, target_id: 'user_01DEQWZNQT8Y47HDPSJKQS1J3F', target_name: 'WorkOS Ruby Gem Test' } WorkOS::AuditTrail.create_event(event: event) ``` I believe the code worked in my Rails app as a side effect of other dependencies and in the workos-ruby test suite due to `simplecov` gem depending on `json` gem as a development dependency. Files with `JSON` or `to_json` need to `require 'json'` for example programs like the snippets in API reference to work standalone: https://workos.com/docs/reference/audit-trail/event/publish Reduced further: ```ruby {}.to_json # undefined method `to_json' for {}:Hash (NoMethodError) JSON.parse("{}") # uninitialized constant JSON (NameError) ``` * Move JSON require to lib/workos.rb When end users `require 'workos'`, the `lib` directory is added to Ruby's `$LOAD_PATH` by `workos.gemspec`: ```ruby spec.require_paths = ['lib'] ``` https://guides.rubygems.org/specification-reference/#require_paths= In a program typical of the WorkOS docs that begins like this... ``` require 'workos' WorkOS.key = ENV.fetch('WORKOS_API_KEY') ``` ...then, the `lib/workos.rb` file is loaded. This file's `WorkOS` module `autoloads` dependencies such as `WorkOS::Connection`. https://www.rubydoc.info/stdlib/core/Kernel%3Aautoload Previously, only dependencies like `SSO` that loaded `Connection`, which `require 'json'` would require the JSON library. So, continuing the above program... ```ruby event = # ... WorkOS::AuditTrail.create_event(event: event) ``` Was never loading any modules that required `'json'`. --- lib/workos.rb | 1 + lib/workos/connection.rb | 2 -- lib/workos/organization.rb | 2 -- lib/workos/profile.rb | 2 -- 4 files changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/workos.rb b/lib/workos.rb index ba3cd04d..afd87fa1 100644 --- a/lib/workos.rb +++ b/lib/workos.rb @@ -3,6 +3,7 @@ require 'workos/version' require 'sorbet-runtime' +require 'json' # Use the WorkOS module to authenticate your # requests to the WorkOS API. The gem will read diff --git a/lib/workos/connection.rb b/lib/workos/connection.rb index b730044e..60b20202 100644 --- a/lib/workos/connection.rb +++ b/lib/workos/connection.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true # typed: true -require 'json' - module WorkOS # The Connection class provides a lightweight wrapper around # a WorkOS Connection resource. This class is not meant to be instantiated diff --git a/lib/workos/organization.rb b/lib/workos/organization.rb index 19cb0006..2459c1ee 100644 --- a/lib/workos/organization.rb +++ b/lib/workos/organization.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true # typed: true -require 'json' - module WorkOS # The Organization class provides a lightweight wrapper around # a WorkOS Organization resource. This class is not meant to be instantiated diff --git a/lib/workos/profile.rb b/lib/workos/profile.rb index b58bc940..5606e384 100644 --- a/lib/workos/profile.rb +++ b/lib/workos/profile.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true # typed: true -require 'json' - module WorkOS # The Profile class provides a lighweight wrapper around # a normalized response from the various IDPs WorkOS