mwilliams-feedzirra 0.0.15 → 0.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/lib/feedzirra.rb +2 -2
  2. data/lib/feedzirra/feed.rb +22 -7
  3. metadata +1 -1
data/lib/feedzirra.rb CHANGED
@@ -7,7 +7,7 @@ module Feedzirra
7
7
  def self.use_curb?
8
8
  @use_curb
9
9
  end
10
- VERSION = "0.0.15"
10
+ VERSION = "0.0.16"
11
11
  end
12
12
 
13
13
  gem 'activesupport'
@@ -21,7 +21,7 @@ rescue LoadError
21
21
  Feedzirra.use_curb = false
22
22
  end
23
23
 
24
- require 'open-uri'
24
+ require 'net/http'
25
25
  require 'sax-machine'
26
26
  require 'dryopteris'
27
27
  require 'uri'
@@ -140,18 +140,33 @@ module Feedzirra
140
140
  else
141
141
  responses = {}
142
142
  url_queue.each do |url|
143
- headers = {}
144
- headers['if-modified-since'] = options[:if_modified_since] if options[:if_modified_since]
145
- headers['if-none-match'] = options[:if_none_match] if options[:if_none_match]
146
-
147
- open(url, headers) do |f|
148
- responses[url] = try_parse(url, f.status[0], f.meta, f.read)
149
- end
143
+ res = fetch_url(url)
144
+ responses[url] = try_parse(url, res.code, res, res.body)
150
145
  end
151
146
  end
152
147
  return urls.is_a?(String) ? responses.values.first : responses
153
148
  end
154
149
 
150
+ def self.fetch_url(url, limit=10, options={})
151
+ raise RuntimeError, "HTTP redirect too deep" if limit == 0
152
+
153
+ uri = URI.parse(url)
154
+ req = Net::HTTP::Get.new(uri.path)
155
+ req['if-modified-since'] = options[:if_modified_since] if options[:if_modified_since]
156
+ req['if-none-match'] = options[:if_none_match] if options[:if_none_match]
157
+ res = Net::HTTP.start(uri.host, uri.port) do |http|
158
+ http.request(req)
159
+ end
160
+
161
+ # Handle HTTP redirect
162
+ case res
163
+ when Net::HTTPSuccess then res
164
+ when Net::HTTPRedirection then fetch_url(res['location'], limit - 1)
165
+ else
166
+ response.error!
167
+ end
168
+ end
169
+
155
170
  def self.try_parse(url, response_code, headers, body)
156
171
  xml = decode_content2(headers, body)
157
172
  klass = determine_feed_parser_for_xml(xml)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mwilliams-feedzirra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.15
4
+ version: 0.0.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Dix