Sha256: bce3118d7164bd439bde114d372b6003161a79cb4838f1572edb985b41a03f22

Contents?: true

Size: 1.75 KB

Versions: 12

Compression:

Stored size: 1.75 KB

Contents

require 'keen/async/storage/redis_handler'

module Keen
  module Async
    class Job
      # Represents one job.
      #
      #

      attr_accessor :project_id, :auth_token, :collection_name, :event_body, :timestamp
      
      def to_json(options=nil)
        @definition.to_json
      end


      def to_s
        self.to_json
      end
      
      def initialize(handler, definition)
        # The `definition` can come from redis, a flat file, or code.
        load_definition(definition)
        @handler = handler
        
      end

      def load_definition(definition)

        definition = Keen::Utils.symbolize_keys(definition)

        # define some key lists:
        required_keys = [:timestamp, :project_id, :auth_token, :collection_name, :event_body]
        optional_keys = [:keen_client_version]
        all_keys = required_keys + optional_keys


        # don't allow them to send nil values for anything
        definition.each do |key, value|
          # reject unrecognized keys:
          raise "Unrecognized key: #{key}" unless all_keys.include? key.to_sym
        end


        required_keys.each do |key|
          
          unless definition.has_key? key
            raise "You failed to send: #{key} -- you sent #{JSON.generate definition}"
          end

          value = definition[key]

          raise "You sent a nil value for the #{key}." if value.nil?
        end


        all_keys.each do |key|
          value = definition[key]
          self.instance_variable_set("@#{key}", value)
        end

        unless definition.has_key? :keen_client_version
          definition[:keen_client_version] = Keen::VERSION
        end

        @definition = definition


      end

      def save
        @handler.record_job(self)
      end

    end
  end
end


Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
keen-0.2.0 lib/keen/async/job.rb
keen-0.1.12 lib/keen/async/job.rb
keen-0.1.11 lib/keen/async/job.rb
keen-0.1.10 lib/keen/async/job.rb
keen-0.1.9 lib/keen/async/job.rb
keen-0.1.8 lib/keen/async/job.rb
keen-0.1.7 lib/keen/async/job.rb
keen-0.1.6 lib/keen/async/job.rb
keen-0.1.5 lib/keen/async/job.rb
keen-0.1.4 lib/keen/async/job.rb
keen-0.1.3 lib/keen/async/job.rb
keen-0.1.0 lib/keen/async/job.rb