Class: FeedMe::ParserBuilder

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

Direct Known Subclasses

StrictParserBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ParserBuilder

Returns a new instance of ParserBuilder.



85
86
87
88
89
90
91
92
93
94
95
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
# File 'lib/feedme.rb', line 85

def initialize(options={})
  @options = options
  
  # rss tags
	@rss_tags = [
	  {
		  :image     => nil,
      :textInput => nil,
      :skipHours => nil,
      :skipDays  => nil,
      :items     => [{ :rdf_Seq => nil }],
     #:item      => @rss_item_tags
		}
	]
  @rss_item_tags = [ {} ]

  #atom tags
  @atom_tags = [
    {
      :author       => nil,
      :contributor  => nil,
     #:entry        => @atom_entry_tags
    }
  ]
  @atom_entry_tags = [
    {
      :author       => nil, 
      :contributor  => nil
    }
  ]

  # tags whose value is a date
  @date_tags = [ :pubDate, :lastBuildDate, :published, :updated, :dc_date, :expirationDate ]
  
  # tags that can be used as the default value for a tag with attributes
  @value_tags = [ CONTENT_KEY, :href, :url ]
  
  # tags that don't become part of the parsed object tree
  @ghost_tags = [ :rdf_Seq ]

  # tag/attribute aliases
	@aliases = {
	  :items        => :item_array,
	  :item_array   => :entry_array,
	  :entries      => :entry_array,
	  :entry_array  => :item_array,
	  :link         => :'link+self'
	}
	
	# bang mods
	@default_transformation = [ :stripHtml ]
	@transformations = {}
	@transformation_fns = {
	  :stripHtml => proc {|str|     # remove all HTML tags
	    str.gsub(/<\/?[^>]*>/, "").strip 
	  },
	  :cleanHtml => proc {|str|     # clean HTML content using FeedNormalizer's HtmlCleaner class 
	    begin
	      require 'rubygems'
	      require 'feed-normalizer'
	      FeedNormalizer::HtmlCleaner.clean(str)
	    rescue
	      str
	    end  
	  }, 
	  :wrap => proc {|str, col|     # wrap text at a certain number of characters (respecting word boundaries)
	    str.gsub(/(.{1,#{col}})( +|$\n?)|(.{1,#{col}})/, "\\1\\3\n").strip 
	  },
	  :trunc => proc {|str, wordcount|  # truncate text, respecting word boundaries
	    str.trunc(wordcount.to_i)
    },
    :truncHtml => proc {|str, wordcount| # truncate text but leave enclosing HTML tags
      begin
        require 'text-helper'
        TextHelper::truncate_html(str, wordcount.to_i)
      rescue
        str
      end
    }
	}
end

Instance Attribute Details

#aliasesObject

Returns the value of attribute aliases.



80
81
82
# File 'lib/feedme.rb', line 80

def aliases
  @aliases
end

#atom_entry_tagsObject

Returns the value of attribute atom_entry_tags.



80
81
82
# File 'lib/feedme.rb', line 80

def 
  @atom_entry_tags
end

#atom_tagsObject

Returns the value of attribute atom_tags.



80
81
82
# File 'lib/feedme.rb', line 80

def atom_tags
  @atom_tags
end

#date_tagsObject

Returns the value of attribute date_tags.



80
81
82
# File 'lib/feedme.rb', line 80

def date_tags
  @date_tags
end

#default_transformationObject

Returns the value of attribute default_transformation.



80
81
82
# File 'lib/feedme.rb', line 80

def default_transformation
  @default_transformation
end

#ghost_tagsObject

Returns the value of attribute ghost_tags.



80
81
82
# File 'lib/feedme.rb', line 80

def ghost_tags
  @ghost_tags
end

#optionsObject

Returns the value of attribute options.



80
81
82
# File 'lib/feedme.rb', line 80

def options
  @options
end

#rss_item_tagsObject

Returns the value of attribute rss_item_tags.



80
81
82
# File 'lib/feedme.rb', line 80

def rss_item_tags
  @rss_item_tags
end

#rss_tagsObject

Returns the value of attribute rss_tags.



80
81
82
# File 'lib/feedme.rb', line 80

def rss_tags
  @rss_tags
end

#transformation_fnsObject

Returns the value of attribute transformation_fns.



80
81
82
# File 'lib/feedme.rb', line 80

def transformation_fns
  @transformation_fns
end

#transformationsObject

Returns the value of attribute transformations.



80
81
82
# File 'lib/feedme.rb', line 80

def transformations
  @transformations
end

#value_tagsObject

Returns the value of attribute value_tags.



80
81
82
# File 'lib/feedme.rb', line 80

def value_tags
  @value_tags
end

Instance Method Details

#all_atom_tagsObject

Prepare tag list for an Atom feed.



175
176
177
178
179
# File 'lib/feedme.rb', line 175

def all_atom_tags
  all_tags = atom_tags.dup
  all_tags[0][:entry] = .dup
  return all_tags
end

#all_rss_tagsObject

Prepare tag list for an RSS feed.



168
169
170
171
172
# File 'lib/feedme.rb', line 168

def all_rss_tags
  all_tags = rss_tags.dup
  all_tags[0][:item] = rss_item_tags.dup
  return all_tags
end

#emulate_atom!Object

Add aliases so that RSS feed elements can be accessed using the names of their Atom counterparts.



198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/feedme.rb', line 198

def emulate_atom!
  aliases.merge!({
    :rights       => :copyright,
    :content      => :description,
    :contributor  => :author,
    :id           => [ :guid_value, :link ],
    :author       => [ :managingEditor, :webMaster ],
    :updated      => [ :lastBuildDate, :pubDate ],
    :published    => [ :pubDate, :lastBuildDate ],
    :icon         => :'image/url',
    :logo         => :'image/url',
    :summary      => :'description_trunc'
  })
end

#emulate_rss!Object

Add aliases so that Atom feed elements can be accessed using the names of their RSS counterparts.



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/feedme.rb', line 183

def emulate_rss!
  aliases.merge!({
    :guid           => :id,       # this alias never actually gets used; see FeedData#id
    :copyright      => :rights,
    :pubDate        => [ :published, :updated ],
    :lastBuildDate  => [ :updated, :published ],
    :description    => [ :content, :summary ],
    :managingEditor => [ :'author/name', :'contributor/name' ],
    :webMaster      => [ :'author/name', :'contributor/name' ],
    :image          => [ :icon, :logo ]
  })
end

#parse(source) ⇒ Object



213
214
215
# File 'lib/feedme.rb', line 213

def parse(source)
 Parser.new(self, source, options)
end