Class: PagerDuty::Connection::ParseTimeStrings

Inherits:
Faraday::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

#on_complete(env) ⇒ Object



140
141
142
# File 'lib/pager_duty/connection.rb', line 140

def on_complete(env)
  parse(env[:body])
end

#parse(body) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/pager_duty/connection.rb', line 144

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
  when Array
    body.map! { |element| parse(element) }
  else
    raise "Can't parse times of #{body.class}: #{body}"
  end
end

#parse_collection_times(collection) ⇒ Object



164
165
166
167
168
169
170
171
172
173
# File 'lib/pager_duty/connection.rb', line 164

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



175
176
177
178
179
180
181
182
183
# File 'lib/pager_duty/connection.rb', line 175

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