Class: KindleHighlights::Book

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(asin:, author:, title:, mechanize_agent: nil) ⇒ Book

Returns a new instance of Book.


14
15
16
17
18
19
# File 'lib/kindle_highlights/book.rb', line 14

def initialize(asin:, author:, title:, mechanize_agent: nil)
  @asin = asin
  @author = author
  @title = title
  @mechanize_agent = mechanize_agent
end

Instance Attribute Details

#asinObject

Returns the value of attribute asin


3
4
5
# File 'lib/kindle_highlights/book.rb', line 3

def asin
  @asin
end

#authorObject

Returns the value of attribute author


3
4
5
# File 'lib/kindle_highlights/book.rb', line 3

def author
  @author
end

#titleObject

Returns the value of attribute title


3
4
5
# File 'lib/kindle_highlights/book.rb', line 3

def title
  @title
end

Class Method Details

.from_html_elements(html_element:, mechanize_agent:) ⇒ Object


5
6
7
8
9
10
11
12
# File 'lib/kindle_highlights/book.rb', line 5

def self.from_html_elements(html_element:, mechanize_agent:)
  new(
    mechanize_agent: mechanize_agent,
    asin: html_element.attributes["id"].value.squish,
    title: html_element.children.search("h2").first.text.squish,
    author: html_element.children.search("p").first.text.split(":").last.strip.squish
  )
end

Instance Method Details

#highlights_from_amazonObject


29
30
31
32
33
# File 'lib/kindle_highlights/book.rb', line 29

def highlights_from_amazon
  return [] unless mechanize_agent.present?

  @highlights ||= fetch_highlights_from_amazon
end

#inspectObject


25
26
27
# File 'lib/kindle_highlights/book.rb', line 25

def inspect
  "<#{self.class}: #{inspectable_vars}>"
end

#to_sObject


21
22
23
# File 'lib/kindle_highlights/book.rb', line 21

def to_s
  "#{title} by #{author}"
end