Skip to content

Commit cc009eb

Browse files
author
Alex Slepak
authored
Add hash support to all models that represent parsed objects (#152)
* add to_h method to each model that implements to_json * fix linter issues * move has_provider out of workos module * include new file * include hash_provider * add a to_json definition * include kernel to give sorbet access to raise * update lint warnings * move hash_provider to workos folder
1 parent db5e6b8 commit cc009eb

13 files changed

Lines changed: 31 additions & 8 deletions

lib/workos.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'workos/version'
55
require 'sorbet-runtime'
66
require 'json'
7+
require 'workos/hash_provider'
78

89
# Use the WorkOS module to authenticate your
910
# requests to the WorkOS API. The gem will read

lib/workos/challenge.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module WorkOS
66
# a WorkOS DirectoryUser resource. This class is not meant to be instantiated
77
# in DirectoryUser space, and is instantiated internally but exposed.
88
class Challenge
9+
include HashProvider
910
extend T::Sig
1011

1112
attr_accessor :id, :object, :expires_at, :code, :authentication_factor_id, :updated_at, :created_at

lib/workos/connection.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module WorkOS
77
# in user space, and is instantiated internally but exposed.
88
# Note: status is deprecated - use state instead
99
class Connection
10+
include HashProvider
1011
extend T::Sig
1112

1213
attr_accessor :id, :name, :connection_type, :domains, :organization_id,

lib/workos/directory.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module WorkOS
66
# a WorkOS Directory resource. This class is not meant to be instantiated
77
# in user space, and is instantiated internally but exposed.
88
class Directory
9+
include HashProvider
910
extend T::Sig
1011

1112
attr_accessor :id, :domain, :name, :type, :state, :organization_id, :created_at, :updated_at

lib/workos/directory_group.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module WorkOS
66
# a WorkOS DirectoryGroup resource. This class is not meant to be instantiated
77
# in user space, and is instantiated internally but exposed.
88
class DirectoryGroup < DeprecatedHashWrapper
9+
include HashProvider
910
extend T::Sig
1011

1112
attr_accessor :id, :name, :custom_attributes, :raw_attributes
@@ -27,10 +28,6 @@ def to_json(*)
2728
}
2829
end
2930

30-
def to_h
31-
to_json
32-
end
33-
3431
private
3532

3633
sig do

lib/workos/directory_user.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module WorkOS
66
# a WorkOS DirectoryUser resource. This class is not meant to be instantiated
77
# in DirectoryUser space, and is instantiated internally but exposed.
88
class DirectoryUser < DeprecatedHashWrapper
9+
include HashProvider
910
extend T::Sig
1011

1112
attr_accessor :id, :idp_id, :emails, :first_name, :last_name, :username, :state,
@@ -46,10 +47,6 @@ def to_json(*)
4647
}
4748
end
4849

49-
def to_h
50-
to_json
51-
end
52-
5350
private
5451

5552
sig do

lib/workos/factor.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module WorkOS
66
# a WorkOS DirectoryUser resource. This class is not meant to be instantiated
77
# in DirectoryUser space, and is instantiated internally but exposed.
88
class Factor
9+
include HashProvider
910
# rubocop:disable Metrics/AbcSize
1011
extend T::Sig
1112
attr_accessor :id, :environment_id, :object, :type, :sms, :totp, :updated_at, :created_at

lib/workos/hash_provider.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
# typed: true
3+
4+
module WorkOS
5+
# Module to include an explicit method for converting a model into a Hash containing
6+
# its attributes. Default implementation will simply call to_json. Individual classes
7+
# may override.
8+
module HashProvider
9+
include Kernel
10+
11+
def to_json(*)
12+
raise 'Must be implemented by including class.'
13+
end
14+
15+
def to_h
16+
to_json
17+
end
18+
end
19+
end

lib/workos/organization.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module WorkOS
66
# a WorkOS Organization resource. This class is not meant to be instantiated
77
# in user space, and is instantiated internally but exposed.
88
class Organization
9+
include HashProvider
910
extend T::Sig
1011

1112
attr_accessor :id, :domains, :name, :allow_profiles_outside_organization, :created_at, :updated_at

lib/workos/profile.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module WorkOS
88
# is not meant to be instantiated in user space, and
99
# is instantiated internally but exposed.
1010
class Profile
11+
include HashProvider
1112
extend T::Sig
1213

1314
sig { returns(String) }

0 commit comments

Comments
 (0)