Sha256: 00d6b0e2ca74ba4f3ea7e9c902af92112b159e45c39cfc1d8d3c2cd9b7ab195c

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

module CampfireExport
  class Room
    include CampfireExport::IO
    attr_accessor :id, :name, :created_at, :last_update

    def initialize(room_xml)
      @id         = room_xml.xpath('id').text
      @name       = room_xml.xpath('name').text
      created_utc = DateTime.parse(room_xml.xpath('created-at').text)
      @created_at = Account.timezone.utc_to_local(created_utc)
    end

    def export(start_date=nil, end_date=nil)
      # Figure out how to do the least amount of work while still conforming
      # to the requester's boundary dates.
      find_last_update
      start_date.nil? ? date = created_at      : date = [start_date, created_at].max
      end_date.nil?   ? end_date = last_update : end_date = [end_date, last_update].min

      while date <= end_date
        transcript = Transcript.new(self, date)
        transcript.export

        # Ensure that we stay well below the 37signals API limits.
        sleep(1.0/10.0)
        date = date.next
      end
    end

    private
      def find_last_update
        begin
          last_message = Nokogiri::XML get("/room/#{id}/recent.xml?limit=1").body
          update_utc   = DateTime.parse(last_message.xpath('/messages/message[1]/created-at').text)
          @last_update = Account.timezone.utc_to_local(update_utc)
        rescue => e
          log(:error,
              "couldn't get last update in #{name} (defaulting to today)",
              e)
          @last_update = Time.now
        end
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stackbuilders-campfire_export-0.5.1 lib/campfire_export/room.rb