Class: BfMultiRss::Fetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/bf_multi_rss/fetcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(concurrency = 4) ⇒ Fetcher

Returns a new instance of Fetcher.



13
14
15
16
# File 'lib/bf_multi_rss/fetcher.rb', line 13

def initialize(concurrency = 4)
  @concurrency = concurrency
  @errors = []
end

Instance Attribute Details

#concurrencyObject (readonly)

Returns the value of attribute concurrency.



12
13
14
# File 'lib/bf_multi_rss/fetcher.rb', line 12

def concurrency
  @concurrency
end

#errorsObject (readonly)

Returns the value of attribute errors.



12
13
14
# File 'lib/bf_multi_rss/fetcher.rb', line 12

def errors
  @errors
end

Instance Method Details

#fetch_all(uris) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bf_multi_rss/fetcher.rb', line 48

def fetch_all(uris)
  @errors = []
  results = Parallel.map(
    uris,
    in_processes: @concurrency
  ) do |uri|
    begin
      posts = fetch_rss(uri)
      BfMultiRss::RssResult.new(uri, posts)
    rescue  REXML::ParseException,
            OpenURI::HTTPError,
            Errno::EHOSTUNREACH,
            Errno::ETIMEDOUT,
            RSS::NotWellFormedError,
            Net::OpenTimeout,
            Net::ReadTimeout,
            Errno::ECONNREFUSED,
            Errno::ECONNRESET,
            NotInvertibleError,
            ArgumentError,
            HTTP::ConnectionError => e
      BfMultiRss::RssError.new(uri, e.to_s)
    end
  end
  errors = results.select do |result|
    result.is_a? BfMultiRss::RssError
  end
  @errors = errors
  results.select do |result|
    !result.is_a? BfMultiRss::RssError
  end
end

#fetch_rss(uri) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/bf_multi_rss/fetcher.rb', line 24

def fetch_rss(uri)
  response = fetch_uri(uri)
  rss = RSS::Parser.parse(response, false)
  if rss.nil?
    err = 'ParseErr ' + uri
    raise NotInvertibleError, err
  end
  rss.items
end

#fetch_uri(uri) ⇒ Object



18
19
20
21
22
# File 'lib/bf_multi_rss/fetcher.rb', line 18

def fetch_uri(uri)
  response = HTTP.get(uri)
  raise_errors(response, uri)
  response.to_s
end

#raise_errors(response, uri) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bf_multi_rss/fetcher.rb', line 34

def raise_errors(response, uri)
  case response.status
  when 500
    err = 'Http500 ' + uri
    raise NotInvertibleError, err
  when 404
    err = 'Http404 ' + uri
    raise NotInvertibleError, err
  when 301
    err = 'Http301 ' + uri
    raise NotInvertibleError, err
  end
end