@@ -163,7 +163,7 @@ One of these might be what you are looking for:
163
163
### Version 2.0.x
164
164
165
165
<details >
166
- <summary >2.0.x CHANGELOGs and READMEs </summary >
166
+ <summary >2.0.x CHANGELOG and README </summary >
167
167
168
168
| Version | Release Date | CHANGELOG | README |
169
169
| ---------| --------------| ---------------------------------------| ---------------------------------|
@@ -196,7 +196,8 @@ One of these might be what you are looking for:
196
196
[ 2.0.1-changelog ] : https://gitlab.com/oauth-xx/oauth2/-/blob/main/CHANGELOG.md?ref_type=heads#201---2022-06-22
197
197
[ 2.0.0-changelog ] : https://gitlab.com/oauth-xx/oauth2/-/blob/main/CHANGELOG.md?ref_type=heads#200---2022-06-21
198
198
199
- [ 2.0.10-readme ] : https://gitlab.com/oauth-xx/oauth2/-/blob/v2.0.11/README.md
199
+ [ 2.0.12-readme ] : https://gitlab.com/oauth-xx/oauth2/-/blob/v2.0.12/README.md
200
+ [ 2.0.11-readme ] : https://gitlab.com/oauth-xx/oauth2/-/blob/v2.0.11/README.md
200
201
[ 2.0.10-readme ] : https://gitlab.com/oauth-xx/oauth2/-/blob/v2.0.10/README.md
201
202
[ 2.0.9-readme ] : https://gitlab.com/oauth-xx/oauth2/-/blob/v2.0.9/README.md
202
203
[ 2.0.8-readme ] : https://gitlab.com/oauth-xx/oauth2/-/blob/v2.0.8/README.md
@@ -492,16 +493,91 @@ response.parsed.class.name # => SnakyHash::StringKeyed (from snaky_hash g
492
493
493
494
As of v2.0.11, if you need to serialize the parsed result, you can!
494
495
495
- There are two ways to do this, and the second option recommended.
496
+ There are two ways to do this, globally, or discretely. The discrete way is recommended.
496
497
497
498
1 . Globally configure ` SnakyHash::StringKeyed ` to use the serializer. Put this in your code somewhere reasonable (like an initializer for Rails):
498
499
499
- ``` ruby
500
+ ``` ruby
500
501
SnakyHash ::StringKeyed .class_eval do
501
502
extend SnakyHash ::Serializer
503
+ end
504
+ ```
505
+
506
+ 2 . Discretely configure a custom Snaky Hash class to use the serializer:
507
+
508
+ ` ` ` ruby
509
+ class MySnakyHash < SnakyHash::StringKeyed
510
+ # Give this hash class ` dump` and ` load ` abilities!
511
+ extend SnakyHash::Serializer
512
+ end
513
+
514
+ # And tell your client to use the custom class in each call:
515
+ client = OAuth2::Client.new("client_id", "client_secret", site: "https://example.org/oauth2")
516
+ token = client.get_token({snaky_hash_klass: MySnakyHash})
517
+ ` ` `
518
+
519
+ # #### Serialization Extensions
520
+
521
+ There are a few hacks you may need in your class to support Ruby < 2.4.2 or < 2.6.
522
+ They are likely not needed if you are on a newer Ruby .
523
+ See ` response_spec.rb` if you need to study the hacks for older Rubies .
524
+
525
+ ` ` ` ruby
526
+ class MySnakyHash < SnakyHash::StringKeyed
527
+ # Give this hash class ` dump` and ` load ` abilities!
528
+ extend SnakyHash::Serializer
529
+
530
+ #### Serialization Extentions
531
+ #
532
+ # Act on the non-hash values (including the values of hashes) as they are dumped to JSON
533
+ # In other words, this retains nested hashes, and only the deepest leaf nodes become bananas.
534
+ # WARNING: This is a silly example!
535
+ dump_value_extensions.add(:to_fruit) do |value|
536
+ "banana" # => Make values "banana" on dump
537
+ end
538
+
539
+ # Act on the non-hash values (including the values of hashes) as they are loaded from the JSON dump
540
+ # In other words, this retains nested hashes, and only the deepest leaf nodes become ***.
541
+ # WARNING: This is a silly example!
542
+ load_value_extensions.add(:to_stars) do |value|
543
+ "***" # Turn dumped bananas into *** when they are loaded
544
+ end
545
+
546
+ # Act on the entire hash as it is prepared for dumping to JSON
547
+ # WARNING: This is a silly example!
548
+ dump_hash_extensions.add(:to_cheese) do |value|
549
+ if value.is_a?(Hash)
550
+ value.transform_keys do |key|
551
+ split = key.split("_")
552
+ first_word = split[0]
553
+ key.sub(first_word, "cheese")
554
+ end
555
+ else
556
+ value
557
+ end
558
+ end
559
+
560
+ # Act on the entire hash as it is loaded from the JSON dump
561
+ # WARNING: This is a silly example!
562
+ load_hash_extensions.add(:to_pizza) do |value|
563
+ if value.is_a?(Hash)
564
+ res = klass.new
565
+ value.keys.each_with_object(res) do |key, result|
566
+ split = key.split("_")
567
+ last_word = split[-1]
568
+ new_key = key.sub(last_word, "pizza")
569
+ result[new_key] = value[key]
570
+ end
571
+ res
572
+ else
573
+ value
574
+ end
575
+ end
502
576
end
503
577
` ` `
504
578
579
+ See ` response_spec.rb` , or the [oauth- xx/ snaky_hash](https: // gitlab.com/ oauth- xx/ snaky_hash) gem for more ideas.
580
+
505
581
# ### What if I hate snakes and/or indifference?
506
582
507
583
` ` ` ruby
0 commit comments