Class: PagerDuty::Connection::ParseTimeStrings

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/pager_duty/connection.rb

Constant Summary collapse

TIME_KEYS =
%w(
  created_at
  created_on
  end
  end_time
  last_incident_timestamp
  last_status_change_on
  start
  started_at
  start_time
)
OBJECT_KEYS =
%w(
  alert
  entry
  incident
  log_entry
  maintenance_window
  note
  override
  service
)

Instance Method Summary collapse

Instance Method Details

#parse(body) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pager_duty/connection.rb', line 88

def parse(body)
  case body
  when Hash, ::Hashie::Mash
    OBJECT_KEYS.each do |key|
      object = body[key]
      parse_object_times(object) if object

      collection_key = key.pluralize
      collection = body[collection_key]
      parse_collection_times(collection) if collection
    end

    body
  else
    raise "Can't parse times of #{body.class}: #{body}"
  end
end

#parse_collection_times(collection) ⇒ Object



106
107
108
109
110
# File 'lib/pager_duty/connection.rb', line 106

def parse_collection_times(collection)
  collection.each do |object|
    parse_object_times(object)
  end
end

#parse_object_times(object) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/pager_duty/connection.rb', line 112

def parse_object_times(object)
  time = Time.zone ? Time.zone : Time

  TIME_KEYS.each do |key|
    if object.has_key?(key) && object[key].present?
      object[key] = time.parse(object[key])
    end
  end
end