Class: PagerDuty::Connection::ParseTimeStrings

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

Constant Summary collapse

TIME_KEYS =
%w(
  at
  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
)
NESTED_COLLECTION_KEYS =
%w(
  acknowledgers
  assigned_to
  pending_actions
)

Instance Method Summary collapse

Instance Method Details

#parse(body) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/pager_duty/connection.rb', line 109

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



127
128
129
130
131
132
133
134
135
136
# File 'lib/pager_duty/connection.rb', line 127

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

    NESTED_COLLECTION_KEYS.each do |key|
      object_collection = object[key]
      parse_collection_times(object_collection) if object_collection
    end
  end
end

#parse_object_times(object) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/pager_duty/connection.rb', line 138

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