Class: QuickBase::RSSGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/QuickBaseRSSGenerator.rb

Overview

This class generates RSS data from specific fields in one or more QuickBase tables. It is intended to make standard RSS text that can be displayed by RSS readers or processed by other utilities.

See the test() method at the bottom of this file for an example of using this class.

Note that by default this class sorts the RSS items by <pubDate>

descending order (i.e. most recent first); this means that items generated from records from different tables can be interspersed which each other. Some RSS readers do not appear to interpret <pubDate> correctly and therefore don’t sort items correctly by themselves. Use ‘unSorted’ to prevent this class from sorting <items>. Also note that the sort order of records retrieved from tables can be set separately for each table, which is useful if the output is not going to be processed by an RSS readser.

Defined Under Namespace

Classes: Table

Instance Method Summary collapse

Constructor Details

#initialize(qbc) ⇒ RSSGenerator

Returns a new instance of RSSGenerator.



84
85
86
87
88
89
90
91
92
93
# File 'lib/QuickBaseRSSGenerator.rb', line 84

def initialize( qbc )
    raise "Bad qbc parameter. qbc must be an instance of QuickBase::Client." if !qbc.is_a?( Client )
    @qbc = qbc
    raise "Please sign into QuickBase before using the RSSGenerator class" if @qbc.ticket.nil?
    @items = Hash.new
    @sortableItems = Hash.new
    @sorted = true
    @tables = Array.new
    @timeNow = @namespace = ""
end

Instance Method Details

#addTable(dbid, tableName, fields, query = nil, qid = nil, qname = nil, numRecords = 0) ⇒ Object

Add a QuickBase table to the list of tables from which to generate RSS

  • dbid = QuickBase table dbid

  • tableName = the name of the QuickBase table as it should appear in the generated RSS

  • fields = Hash of RSS fields and the IDs of QuickBase fields , e.g. { “description” => “10”, “myRSSfield” => “13” }

  • query, qid, qname = query parameters passed directly to QuickBase::Client,doQuery()

  • numItems = the number of records to retrieve from QuickBase



230
231
232
233
# File 'lib/QuickBaseRSSGenerator.rb', line 230

def addTable( dbid, tableName, fields, query = nil, qid = nil, qname = nil, numRecords = 0 )
   t = Table.new( dbid, tableName, fields, query, qid, qname, numRecords )
   @tables << t
end

#appendTimeToDescriptionObject

Insert the current time at the end of the descripton for the RSS feed generated by this class.



202
203
204
# File 'lib/QuickBaseRSSGenerator.rb', line 202

def appendTimeToDescription
   @timeNow = " (#{Time.now})"
end


218
219
220
221
# File 'lib/QuickBaseRSSGenerator.rb', line 218

def footer
   text =   "  </channel>\n"
   text << " </rss>\n"
end

#generateRSSItems(table) ⇒ Object

Generates RSS <item>s from a filtered list of records and fields from a table.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/QuickBaseRSSGenerator.rb', line 96

def generateRSSItems( table )

   @qbc.doQuery( table.dbid, table.query, table.qid, table.qname, table.clist, table.slist, "structured", table.options )

   @qbc.eachRecord { |r|

      pubDate = ""
      title = ""
      description = ""
      link = ""
      otherFieldValues = ""
      itemText = ""
      sortValue = 0
   
      @qbc.eachField(r){ |f|
             case f.attributes[ "id" ]
                 when table.fields[ "title" ] 
                     title = f.text if f.has_text?
                     next
                 when table.fields[ "description" ] 
                     description = f.text if f.has_text?
                     next
                 when table.fields[ "pubDate" ] 
                     pubDate = f.text if f.has_text?
                     next
                 when table.fields[ "link" ] 
                     link = f.text if f.has_text?
                     next
                 when table.slist 
                     sortValue = f.text if f.has_text? and @sorted
                     next
             end
             table.optionalFields.each{ |fid,fieldName|
                if f.attributes["id"] == fid and f.has_text?
                      field, value = onSetItemField( "#{@namespace}#{fieldName}", f.text )
                      otherFieldValues << "     <#{field}>#{@qbc.encodeXML(value)}</#{field}>\n"
                end   
             }
       }
       
       titleHash = "#{table.tableName}(#{link}): #{description[0..20]}..."
       link = "https://www.quickbase.com/db/#{table.dbid}?a=dr&rid=#{link}"

       itemText << "    <item>\n"
       
       title << " (#{table.tableName})"
       field, value = onSetItemField( "#{@namespace}title", title )
       itemText << "     <#{field}>#{@qbc.encodeXML(value)}</#{field}>\n"
       
       field, value = onSetItemField( "#{@namespace}pubDate", @qbc.formatDate(pubDate, "%a, %d %b %Y %H:%M PST" ) )
       itemText << "     <#{field}>#{value}</#{field}>\n"

       guid = link.dup
       field, value = onSetItemField( "#{@namespace}link", link )
       itemText << "     <#{field}>#{@qbc.encodeXML(value)}</#{field}>\n"
       
       field, value = onSetItemField( "#{@namespace}guid", guid )
       itemText << "     <#{field}>#{@qbc.encodeXML(value)}</#{field}>\n"
       
       field, value = onSetItemField( "#{@namespace}description", description )
       itemText << "     <#{field}>#{@qbc.encodeXML(value)}</#{field}>\n"
       
       itemText << otherFieldValues
       itemText << "    </item>\n"
       
       @items[titleHash] = itemText
       @sortableItems[titleHash] = sortValue
   
   }
   
end

#generateRSStextObject

Generate all the RSS text for all the tables.



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/QuickBaseRSSGenerator.rb', line 236

def generateRSStext
   rssText = ""
   if @tables.length > 0
      rssText = header
      rssText << @title if @title
      rssText << @link if @link
      rssText << @description if @description
      @tables.each{ |table| generateRSSItems( table ) }
      if @sorted
         sortedItems = @sortableItems.sort{|a,b| b[1]<=>a[1] if a[1] and b[1] }
         sortedItems.each{ |item| rssText << @items[item[0]] }
      else   
         @items.each{ |item| rssText << @items[item[0]] }
      end
      rssText << footer
   else
      raise "Call addTable() one or more times before calling generateRSS"
   end
   rssText
end

#headerObject



212
213
214
215
216
# File 'lib/QuickBaseRSSGenerator.rb', line 212

def header 
   text =  "<?xml version=\"1.0\" ?>\n"
   text << " <rss version=\"2.0\">\n"
   text << "  <channel>\n"
end

#onSetItemField(field, value) ⇒ Object

To modify item field names or values before they are inserted into the RSS output, derive from this class, override this method, and modify the return values.



170
171
172
# File 'lib/QuickBaseRSSGenerator.rb', line 170

def onSetItemField( field, value )
   return field, value
end

#setDescription(description, appendTime = true) ⇒ Object

Set the description for the RSS feed generated by this class.



207
208
209
210
# File 'lib/QuickBaseRSSGenerator.rb', line 207

def setDescription( description, appendTime = true )
   appendTimeToDescription if appendTime
   @description = "   <#{@namespace}description>#{@qbc.encodeXML(description)}#{@timeNow}</#{@namespace}description>\n"
end

Set the link for the RSS feed generated by this class. Set isDBID = false if this is not a QuickBase dbid.



192
193
194
195
196
197
198
# File 'lib/QuickBaseRSSGenerator.rb', line 192

def setLink( link, isDBID = true )
   if isDBID
      @link = "   <#{@namespace}link>https://www.quickbase.com/db/#{link}</#{@namespace}link>\n"
   else
      @link = "   <#{@namespace}link>#{@qbc.encodeXML(link)}</#{@namespace}link>\n"
   end      
end

#setTitle(title) ⇒ Object

Set the title for the RSS feed generated by this class.



186
187
188
# File 'lib/QuickBaseRSSGenerator.rb', line 186

def setTitle( title )
   @title = "   <#{@namespace}title>#{@qbc.encodeXML(title)}</#{@namespace}title>\n"
end

#unSortedObject

Items are sorted by <pubDate> unless this method is called.



181
182
183
# File 'lib/QuickBaseRSSGenerator.rb', line 181

def unSorted
   @sorted = false
end

#useNamespaceObject

Prepend ‘quickbase:’ namespace to all data from QuickBase. intended for use by RSS processors other than RSS Readers.



176
177
178
# File 'lib/QuickBaseRSSGenerator.rb', line 176

def useNamespace
   @namespace = "quickbase:"
end