module PayPoint::Blue::PayloadBuilder
Provides helper methods for payload construction used throughout the API
. It allows definition of payload shortcuts and default values.
Attributes
defaults[RW]
Public Class Methods
included(base)
click to toggle source
# File lib/paypoint/blue/payload_builder.rb, line 7 def self.included(base) base.extend ClassMethods end
Public Instance Methods
build_payload(payload, defaults: [])
click to toggle source
Builds the payload by applying default values and replacing shortcuts
@note When using the callback and notification shortcuts, the builder will also default their format
to +'REST_JSON'+, because the PayPoint
API
requires it in some cases. If your endpoints expect XML, you won't be able to use these shortcuts.
@param [Hash] payload the original payload using shortcuts @param [Array<Symbol>] defaults an array of symbols for defaults
that should be applied to this payload
# File lib/paypoint/blue/payload_builder.rb, line 57 def build_payload(payload, defaults: []) apply_defaults(payload, defaults) expand_shortcuts(payload) end
Private Instance Methods
apply_defaults(payload, applicable_defaults)
click to toggle source
# File lib/paypoint/blue/payload_builder.rb, line 64 def apply_defaults(payload, applicable_defaults) return unless defaults defaults.each do |key, value| if applicable_defaults.include?(key) && !payload.key?(key) payload[key] = interpolate_values(value, payload) end end end
expand_shortcut(payload, key, path)
click to toggle source
# File lib/paypoint/blue/payload_builder.rb, line 86 def expand_shortcut(payload, key, path) value = payload.delete(key) segments = path.split(".").map(&:to_sym) leaf = segments.pop leaf_parent = segments.reduce(payload) { |a, e| a[e] ||= {} } leaf_parent[leaf] ||= value callback_re = /_(?:callback|notification)\Z/ leaf_parent[:format] ||= "REST_JSON" if key =~ callback_re end
expand_shortcuts(payload)
click to toggle source
# File lib/paypoint/blue/payload_builder.rb, line 78 def expand_shortcuts(payload) payload.keys.each do |key| next unless (path = self.class.shortcut(key)) expand_shortcut(payload, key, path) end payload end
interpolate_values(value, payload)
click to toggle source
# File lib/paypoint/blue/payload_builder.rb, line 74 def interpolate_values(value, payload) value.gsub(/%(\w+)%/) { |m| payload[Regexp.last_match(1).to_sym] || m } end