Class: NewsFetcher::Fetcher

Inherits:
Object
  • Object
show all
Includes:
SetParams
Defined in:
lib/newsfetcher/fetcher.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_uriObject

Returns the value of attribute actual_uri.



9
10
11
# File 'lib/newsfetcher/fetcher.rb', line 9

def actual_uri
  @actual_uri
end

#max_redirectsObject

Returns the value of attribute max_redirects.



7
8
9
# File 'lib/newsfetcher/fetcher.rb', line 7

def max_redirects
  @max_redirects
end

#movedObject

Returns the value of attribute moved.



8
9
10
# File 'lib/newsfetcher/fetcher.rb', line 8

def moved
  @moved
end

#timeoutObject

Returns the value of attribute timeout.



6
7
8
# File 'lib/newsfetcher/fetcher.rb', line 6

def timeout
  @timeout
end

#uriObject

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

#contentObject



40
41
42
# File 'lib/newsfetcher/fetcher.rb', line 40

def content
  @response&.body
end

#find_feedsObject

Raises:



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

#getObject

Raises:



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.message} (#{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_feedObject

Raises:



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_reasonObject



36
37
38
# File 'lib/newsfetcher/fetcher.rb', line 36

def response_reason
  @response&.reason_phrase
end

#response_statusObject



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