Class: HentryConsumer::HFeed

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html, options = {}) ⇒ HFeed

Returns a new instance of HFeed.



7
8
9
10
11
12
# File 'lib/hentry_consumer/h_feed.rb', line 7

def initialize(html, options={})
  @options = options
  @html = Nokogiri::HTML(open(html, headers).read)
  @entries = []
  parse_html
end

Instance Attribute Details

#entriesObject

Returns the value of attribute entries.



5
6
7
# File 'lib/hentry_consumer/h_feed.rb', line 5

def entries
  @entries
end

#htmlObject

Returns the value of attribute html.



5
6
7
# File 'lib/hentry_consumer/h_feed.rb', line 5

def html
  @html
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/hentry_consumer/h_feed.rb', line 5

def options
  @options
end

Instance Method Details

#headersObject



48
49
50
51
52
# File 'lib/hentry_consumer/h_feed.rb', line 48

def headers
  h = {}
  h["If-Modified-Since"] = CGI.rfc1123_date(last_modified_at) if last_modified_at
  h
end

#last_modified_atObject



42
43
44
45
46
# File 'lib/hentry_consumer/h_feed.rb', line 42

def last_modified_at
  time = @options[:last_modified_at] || return
  time = Time.parse(time) if time.is_a? String
  time.to_time
end

#parse_htmlObject



14
15
16
17
18
19
20
# File 'lib/hentry_consumer/h_feed.rb', line 14

def parse_html
  self.html.css(".h-entry").each do |hentry|
    entry = HEntry.new(hentry.children)
    next if entry.older_than(last_modified_at)
    self.entries << entry
  end
end

#to_hashObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/hentry_consumer/h_feed.rb', line 27

def to_hash
  {:items =>
    [{
      :type => ["h-feed"],
      :properties => {
        :entries => self.entries
      }
    }]
  }
end

#to_htmlObject Also known as: to_s



22
23
24
# File 'lib/hentry_consumer/h_feed.rb', line 22

def to_html
  self.html.css(".h-entry").collect(&:to_html).join
end

#to_json(*a) ⇒ Object



38
39
40
# File 'lib/hentry_consumer/h_feed.rb', line 38

def to_json(*a)
  to_hash.to_json(a)
end