Class: Revactor::HttpFetcher::Worker

Inherits:
Object
  • Object
show all
Extended by:
Actorize
Defined in:
lib/revactor/http_fetcher.rb

Instance Method Summary collapse

Methods included from Actorize

spawn, spawn_link

Constructor Details

#initialize(fetcher) ⇒ Worker

Returns a new instance of Worker.



60
61
62
63
# File 'lib/revactor/http_fetcher.rb', line 60

def initialize(fetcher)
  @fetcher = fetcher      
  loop { wait_for_request }
end

Instance Method Details

#decode_body(response) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/revactor/http_fetcher.rb', line 91

def decode_body(response)
  if response.content_encoding == 'gzip'
    Zlib::GzipReader.new(StringIO.new(response.body)).read
  else
    response.body
  end
end

#fetch(url, args) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/revactor/http_fetcher.rb', line 81

def fetch(url, args)
  Actor::HttpClient.get(url, :head => {'Accept-Encoding' => 'gzip'}) do |response|
    if response.status == 200        
      @fetcher << T[:fetched, url, decode_body(response), args]
    else
      @fetcher << T[:failed, url, response.status, args]
    end
  end
end

#wait_for_requestObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/revactor/http_fetcher.rb', line 65

def wait_for_request
  Actor.receive do |filter|
    filter.when(T[:fetch]) do |_, url, args|
      begin
        fetch url, args
      rescue => ex
        @fetcher << T[:error, url, ex, args]
      end
    
      # FIXME this should be unnecessary, but the HTTP client "leaks" messages
      Actor.current.mailbox.clear
      @fetcher << T[:ready, Actor.current]
    end
  end
end