Class: Upperkut::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/upperkut/item.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, enqueued_at = nil) ⇒ Item

Returns a new instance of Item.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
# File 'lib/upperkut/item.rb', line 5

def initialize(body, enqueued_at = nil)
  raise ArgumentError, 'Body should be a Hash' unless body.is_a?(Hash)

  @body = body
  @enqueued_at = enqueued_at || Time.now.utc.to_i
end

Instance Attribute Details

#enqueued_atObject (readonly)

Returns the value of attribute enqueued_at.



3
4
5
# File 'lib/upperkut/item.rb', line 3

def enqueued_at
  @enqueued_at
end

Class Method Details

.from_json(item_json) ⇒ Object



35
36
37
38
# File 'lib/upperkut/item.rb', line 35

def self.from_json(item_json)
  hash = JSON.parse(item_json)
  new(hash['body'], hash['enqueued_at'])
end

Instance Method Details

#[](key) ⇒ Object



12
13
14
# File 'lib/upperkut/item.rb', line 12

def [](key)
  @body[key]
end

#[]=(key, value) ⇒ Object



16
17
18
# File 'lib/upperkut/item.rb', line 16

def []=(key, value)
  @body[key] = value
end

#bodyObject



24
25
26
# File 'lib/upperkut/item.rb', line 24

def body
  @body
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/upperkut/item.rb', line 20

def key?(key)
  @body.key?(key)
end

#to_jsonObject



28
29
30
31
32
33
# File 'lib/upperkut/item.rb', line 28

def to_json
  JSON.generate(
    'body' => @body,
    'enqueued_at' => @enqueued_at
  )
end