ringcentral-ruby/ringcentral-rss-ruby

View on GitHub
lib/ringcentral-rss/atom_feed.rb

Summary

Maintainability
A
0 mins
Test Coverage
require 'atom'

module RingCentral
  module RSS
    # Represents an Atom feed which loads records via RingCentral
    # message-store responses
    class AtomFeed
      attr_accessor :feed

      def initialize(response = nil, opts = {})
        if opts.key? :feed_name
          @feed_name = opts[:feed_name]
        else
          @feed_name = 'RingCentral Feed'
        end
        @feed = nil
        load_message_store_response(response) unless response.nil?
      end

      def load_message_store_response(response)
        @feed = Atom::Feed.new do |f|
          f.title = @feed_name
          f.links << Atom::Link.new(href: response.body['uri'].to_s)
          f.updated = Time.parse(response.headers['date'].to_s)
          response.body['records'].each do |message|
            f.entries << RingCentral::RSS::AtomEntry.new(message).entry
          end
        end
      end

      def to_xml
        !@feed.nil? && @feed.is_a?(Atom::Feed) ? @feed.to_xml : nil
      end
    end
  end
end