Class: Junkie::Resolver::RedirectResolver
- Inherits:
-
Object
- Object
- Junkie::Resolver::RedirectResolver
- Defined in:
- lib/junkie/resolver/redirect.rb
Constant Summary collapse
- DEFAULT_CONFIG =
{ uri_patterns: [ 'http://download.serienjunkies.org/go-' ], }
Instance Method Summary collapse
-
#initialize(channels) ⇒ RedirectResolver
constructor
A new instance of RedirectResolver.
-
#resolve_links(episode) ⇒ Object
resolve links that matches any pattern by issuing a HEAD request and using the Location response header instead of the original supplied header.
- #send_episode(episode) ⇒ Object
Methods included from Log
Methods included from Config
collect_default_configs, get_config, included
Constructor Details
#initialize(channels) ⇒ RedirectResolver
Returns a new instance of RedirectResolver.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/junkie/resolver/redirect.rb', line 15 def initialize(channels) @channels = channels @patterns = Config.get_config(self)[:uri_patterns] channels[:episodes].subscribe do |episode| next unless episode.status == :decrypted resolve_links(episode) end end |
Instance Method Details
#resolve_links(episode) ⇒ Object
resolve links that matches any pattern by issuing a HEAD request and using the Location response header instead of the original supplied header
28 29 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 |
# File 'lib/junkie/resolver/redirect.rb', line 28 def resolve_links(episode) multi = EventMachine::MultiRequest.new request_count = 0 episode.links.each_with_index do |link, index| next unless @patterns.index { |p| link.include?(p) } request_count += 1 multi.add(index, EventMachine::HttpRequest.new(link).head) end if request_count == 0 send_episode(episode) else multi.callback do links = episode.links.clone responses = multi.responses[:callback] responses.each do |index, value| location = value.response_header.fetch("LOCATION") { nil } unless location.nil? links[index] = location end end episode.links = links send_episode(episode) end end end |
#send_episode(episode) ⇒ Object
60 61 62 63 |
# File 'lib/junkie/resolver/redirect.rb', line 60 def send_episode(episode) episode.status = :resolved @channels[:episodes].push(episode) end |