class Mako::SubscriptionListWriter

Attributes

destination[R]
feeds[R]

Public Class Methods

new(args) click to toggle source
# File lib/mako/subscription_list_writer.rb, line 9
def initialize(args)
  @feeds = args.fetch(:feeds)
  @destination = args.fetch(:destination)
end

Public Instance Methods

append_and_write() click to toggle source

Appends the new subscriptions to the subscription list and writes the results out to the file.

# File lib/mako/subscription_list_writer.rb, line 16
def append_and_write
  contents = append_and_render
  File.open(destination, 'w+', encoding: 'utf-8') do |f|
    f.write(contents)
  end
end

Private Instance Methods

append_and_render() click to toggle source

Returns the rendered string for the correct file type.

@return [String]

# File lib/mako/subscription_list_writer.rb, line 28
def append_and_render
  loaded_list = SubscriptionListParser.new(list: destination)
  case File.extname destination
  when '.xml' || '.opml'
    render_opml(loaded_list)
  when '.json'
    render_json(loaded_list)
  when '.txt'
    render_txt(loaded_list)
  end
end
render_json(list) click to toggle source

Append feeds to current subscription list and return a JSON array

@return [String]

# File lib/mako/subscription_list_writer.rb, line 51
def render_json(list)
  (list.parse + feeds).to_json
end
render_opml(list) click to toggle source

Append feeds to current subscription list and return XML document

@return [String]

# File lib/mako/subscription_list_writer.rb, line 58
def render_opml(list)
  document = Nokogiri::XML(list.load_list)
  feeds.each do |feed_url|
    node = "<outline xmlUrl='#{feed_url}' />\n"
    document.xpath("//outline[@text='Subscriptions']").last.add_child node
  end
  formatted_no_decl = Nokogiri::XML::Node::SaveOptions::FORMAT +
                      Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
  document.to_xml(encoding: 'utf-8', save_with: formatted_no_decl)
end
render_txt(list) click to toggle source

Append feeds to current subscription list and return a string separated by n characters

@return [String]

# File lib/mako/subscription_list_writer.rb, line 44
def render_txt(list)
  (list.parse + feeds).join("\n")
end