Generate OPML files for following web-things
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

opml.rb 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # frozen_string_literal: true
  2. require 'nokogiri'
  3. class OPML
  4. PROPERTIES = %w[title date_created date_modified
  5. owner_name owner_email docs].freeze
  6. attr_accessor :title, :date_created, :date_modified, :owner_name, :owner_email, :docs
  7. def initialize(&block)
  8. instance_eval(&block)
  9. @docs = 'http://dev.opml.org/spec2.html'
  10. @outlines = []
  11. end
  12. def camel_case(str)
  13. str.downcase.split('_').each_with_index.map do |v, i|
  14. i.zero? ? v : v.capitalize
  15. end.join
  16. end
  17. def add_outline(params)
  18. params = Hash[params.map { |k, v| [camel_case(k.to_s), v] }]
  19. @outlines.push params
  20. end
  21. def add_note_with_params(xml, attrs)
  22. attrs.each do |attr|
  23. val = send(attr.to_sym)
  24. key = camel_case(attr).to_sym
  25. xml.send(key, val) if val
  26. end
  27. end
  28. def xml
  29. Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
  30. xml.opml(version: '2.0') do
  31. xml.head do
  32. add_note_with_params xml, PROPERTIES
  33. end
  34. xml.body do
  35. xml.outline text: 'GitHub Releases' do
  36. @outlines.each do |outline|
  37. xml.outline outline
  38. end
  39. end
  40. end
  41. end
  42. end.to_xml
  43. end
  44. end