Skip to content

Instantly share code, notes, and snippets.

@tuwukee
Last active March 9, 2020 20:17
Show Gist options
  • Save tuwukee/b3424afff2218d3056edac1c18ab4057 to your computer and use it in GitHub Desktop.
Save tuwukee/b3424afff2218d3056edac1c18ab4057 to your computer and use it in GitHub Desktop.
Sample strategy
module Template
class JsonInputStrategy < Template::InputStrategy
def parse_request_input
begin
JSON.parse(request_input)
rescue JSON::ParserError
{}
end
end
end
end
class InputProcessor
include ActiveModel::Model
attr_accessor :request_input, :input_type
def processor
@processor ||= case input_type
when Template::INPUT_TYPES[:csv]
Template::CsvInputStrategy.new(request_input: request_input)
when Template::INPUT_TYPES[:xml]
Template::XmlInputStrategy.new(request_input: request_input)
else
Template::JsonInputStrategy.new(request_input: request_input)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment