class DayOneKindle::ClippingParser

Constants

REGEX

Attributes

raw[R]

Public Class Methods

new(raw) click to toggle source
# File lib/dayone-kindle/clipping_parser.rb, line 11
def initialize(raw)
  @raw = raw
end

Public Instance Methods

highlight() click to toggle source
# File lib/dayone-kindle/clipping_parser.rb, line 15
def highlight
  return unless metadata.match(/Highlight/i)
  return unless quote

  Highlight.new(
    book: book,
    highlight: quote,
    time: time,
    page: page,
    location: location,
    authors: authors
  )
end

Private Instance Methods

authors() click to toggle source
# File lib/dayone-kindle/clipping_parser.rb, line 66
def authors
  @authors ||= begin
    string = matches.try(:[], :authors)
    string && !string.empty? && string.split(';')
  end
end
book() click to toggle source
# File lib/dayone-kindle/clipping_parser.rb, line 31
def book
  @book ||= begin
    b = matches.try(:[], :book)
    b && !b.empty? && b.strip
  end
end
location() click to toggle source
# File lib/dayone-kindle/clipping_parser.rb, line 59
def location
  @location ||= begin
    location = metadata.match(/location (?<location>[\d-]+)/i).try(:[], :location)
    location && !location.empty? && location
  end
end
matches() click to toggle source
# File lib/dayone-kindle/clipping_parser.rb, line 77
def matches
  @matches ||= raw.match(REGEX)
end
metadata() click to toggle source
# File lib/dayone-kindle/clipping_parser.rb, line 73
def metadata
  @metadata ||= matches[:metadata]
end
page() click to toggle source
# File lib/dayone-kindle/clipping_parser.rb, line 52
def page
  @page ||= begin
    page = metadata.match(/page (?<page>\d+)/i).try(:[], :page)
    page && !page.empty? && page.to_i
  end
end
quote() click to toggle source
# File lib/dayone-kindle/clipping_parser.rb, line 38
def quote
  @quote ||= begin
    q = matches.try(:[], :quote)
    q && !q.empty? && q && q.gsub(/ \u00a0/, "\n\n").strip
  end
end
time() click to toggle source
# File lib/dayone-kindle/clipping_parser.rb, line 45
def time
  @time ||= begin
    string = metadata.match(/added on (?<time>.+)/i).try(:[], :time)
    string && !string.empty? && Time.parse(string.strip)
  end
end