praggerのRSS::loadをbasic認証に対応させてみた。

とりあえず自分用に必要だったので、basic認証に対応させてみた。

## Load RSS from given URLs -- IKeJI
##
## Load RSS from given URLs.
## If URL is an Array, all URLs in the array will be loaded.
##
## - module: RSS::load
##   config:
##     uri: http://www.example.com/hoge.rdf
##     auth:
##       user_id: USER_ID
##       password: PASSWORD

require 'open-uri'
require 'rss/1.0'
require 'rss/2.0'
require 'rss/maker'

def load(config, data)
  begin
    auth = nil
    if config['auth'].is_a?(Hash)
      auth = {:http_basic_authentication => [ config['auth']['user_id'], config['auth']['password'] ]}
    end
    rss_source =
      if config['url'].is_a?(Array)
        config['url'].map {|url| open(url, auth ) {|io| io.read } }
      else
        [ open(config['url'], auth) {|r| r.read } ]
      end
  rescue
    puts "LoadError File = #{config["url"]}"
    return []
  end

  feeds = rss_source.collect {|cont|
    begin
      RSS::Parser.parse(cont)
    rescue RSS::InvalidRSSError
      RSS::Parser.parse(cont, false)
    end
  }

  feeds.select {|f| f}.inject([]) {|acc,f| acc + f.items }
end

svn diffはこっち

Index: plugin/RSS/load.rb
===================================================================
--- plugin/RSS/load.rb  (リビジョン 97)
+++ plugin/RSS/load.rb  (作業コピー)
@@ -6,6 +6,9 @@
 ## - module: RSS::load
 ##   config:
 ##     uri: http://www.example.com/hoge.rdf
+##     auth:
+##       user_id: USER_ID
+##       password: PASSWORD

 require 'open-uri'
 require 'rss/1.0'
@@ -14,11 +17,15 @@

 def load(config, data)
   begin
+    auth = nil
+    if config['auth'].is_a?(Hash)
+      auth = {:http_basic_authentication => [ config['auth']['user_id'], config['auth']['password'] ]}
+    end
     rss_source =
       if config['url'].is_a?(Array)
-        config['url'].map {|url| open(url) {|io| io.read } }
+        config['url'].map {|url| open(url, auth ) {|io| io.read } }
       else
-        [ open(config['url']) {|r| r.read } ]
+        [ open(config['url'], auth) {|r| r.read } ]
       end
   rescue
     puts "LoadError File = #{config["url"]}"

しかし対象のページは結局その上でマイページへのログインが必要だったので使うことは無かったのだった……。