Created
August 16, 2022 19:33
-
-
Save palkan/ac1dcbd70394d36df87ff1bb5aeb9f76 to your computer and use it in GitHub Desktop.
pattern_matching_joe.rb
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
require 'active_support/core_ext/hash/indifferent_access' | |
def send_admin_notification(changes = previous_changes) | |
case changes | |
in ends_at: [Time, _] | |
:resubscribed | |
in ends_at: [nil, Time] | |
:churned | |
in processor_plan: [nil, String] | |
:subscribed | |
in processor_plan: Array | |
:plan_changed | |
in data: [_, {pause_behaviour: nil}] | |
:unpaused | |
in data: [{pause_behaviour: nil},_] | |
:paused | |
end | |
end | |
resubscribed = { | |
"ends_at": [ | |
Time.now - 60, | |
Time.now | |
], | |
"processor_plan": [ | |
"new", | |
nil | |
], | |
"data": [ | |
{ | |
"pause_behaviour": nil | |
}.with_indifferent_access, | |
{ | |
"pause_behaviour": "1" | |
}.with_indifferent_access | |
] | |
}.with_indifferent_access | |
churned = { | |
"ends_at": [ | |
nil, | |
Time.now | |
], | |
"processor_plan": [ | |
"new", | |
nil | |
], | |
"data": [ | |
{ | |
"pause_behaviour": nil | |
}.with_indifferent_access, | |
{ | |
"pause_behaviour": "1" | |
}.with_indifferent_access | |
] | |
}.with_indifferent_access | |
subscribed = { | |
"processor_plan": [ | |
nil, | |
"new" | |
], | |
"data": [ | |
{ | |
"pause_behaviour": nil | |
}.with_indifferent_access, | |
{ | |
"pause_behaviour": "1" | |
}.with_indifferent_access | |
] | |
}.with_indifferent_access | |
plan_changed = { | |
"processor_plan": [ | |
"new", | |
nil | |
], | |
"data": [ | |
{ | |
"pause_behaviour": nil | |
}.with_indifferent_access, | |
{ | |
"pause_behaviour": "1" | |
}.with_indifferent_access | |
] | |
}.with_indifferent_access | |
unpaused = { | |
"plan": [ | |
"new", | |
nil | |
], | |
"data": [ | |
{ | |
"pause_behaviour": "1", | |
"other_key": "was" | |
}.with_indifferent_access, | |
{ | |
"pause_behaviour": nil, | |
"other_key": "was", | |
"new_key": "1" | |
}.with_indifferent_access | |
] | |
}.with_indifferent_access | |
paused = { | |
"plan": [ | |
"new", | |
nil | |
], | |
"data": [ | |
{ | |
"pause_behaviour": nil | |
}.with_indifferent_access, | |
{ | |
"pause_behaviour": "1" | |
}.with_indifferent_access | |
] | |
}.with_indifferent_access | |
puts send_admin_notification(resubscribed) | |
puts send_admin_notification(churned) | |
puts send_admin_notification(subscribed) | |
puts send_admin_notification(plan_changed) | |
puts send_admin_notification(unpaused) | |
puts send_admin_notification(paused) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment