Class: Wayback::Response::ParseMemento

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

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object



53
54
55
56
57
# File 'lib/wayback/response/parse_memento.rb', line 53

def on_complete(env)
  if respond_to?(:parse) && ((env[:response_headers] && env[:response_headers]['content-type']) || '').match(/^application\/link-format/i)
    env[:body] = parse(env[:body]) unless [204, 301, 302, 304].include?(env[:status])
  end
end

#parse(body, *opts) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/wayback/response/parse_memento.rb', line 8

def parse(body, *opts)
  case body
    # Assume it starts with "<http"
    when /^\<http/
      body, info = body.gsub(/,(\s+)?\</, ",\n<").gsub(/\s(\s+)/, ' ').split("\n"), {:id => nil, :dates => {}, :first_date => nil, :last_date => nil}

      body.each do |s|
        attrs, uri = s.split('; '), s.gsub(/^(\<)([A-Z0-9\-\/\:\.\?\=\&]+)(\>)(.*)$/i, '\2')
        rels, datetime, date, from, til = [], nil, nil, nil, nil

        attrs.each do |a|
          k, v = a.gsub(/^([A-Z0-9\-]+)(=.*)$/i, '\1'), a.gsub(/^([A-Z0-9\-]+)(=(\'|\"))(.*)(\'|\")(,)?$/i, '\4')
          case k
            when 'datetime'
              datetime, date = v, Time.parse(v).utc.strftime('%Y%m%d%H%M%S').to_i
            when 'rel'
              rels = v.split(' ')
            when 'from'
              # Not handled by archive.org
            when 'until'
              # Not handled by archive.org
          end
        end

        if rels.include?('original')
          info[:id] = uri
        elsif rels.include?('memento')
          info[:last_date] = date if rels.include?('last')
          info[:first_date] = date if rels.include?('first')
          info[:dates][date] = {:datetime => datetime, :uri => uri} unless date.nil?
        elsif rels.include?('timebundle')
          #
        elsif rels.include?('timegate')
          #
        elsif rels.include?('timemap')
          #
        end
      end

      info
    else
      nil
  end
end