class Wicoris::Postman::Job

Attributes

logger[R]

Public Class Methods

new(json_file, opts = {}) click to toggle source
# File lib/wicoris/postman/job.rb, line 6
def initialize(json_file, opts = {})
  @json_file = json_file
  @opts = opts
  @logger = opts[:logger]
end

Public Instance Methods

clear!() click to toggle source

Remove the JSON file.

# File lib/wicoris/postman/job.rb, line 36
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
letter() click to toggle source

@returns [String] Path to actual letter

# File lib/wicoris/postman/job.rb, line 13
def letter
  if not File.exists?(file)
    fail "Letter does not exist: #{file}"
  else
    file
  end
end
patient() click to toggle source

@returns [String] Patient's display name.

# File lib/wicoris/postman/job.rb, line 44
def patient
  format('%s, %s (%s)',
         patient_last_name,
         patient_first_name,
         patient_date_of_birth)
rescue
  'Unknown'
end
process() click to toggle source

Process the job

# File lib/wicoris/postman/job.rb, line 22
def process
  deliver
  logger.info :message => 'Letter delivered :-D',
              :patient => patient,
              :job => @json_file
rescue => e
  logger.error :message => 'Letter not delivered ;-(',
               :patient => patient,
               :reason => e.message,
               :job => @json_file
  logger.debug e
end
to_s() click to toggle source
# File lib/wicoris/postman/job.rb, line 53
def to_s
  @json_file
end

Private Instance Methods

deliver() click to toggle source
# File lib/wicoris/postman/job.rb, line 59
def deliver
  delivery_machine.new(self, @opts).run
end
delivery_machine() click to toggle source

How we gonna deliver the letter?

# File lib/wicoris/postman/job.rb, line 64
def delivery_machine
  # NOTE: `Object#type` is an existing method in Ruby 1.8.7, therefore we
  # have to fetch the attribute from the JSON hash.
  case json['type']
  when 'fax'  then FaxMachine
  when 'copy' then Copier
  # TODO: Handle unknown case!
  #else
  #  ...
  end
end
json() click to toggle source

Parse and return the JSON. @returns [Hash] Cached JSON.

# File lib/wicoris/postman/job.rb, line 78
def json
  @json ||= JSON.parse(json_file_content)
end
json_file_content() click to toggle source

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.

# File lib/wicoris/postman/job.rb, line 86
def json_file_content
  Iconv.conv('UTF-8', 'MacRoman', File.read(@json_file))
end
method_missing(id,*args,&block) click to toggle source

Provide convenient methods for accessing JSON attributes.

Calls superclass method
# File lib/wicoris/postman/job.rb, line 91
def method_missing(id,*args,&block)
  json.key?(id.to_s) ? json[id.to_s] : super
end