Sha256: 35a5bffa9bc48b3ed1f016bfb94d1a598d84ad92055ae872a9132e7af392eec5

Contents?: true

Size: 978 Bytes

Versions: 2

Compression:

Stored size: 978 Bytes

Contents

module Mandy
  class Task
    JSON_PAYLOAD_KEY = "json"
    KEY_VALUE_SEPERATOR = "\t" unless defined?(KEY_VALUE_SEPERATOR)

    def initialize(input=STDIN, output=STDOUT)
      @input, @output = input, output
    end

    def emit(key, value=nil)
      key = 'nil' if key.nil?
      @output.puts(value.nil? ? key.to_s : "#{serialize(key)}\t#{serialize(value)}")
    end

    def get(store, key)
      Mandy.stores[store].get(key)
    end

    def put(store, key, values)
      Mandy.stores[store].put(key, values)
    end

    private
    
    def parameter(name)
      return find_json_param(name) if json_provided?
      ENV[name.to_s]
    end
    
    def find_json_param(name)
      @json_args ||= JSON.parse(URI.decode(ENV[JSON_PAYLOAD_KEY]))
      @json_args[name.to_s]
    end
    
    def json_provided?
      !ENV[JSON_PAYLOAD_KEY].nil?
    end

    def serialize(value)
      value = ArraySerializer.new(value) if value.is_a?(Array)
      value.to_s
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
trafficbroker-mandy-0.2.10 lib/task.rb
trafficbroker-mandy-0.2.11 lib/task.rb