Class: KindleManager::HighlightsParser::BookWithNote

Inherits:
Object
  • Object
show all
Includes:
Parsers::Common
Defined in:
lib/kindle_manager/parsers/highlights_parser.rb

Instance Method Summary collapse

Methods included from Parsers::Common

#parse_date

Constructor Details

#initialize(node, options = {}) ⇒ BookWithNote

Returns a new instance of BookWithNote.



6
7
8
9
# File 'lib/kindle_manager/parsers/highlights_parser.rb', line 6

def initialize(node, options = {})
  @node = node
  @fetched_at = options[:fetched_at]
end

Instance Method Details

#asinObject



15
16
17
# File 'lib/kindle_manager/parsers/highlights_parser.rb', line 15

def asin
  @_asin ||= @node.css('#kp-notebook-annotations-asin').first['value']
end

#authorObject



23
24
25
# File 'lib/kindle_manager/parsers/highlights_parser.rb', line 23

def author
  @_author ||= @node.css('p.kp-notebook-metadata.a-size-base').first.text
end

#count_summaryObject

This can be used to verify the count of hightlights and notes



67
68
69
70
71
72
73
# File 'lib/kindle_manager/parsers/highlights_parser.rb', line 67

def count_summary
  @_count_summary ||= begin
    text = @node.css('.kp-notebook-row-separator > .kp-notebook-metadata').last.text.strip
    a, b = text.split('|').map{|text| m = text.match(/\d+/); m.nil? ? nil : m[0].to_i }
    {'text' => text, 'highlights_count' => a, 'notes_count' => b}
  end
end

#highlightsObject



58
59
60
# File 'lib/kindle_manager/parsers/highlights_parser.rb', line 58

def highlights
  highlights_and_notes.reject{|e| e['highlight'].blank? }
end

#highlights_and_notesObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kindle_manager/parsers/highlights_parser.rb', line 39

def highlights_and_notes
  @_highlights_and_notes ||= begin
    # Excluding the first element which has book info
    @node.css('.a-spacing-base')[1..-1].map do |node|
      begin
        location = node.css('#kp-annotation-location').first['value'].to_i
        highlight_node = node.css('.kp-notebook-highlight').first
        highlight = highlight_node && highlight_node.css('#highlight').first.text
        color = highlight_node && highlight_node['class'].split.find{|v| v =~ /kp-notebook-highlight-/ }.split('-').last
        note = node.css('#note').first.text
        {'location' => location, 'highlight' => highlight, 'color' => color, 'note' => note}
      rescue => e
        puts "[DEBUG] #{e.class}: #{e.message}\n#{node.to_html}\n"
        next
      end
    end.compact
  end
end

#highlights_countObject



31
32
33
# File 'lib/kindle_manager/parsers/highlights_parser.rb', line 31

def highlights_count
  @_highlights_count ||= @node.css('.kp-notebook-highlight').size
end

#inspectObject



11
12
13
# File 'lib/kindle_manager/parsers/highlights_parser.rb', line 11

def inspect
  "#<#{self.class.name}:#{self.object_id} #{self.to_hash}>"
end

#invalid?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/kindle_manager/parsers/highlights_parser.rb', line 83

def invalid?
  !!(asin.blank? || count_summary['text'] =~ /--/)
end

#last_annotated_onObject



27
28
29
# File 'lib/kindle_manager/parsers/highlights_parser.rb', line 27

def last_annotated_on
  @_last_annotated_on ||= parse_date(@node.css('#kp-notebook-annotated-date').text)
end

#notesObject



62
63
64
# File 'lib/kindle_manager/parsers/highlights_parser.rb', line 62

def notes
  highlights_and_notes.reject{|e| e['note'].blank? }
end

#notes_countObject



35
36
37
# File 'lib/kindle_manager/parsers/highlights_parser.rb', line 35

def notes_count
  @_notes_count ||= @node.css('.kp-notebook-note').reject{|e| e['class'] =~ /aok-hidden/ }.size
end

#titleObject



19
20
21
# File 'lib/kindle_manager/parsers/highlights_parser.rb', line 19

def title
  @_title ||= @node.css('h3.kp-notebook-metadata').text
end

#to_hashObject



75
76
77
78
79
80
81
# File 'lib/kindle_manager/parsers/highlights_parser.rb', line 75

def to_hash
  hash = {}
  %w[asin title author last_annotated_on highlights_count notes_count highlights_and_notes].each do |f|
    hash[f] = send(f)
  end
  hash
end