Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require JSON #65

Merged
merged 2 commits into from
Dec 9, 2020
Merged

Require JSON #65

merged 2 commits into from
Dec 9, 2020

Conversation

croaky
Copy link
Contributor

@croaky croaky commented Dec 9, 2020

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:

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:

{}.to_json # undefined method `to_json' for {}:Hash (NoMethodError)
JSON.parse("{}") # uninitialized constant JSON (NameError)

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)
```
@croaky croaky requested a review from a team as a code owner December 9, 2020 18:49
Copy link
Contributor

@blairworkos blairworkos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@willmanduffy willmanduffy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely works to resolve this issue. @croaky, I'm curious, is your use case also resolved if we put the require inside of lib/workos.rb once? That way all files in the WorkOS module should get access too.

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'`.
@croaky
Copy link
Contributor Author

croaky commented Dec 9, 2020

Hi @willmanduffy, definitely! I pushed that change as a follow-up commit to the branch and added some detail about what I believe is mechanically happening to the commit message.

@blairworkos blairworkos merged commit 179b6ab into workos:master Dec 9, 2020
blairworkos pushed a commit that referenced this pull request Jan 29, 2021
* 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'`.
blairworkos pushed a commit that referenced this pull request Mar 3, 2021
* 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'`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants