Class: Crawlable::Feed

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Feed

Returns a new instance of Feed.



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/crawlable/feed.rb', line 73

def initialize(*args, &block)
  options = args.extract_options!
  
  self.name    = args.shift
  
  options.each do |k, v|
    self.send(k, v) if self.respond_to?(k)
  end
  
  instance_eval(&block)
  
  self
end

Class Attribute Details

.call_optionsObject

Returns the value of attribute call_options.



5
6
7
# File 'lib/crawlable/feed.rb', line 5

def call_options
  @call_options
end

.feedsObject

Returns the value of attribute feeds.



5
6
7
# File 'lib/crawlable/feed.rb', line 5

def feeds
  @feeds
end

.optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/crawlable/feed.rb', line 5

def options
  @options
end

Instance Attribute Details

Returns the value of attribute copyright.



71
72
73
# File 'lib/crawlable/feed.rb', line 71

def copyright
  @copyright
end

#description(string = nil) ⇒ Object

Returns the value of attribute description.



71
72
73
# File 'lib/crawlable/feed.rb', line 71

def description
  @description
end

#master(string = nil) ⇒ Object

Returns the value of attribute master.



71
72
73
# File 'lib/crawlable/feed.rb', line 71

def master
  @master
end

#nameObject

Returns the value of attribute name.



71
72
73
# File 'lib/crawlable/feed.rb', line 71

def name
  @name
end

#path(value = nil) ⇒ Object

Returns the value of attribute path.



71
72
73
# File 'lib/crawlable/feed.rb', line 71

def path
  @path
end

#title(string = nil) ⇒ Object

Returns the value of attribute title.



71
72
73
# File 'lib/crawlable/feed.rb', line 71

def title
  @title
end

#updated_atObject

Returns the value of attribute updated_at.



71
72
73
# File 'lib/crawlable/feed.rb', line 71

def updated_at
  @updated_at
end

#url(string = nil) ⇒ Object

Returns the value of attribute url.



71
72
73
# File 'lib/crawlable/feed.rb', line 71

def url
  @url
end

Class Method Details

.define!(*args, &block) ⇒ Object



16
17
18
19
20
# File 'lib/crawlable/feed.rb', line 16

def define!(*args, &block)
  self.options = args.extract_options!#.merge(:run => args.shift)
  
  instance_eval(&block) if block_given?
end

.find(path, directory) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/crawlable/feed.rb', line 22

def find(path, directory)
  if path =~ /(\/feed\/?)(atom|rss2|rss|rdf)?$/i
    feed   = $1
    format = $2

    format = format.blank? ? "rss" : format.to_s
    file = path.gsub(format, "").split("/").delete_if {|i| i.blank? }[0..-2].join("/")

    return unless self.paths.include?(file)
    file = file.split("/").join("_")
    file = File.join(directory, "feeds", "#{file}.#{format}")
    return file
  end
end

.method_missing(meth, *args, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/crawlable/feed.rb', line 53

def method_missing(meth, *args, &block)
  if block_given?
    if !self.call_options.blank?
      if call_options.has_key?(meth.to_sym)
        self.feeds << self.new(meth, *args, &block)
      else
        super(meth, *args, &block)
      end
    else
      self.feeds << self.new(meth, *args, &block)
    end
  else
    super(meth, *args, &block)
  end
end

.parse!(path, options = {}) ⇒ Object



37
38
39
40
41
42
# File 'lib/crawlable/feed.rb', line 37

def parse!(path, options = {})
  path ||= File.join(::Rails.root, 'config/initializers/feeds.rb')
  self.call_options = options.symbolize_keys
  eval(IO.read(path))
  self.call_options = nil
end

.pathsObject



12
13
14
# File 'lib/crawlable/feed.rb', line 12

def paths
  feeds.map(&:path)
end

.process!(from, to, *formats, &block) ⇒ Object



48
49
50
51
# File 'lib/crawlable/feed.rb', line 48

def process!(from, to, *formats, &block)
  parse!(from)
  write(to, *formats)
end

.write(to, *formats) ⇒ Object



44
45
46
# File 'lib/crawlable/feed.rb', line 44

def write(to, *formats)
  self.feeds.each { |feed| feed.write(to, *formats) }
end

Instance Method Details

#author(string = nil) ⇒ Object



113
114
115
116
# File 'lib/crawlable/feed.rb', line 113

def author(string = nil)
  @author = string unless string.nil?
  @author
end

#entriesObject



123
124
125
# File 'lib/crawlable/feed.rb', line 123

def entries
  @entries ||= []
end

#entry(path, *args, &block) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/crawlable/feed.rb', line 127

def entry(path, *args, &block)
  options = args.extract_options!

  result = options.dup
  result.merge!(:url => path)
  
  self.entries.push(result)
end

#to_atomObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/crawlable/feed.rb', line 169

def to_atom
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.feed "xmlns" => "http://www.w3.org/2005/Atom" do
      xml.title   self.title
      #xml.link    "rel" => "self", "href" => url_for(:only_path => false, :controller => 'feeds', :action => 'atom')
      #xml.link    "rel" => "alternate", "href" => url_for(:only_path => false, :controller => 'posts')
      #xml.id      url_for(:only_path => false, :controller => 'posts')
      xml.updated self.updated_at.strftime "%Y-%m-%dT%H:%M:%SZ" if self.updated_at
      xml.author  { xml.name self.author }
      
      self.entries.each do |entry|
        xml.entry do
          xml.title   entry[:title]
          #xml.link    "rel" => "alternate", "href" => url_for(:only_path => false, :controller => 'posts', :action => 'show', :id => entry[:id])
          #xml.id      url_for(:only_path => false, :controller => 'posts', :action => 'show', :id => entry[:id])
          xml.updated entry[:updated_at].strftime "%Y-%m-%dT%H:%M:%SZ"
          xml.author  { xml.name entry[:author] } if entry[:author]
          xml.summary "Post summary"
          xml.content "type" => "html" do
            #xml.text! render(:partial => "posts/post", :post => post)
          end
        end
      end

    end
  end
  builder.to_xml
end

#to_rssObject



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
167
# File 'lib/crawlable/feed.rb', line 136

def to_rss
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.rss "xmlns:dc" => "http://purl.org/dc/elements/1.1/" do
      xml.channel do
        xml.title           self.title
        xml.link            self.url
        xml.description     self.description
        
        self.entries.each do |entry|
          xml.item do
            xml.title       entry[:title]
            xml.link        entry[:url]
            xml.description entry[:description]
            xml.guid        entry[:guid] || entry[:url]
            xml.author      entry[:author] unless entry[:author].blank?
            entry[:categories].each do |category|
              xml.category category
            end unless entry[:categories].blank?
            xml.comments    entry[:comments] unless entry[:comments].blank?
            xml.enclosure(
              :url    => entry[:asset],
              :length => IO.size(entry[:asset]).to_s,
              :type   => entry[:asset_type]
            ) unless entry[:asset].blank?
            xml.webMaster   self.master unless self.master.blank?
          end
        end
      end
    end
  end
  builder.to_xml
end

#write(to, *formats) ⇒ Object



198
199
200
201
202
203
204
205
# File 'lib/crawlable/feed.rb', line 198

def write(to, *formats)
  formats.each do |format|
    FileUtils.mkdir_p(File.dirname(to))
    File.open("#{to}.#{format.to_s}", 'wb') do |file|
      file.puts send("to_#{format.to_s}")
    end
  end
end