Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Require JSON While writing an example program for #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 #<Hash:0x00007fa22e11b4f8> (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'`.
- Loading branch information