Skip to content

Commit

Permalink
Require JSON (#65)
Browse files Browse the repository at this point in the history
* 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
croaky authored Dec 9, 2020
1 parent 8296e5b commit 179b6ab
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 6 deletions.
1 change: 1 addition & 0 deletions lib/workos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions lib/workos/connection.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions lib/workos/organization.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions lib/workos/profile.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 179b6ab

Please sign in to comment.