Class: Wayback::Response::ParseAvailablity

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/wayback/response/parse_availability.rb

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object


19
20
21
22
23
# File 'lib/wayback/response/parse_availability.rb', line 19

def on_complete(env)
  if respond_to?(:parse) && ((env[:response_headers] && env[:response_headers]['content-type']) || '').match(/^application\/javascript/i)
    env[:body] = parse(env[:body]) unless unparsable_status_codes.include?(env[:status])
  end
end

#parse(body) ⇒ Object


8
9
10
11
12
13
14
15
16
17
# File 'lib/wayback/response/parse_availability.rb', line 8

def parse(body)
  case body
    when /^\{/
      obj = JSON.parse(body, :symbolize_names => true)[:archived_snapshots][:closest] rescue nil
      obj[:id] = obj[:timestamp] if obj && obj[:timestamp]
      obj
    else
      nil
  end
end

#unparsable_status_codesObject


25
26
27
# File 'lib/wayback/response/parse_availability.rb', line 25

def unparsable_status_codes
  [204, 301, 302, 304]
end