Class: NewsFetcher::Fetcher
- Inherits:
-
Object
- Object
- NewsFetcher::Fetcher
- Includes:
- SetParams
- Defined in:
- lib/newsfetcher/fetcher.rb
Instance Attribute Summary collapse
-
#actual_uri ⇒ Object
Returns the value of attribute actual_uri.
-
#max_redirects ⇒ Object
Returns the value of attribute max_redirects.
-
#moved ⇒ Object
Returns the value of attribute moved.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#uri ⇒ Object
Returns the value of attribute uri.
Class Method Summary collapse
Instance Method Summary collapse
- #content ⇒ Object
- #find_feeds ⇒ Object
- #get ⇒ Object
-
#initialize(**params) ⇒ Fetcher
constructor
A new instance of Fetcher.
- #parse_feed ⇒ Object
- #response_reason ⇒ Object
- #response_status ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(**params) ⇒ Fetcher
17 18 19 20 21 22 |
# File 'lib/newsfetcher/fetcher.rb', line 17 def initialize(**params) super({ timeout: 30, max_redirects: 5, }.merge(params)) end |
Instance Attribute Details
#actual_uri ⇒ Object
Returns the value of attribute actual_uri.
9 10 11 |
# File 'lib/newsfetcher/fetcher.rb', line 9 def actual_uri @actual_uri end |
#max_redirects ⇒ Object
Returns the value of attribute max_redirects.
7 8 9 |
# File 'lib/newsfetcher/fetcher.rb', line 7 def max_redirects @max_redirects end |
#moved ⇒ Object
Returns the value of attribute moved.
8 9 10 |
# File 'lib/newsfetcher/fetcher.rb', line 8 def moved @moved end |
#timeout ⇒ Object
Returns the value of attribute timeout.
6 7 8 |
# File 'lib/newsfetcher/fetcher.rb', line 6 def timeout @timeout end |
#uri ⇒ Object
Returns the value of attribute uri.
5 6 7 |
# File 'lib/newsfetcher/fetcher.rb', line 5 def uri @uri end |
Class Method Details
.get(uri) ⇒ Object
13 14 15 |
# File 'lib/newsfetcher/fetcher.rb', line 13 def self.get(uri) new(uri: uri).tap(&:get) end |
Instance Method Details
#content ⇒ Object
40 41 42 |
# File 'lib/newsfetcher/fetcher.rb', line 40 def content @response&.body end |
#find_feeds ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/newsfetcher/fetcher.rb', line 59 def find_feeds raise Error, "No response yet" unless @response raise Error, "Can't find feeds in failed response" unless @response.success? html = Nokogiri::HTML::Document.parse(content) html.xpath('//link[@rel="alternate"]').select { |link| FeedTypes.include?(link['type']) }.map do |link| { uri: @uri.join(link['href']), type: link['type'], } end end |
#get ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/newsfetcher/fetcher.rb', line 71 def get @actual_uri = @uri redirects = 0 while redirects < @max_redirects do @response = nil connection = Faraday.new( url: @actual_uri, request: { timeout: @timeout }, ssl: { verify: false }) begin @response = connection.get rescue Faraday::Error => e raise Error, "Error: #{e.} (#{e.class})" end if (300...400).include?(@response.status) @moved = (@response.status == 302) location = @response.headers[:location] or raise Error, "No Location header found in redirect" @actual_uri = @actual_uri.join(Addressable::URI.parse(location)) redirects += 1 next end return @response.success? end raise Error, "Too many redirects" end |
#parse_feed ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/newsfetcher/fetcher.rb', line 44 def parse_feed raise Error, "No response yet" unless @response raise Error, "Can't parse feed in failed response" unless @response.success? Feedjira.configure { |c| c.strip_whitespace = true } begin feedjira = Feedjira.parse(content) rescue Feedjira::NoParserAvailable, Date::Error => e raise Error, "Can't parse XML feed from #{@uri}: #{e}" end { title: feedjira.title, items: feedjira.entries.map { |e| Item.new(e) }, } end |
#response_reason ⇒ Object
36 37 38 |
# File 'lib/newsfetcher/fetcher.rb', line 36 def response_reason @response&.reason_phrase end |
#response_status ⇒ Object
32 33 34 |
# File 'lib/newsfetcher/fetcher.rb', line 32 def response_status @response&.status end |
#success? ⇒ Boolean
28 29 30 |
# File 'lib/newsfetcher/fetcher.rb', line 28 def success? @response.success? end |