Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class GenerateSendGridHeaders
- # (uses the attr_extras gem)
- method_object :call,
- [ :mailer!, :action!, :args!, :env ]
- def call
- {
- category: [ mailer_name, "#{mailer_name}##{action}" ],
- unique_args: {
- environment: env,
- arguments: args_hash.to_json,
- },
- }
- end
- private
- def args_hash
- method_params = mailer.method(action).parameters
- param_names = method_params.map(&:last)
- rest_param_count = method_params.count { |(type, _)| type == :rest }
- raise "Only handles a single splat!" if rest_param_count > 1
- non_rest_param_count = method_params.length - rest_param_count
- non_rest_param_names = param_names.first(non_rest_param_count)
- non_rest_values = args.first(non_rest_param_count)
- # NOTE: This assumes the splat is always the last argument.
- # That is likely but not guaranteed.
- rest_values = args.from(non_rest_param_count)
- hash = Hash[ non_rest_param_names.zip(non_rest_values) ]
- if rest_values.any?
- rest_param_name = param_names.last
- hash.merge!(rest_param_name => rest_values)
- end
- hash
- end
- def mailer_name
- mailer.class.name
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement