Class: KindleManager::HighlightsParser::BookWithNote
- Inherits:
-
Object
- Object
- KindleManager::HighlightsParser::BookWithNote
- Includes:
- Parsers::Common
- Defined in:
- lib/kindle_manager/parsers/highlights_parser.rb
Instance Method Summary collapse
- #asin ⇒ Object
- #author ⇒ Object
-
#count_summary ⇒ Object
This can be used to verify the count of hightlights and notes.
- #highlights ⇒ Object
- #highlights_and_notes ⇒ Object
- #highlights_count ⇒ Object
-
#initialize(node, options = {}) ⇒ BookWithNote
constructor
A new instance of BookWithNote.
- #inspect ⇒ Object
- #invalid? ⇒ Boolean
- #last_annotated_on ⇒ Object
- #notes ⇒ Object
- #notes_count ⇒ Object
- #title ⇒ Object
- #to_hash ⇒ Object
Methods included from Parsers::Common
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, = {}) @node = node @fetched_at = [:fetched_at] end |
Instance Method Details
#asin ⇒ Object
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 |
#author ⇒ Object
23 24 25 |
# File 'lib/kindle_manager/parsers/highlights_parser.rb', line 23 def @_author ||= @node.css('p.kp-notebook-metadata.a-size-base').first.text end |
#count_summary ⇒ Object
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 |
#highlights ⇒ Object
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_notes ⇒ Object
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.}\n#{node.to_html}\n" next end end.compact end end |
#highlights_count ⇒ Object
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 |
#inspect ⇒ Object
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
83 84 85 |
# File 'lib/kindle_manager/parsers/highlights_parser.rb', line 83 def invalid? !!(asin.blank? || count_summary['text'] =~ /--/) end |
#last_annotated_on ⇒ Object
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 |
#notes ⇒ Object
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_count ⇒ Object
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 |
#title ⇒ Object
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_hash ⇒ Object
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 |