Class: WebpageArchivist::Fetcher::StyleSheetRequest

Inherits:
ElementRequest show all
Defined in:
lib/webpage-archivist/fetcher/stylesheet_request.rb

Overview

A request specific for stylesheet as a stylesheet can reference images or other stylesheets

Instance Attribute Summary

Attributes inherited from ElementRequest

#element, #status, #uri

Instance Method Summary collapse

Methods inherited from ElementRequest

#add_requester, #notify_requesters, #process_response, #start, #will_process_response

Constructor Details

#initialize(uri, plumber) ⇒ StyleSheetRequest

Returns a new instance of StyleSheetRequest.



8
9
10
11
12
# File 'lib/webpage-archivist/fetcher/stylesheet_request.rb', line 8

def initialize uri, plumber
  super WebpageArchivist::Stylesheet, uri, plumber
  @parsed_uri = Addressable::URI.parse(uri)
  @waiting_requests = 0
end

Instance Method Details

#after_requestsObject

Process the response once all the elements have been fetched



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/webpage-archivist/fetcher/stylesheet_request.rb', line 85

def after_requests
  ::WebpageArchivist.debug "After requests [#{@uri}]" if ::WebpageArchivist.log
  if @modified
    @stylesheet.each_import do |i|
      if e = @plumber[i].andand.element
        e.file_name
      else
        nil
      end
    end

    @stylesheet.each_image do |i|
      if e = @plumber[i].andand.element
        e.file_name
      else
        nil
      end
    end

    element.save_content @stylesheet.to_css
  end
  self.status = :over
  notify_requesters
end

#content_not_changed(http) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/webpage-archivist/fetcher/stylesheet_request.rb', line 14

def content_not_changed http
  @modified = false
  if element.andand.last_content
    create_sub_requests http
  else
    self.status = :over
    notify_requesters
  end
end

#create_sub_requests(http) ⇒ Object

Don’t directly save the content but first retrieve any possible other stylesheet and image



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/webpage-archivist/fetcher/stylesheet_request.rb', line 30

def create_sub_requests http
  if @modified
    charset = @plumber.response_charset(http)
    @stylesheet = WebpageArchivist::StylesheetDocument.new(http.response, @uri, charset)
  else
    @stylesheet = WebpageArchivist::StylesheetDocument.new(element.last_content, @uri, element.last_charset)
  end

  already = Set.new
  @stylesheet.each_import do |i|
    unless already.include? i
      already.add i
      i = @parsed_uri.absolutize(i)
      @plumber.request_element self, WebpageArchivist::Stylesheet, i
      @waiting_requests += 1
      i
    end
  end

  already = Set.new
  @stylesheet.each_image do |i|
    unless already.include? i
      already.add i
      i = @parsed_uri.absolutize(i)
      @plumber.request_element self, WebpageArchivist::Image, i
      @waiting_requests += 1
      i
    end
  end

  if @modified
    element.update(:last_content => @stylesheet.to_css, :last_charset => @stylesheet.charset)
  end

  self.status = :fetching_requests

  # No external resource -> end here
  if @waiting_requests == 0
    after_requests
  end

end

#request_over(uri) ⇒ Object

Called by a request when it is over

uri

the request uri



75
76
77
78
79
80
81
82
# File 'lib/webpage-archivist/fetcher/stylesheet_request.rb', line 75

def request_over uri
  @waiting_requests -= 1
  ::WebpageArchivist.debug "Request over for [#{@uri}] on [#{uri}], missing #{@waiting_requests}" if ::WebpageArchivist.log

  if (@waiting_requests == 0) && (status == :fetching_requests)
    after_requests
  end
end

#save_content_end_request(http) ⇒ Object



24
25
26
27
# File 'lib/webpage-archivist/fetcher/stylesheet_request.rb', line 24

def save_content_end_request http
  @modified = true
  create_sub_requests http
end