-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #438 from cedarcode/sr--add-json-serializer
Extract shared logic for serializing credential options into a new `JSONSerializer` module
- Loading branch information
Showing
7 changed files
with
84 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
module WebAuthn | ||
module JSONSerializer | ||
# Argument wildcard for Ruby on Rails controller automatic object JSON serialization | ||
def as_json(*) | ||
to_hash_with_camelized_keys | ||
end | ||
|
||
private | ||
|
||
def to_hash_with_camelized_keys | ||
attributes.each_with_object({}) do |attribute_name, hash| | ||
value = send(attribute_name) | ||
|
||
if value.respond_to?(:as_json) | ||
hash[camelize(attribute_name)] = value.as_json | ||
elsif value | ||
hash[camelize(attribute_name)] = deep_camelize_keys(value) | ||
end | ||
end | ||
end | ||
|
||
def deep_camelize_keys(object) | ||
case object | ||
when Hash | ||
object.each_with_object({}) do |(key, value), result| | ||
result[camelize(key)] = deep_camelize_keys(value) | ||
end | ||
when Array | ||
object.map { |element| deep_camelize_keys(element) } | ||
else | ||
object | ||
end | ||
end | ||
|
||
def camelize(term) | ||
first_term, *rest = term.to_s.split('_') | ||
|
||
[first_term, *rest.map(&:capitalize)].join.to_sym | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters