module Wicoris module Postman class Job attr_reader :logger def initialize(json_file, opts = {}) @json_file = json_file @opts = opts @logger = opts[:logger] end # @returns [String] Path to actual letter def letter if not File.exists?(file) msg = to_hash msg[:message] = 'Letter does not exist' @opts[:logger].error(msg) if @opts[:logger] raise "Letter does not exist: #{file}" # TODO: DRY! else file end end # Process the job def process # NOTE: `Object#type` is an existing method in Ruby 1.8.7, therefore we # have to fetch the attribute from the JSON hash. delivery_method = case json['type'] when 'fax' then FaxMachine when 'copy' then Copier # TODO: Handle unknown case! #else # ... end delivery_method.new(self, @opts).run end # Remove the JSON file. def clear! FileUtils.rm(@json_file, :noop => (@opts[:noop] == true)) if json rescue JSON::ParserError logger.warn :message => 'Refused to delete non-JSON file.', :json_file => @json_file end # @returns [Hash] Job properties def to_hash properties = { 'json_file' => @json_file } properties.merge(json) rescue properties end private # Parse and return the JSON. # @returns [Hash] Cached JSON. def json @json ||= JSON.parse(json_file_content) end # FileMaker creates JSON files with Mac OS Roman file encoding. # We have to convert it to UTF-8 in order to avoid cryptic symbols. # # @returns [String] UTF-8 encoded file content. def json_file_content Iconv.conv('UTF-8', 'MacRoman', File.read(@json_file)) end # Provide convenient methods for accessing JSON attributes. def method_missing(id,*args,&block) json.key?(id.to_s) ? json[id.to_s] : super end end end end