Class: Keen::Async::Job
- Inherits:
-
Object
- Object
- Keen::Async::Job
- Defined in:
- lib/keen/async/job.rb
Instance Attribute Summary collapse
-
#auth_token ⇒ Object
Represents one job.
-
#collection_name ⇒ Object
Represents one job.
-
#event_body ⇒ Object
Represents one job.
-
#project_id ⇒ Object
Represents one job.
-
#timestamp ⇒ Object
Represents one job.
Instance Method Summary collapse
-
#initialize(handler, definition) ⇒ Job
constructor
A new instance of Job.
- #load_definition(definition) ⇒ Object
- #save ⇒ Object
- #to_json(options = nil) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(handler, definition) ⇒ Job
Returns a new instance of Job.
21 22 23 24 25 26 |
# File 'lib/keen/async/job.rb', line 21 def initialize(handler, definition) # The `definition` can come from redis, a flat file, or code. load_definition(definition) @handler = handler end |
Instance Attribute Details
#auth_token ⇒ Object
Represents one job.
10 11 12 |
# File 'lib/keen/async/job.rb', line 10 def auth_token @auth_token end |
#collection_name ⇒ Object
Represents one job.
10 11 12 |
# File 'lib/keen/async/job.rb', line 10 def collection_name @collection_name end |
#event_body ⇒ Object
Represents one job.
10 11 12 |
# File 'lib/keen/async/job.rb', line 10 def event_body @event_body end |
#project_id ⇒ Object
Represents one job.
10 11 12 |
# File 'lib/keen/async/job.rb', line 10 def project_id @project_id end |
#timestamp ⇒ Object
Represents one job.
10 11 12 |
# File 'lib/keen/async/job.rb', line 10 def @timestamp end |
Instance Method Details
#load_definition(definition) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/keen/async/job.rb', line 28 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 |
#save ⇒ Object
71 72 73 |
# File 'lib/keen/async/job.rb', line 71 def save @handler.record_job(self) end |
#to_json(options = nil) ⇒ Object
12 13 14 |
# File 'lib/keen/async/job.rb', line 12 def to_json(=nil) @definition.to_json end |
#to_s ⇒ Object
17 18 19 |
# File 'lib/keen/async/job.rb', line 17 def to_s self.to_json end |