liquid_interpolatable.rb 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. module LiquidInterpolatable
  2. extend ActiveSupport::Concern
  3. def interpolate_options(options, payload)
  4. case options.class.to_s
  5. when 'String'
  6. interpolate_string(options, payload)
  7. when 'ActiveSupport::HashWithIndifferentAccess', 'Hash'
  8. duped_options = options.dup
  9. duped_options.each do |key, value|
  10. duped_options[key] = interpolate_options(value, payload)
  11. end
  12. when 'Array'
  13. options.collect do |value|
  14. interpolate_options(value, payload)
  15. end
  16. end
  17. end
  18. def interpolate_string(string, payload)
  19. Liquid::Template.parse(string).render!(payload, registers: {agent: self})
  20. end
  21. require 'uri'
  22. # Percent encoding for URI conforming to RFC 3986.
  23. # Ref: http://tools.ietf.org/html/rfc3986#page-12
  24. module Filters
  25. def uri_escape(string)
  26. CGI::escape string
  27. end
  28. end
  29. Liquid::Template.register_filter(LiquidInterpolatable::Filters)
  30. module Tags
  31. class Credential < Liquid::Tag
  32. def initialize(tag_name, name, tokens)
  33. super
  34. @credential_name = name.strip
  35. end
  36. def render(context)
  37. credential = context.registers[:agent].credential(@credential_name)
  38. raise "No user credential named '#{@credential_name}' defined" if credential.nil?
  39. credential
  40. end
  41. end
  42. end
  43. Liquid::Template.register_tag('credential', LiquidInterpolatable::Tags::Credential)
  44. end