Class: PagerJudy::API::Item

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

Overview

Represents an item, e.g. a service, a user, …

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, type, id, criteria = {}) ⇒ Item

Returns a new instance of Item.



8
9
10
11
12
13
# File 'lib/pager_judy/api/item.rb', line 8

def initialize(resource, type, id, criteria = {})
  @resource = resource
  @type = type
  @id = id
  @criteria = criteria
end

Instance Attribute Details

#criteriaObject (readonly)

Returns the value of attribute criteria.



18
19
20
# File 'lib/pager_judy/api/item.rb', line 18

def criteria
  @criteria
end

#idObject (readonly)

Returns the value of attribute id.



17
18
19
# File 'lib/pager_judy/api/item.rb', line 17

def id
  @id
end

#resourceObject (readonly)

Returns the value of attribute resource.



15
16
17
# File 'lib/pager_judy/api/item.rb', line 15

def resource
  @resource
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#readObject



25
26
27
# File 'lib/pager_judy/api/item.rb', line 25

def read
  resource.get(criteria).fetch(type)
end

#to_hObject



39
40
41
# File 'lib/pager_judy/api/item.rb', line 39

def to_h
  read
end

#update(data) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/pager_judy/api/item.rb', line 29

def update(data)
  if dry_run?
    result = data
  else
    result = resource.put(type => data).fetch(type)
  end
  name = result.fetch("name")
  logger.info { "updated #{type} #{name.inspect} [#{id}]" }
end

#with(more_criteria) ⇒ Object



20
21
22
23
# File 'lib/pager_judy/api/item.rb', line 20

def with(more_criteria)
  more_criteria = Hash[more_criteria.select { |_, v| v }]
  self.class.new(resource, type, id, criteria.merge(more_criteria))
end