class Feed2Email::OPMLExporter

Constants

MAX_REDIRECTS

Public Class Methods

export(path) click to toggle source
# File lib/feed2email/opml_exporter.rb, line 9
def self.export(path)
  require 'feed2email/feed'

  open(path, 'w') do |f|
    uris = Feed.by_smallest_id.select_map(:uri)

    if new(uris).export(f) > 0
      uris.size
    end
  end
end
new(uris) click to toggle source
# File lib/feed2email/opml_exporter.rb, line 21
def initialize(uris)
  @uris = uris
end

Public Instance Methods

export(io) click to toggle source
# File lib/feed2email/opml_exporter.rb, line 25
def export(io)
  io.write(xml)
end

Private Instance Methods

builder() click to toggle source
# File lib/feed2email/opml_exporter.rb, line 31
def builder
  Nokogiri::XML::Builder.new do |xml|
    xml.root {
      xml.opml(version: '2.0') {
        xml.head {
          xml.title 'feed2email subscriptions'
          xml.dateCreated Time.now
          xml.ownerName ENV['USER']
          xml.docs 'http://dev.opml.org/spec2.html'
        }
        xml.body {
          uris.each do |uri|
            xml.outline(text: uri, type: feed_type(uri), xmlUrl: uri)
          end
        }
      }
    }
  end
end
feed_type(url) click to toggle source

Adjusted from github.com/yugui/rubycommitters/blob/master/opml-generator.rb

# File lib/feed2email/opml_exporter.rb, line 53
def feed_type(url)
  uri = nil
  redirects = 0

  loop do
    uri = URI.parse(url)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = (uri.scheme == 'https')

    begin
      response = http.head(uri.request_uri)
    rescue
      break
    end

    if response.code =~ /\A3\d\d\z/
      redirects += 1
      return unless response['location'] && redirects <= MAX_REDIRECTS
      url = response['location']
      next
    end

    case response['content-type'][/[^;]+/]
    when 'text/rss', 'text/rss+xml', 'application/rss+xml',
         'application/rdf+xml', 'application/xml', 'text/xml'
      return 'rss'
    when 'text/atom', 'text/atom+xml', 'application/atom+xml'
      return 'atom'
    else
      break
    end
  end

  case File.extname(uri.path)
  when '.rdf', '.rss'
    return 'rss'
  when '.atom'
    return 'atom'
  end

  case File.basename(uri.path)
  when 'rss.xml', 'rdf.xml'
    return 'rss'
  when 'atom.xml'
    return 'atom'
  else
    return
  end
end
uris() click to toggle source
# File lib/feed2email/opml_exporter.rb, line 103
def uris; @uris end
xml() click to toggle source
# File lib/feed2email/opml_exporter.rb, line 105
def xml
  builder.to_xml
end