Class: ReadwiseCurator::HighlightService
- Inherits:
-
Object
- Object
- ReadwiseCurator::HighlightService
- Defined in:
- lib/readwise_curator/highlight_service.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#site ⇒ Object
readonly
Returns the value of attribute site.
Instance Method Summary collapse
- #fetch_highlights_for_book(book_id) ⇒ Object
-
#initialize(site, options = {}) ⇒ HighlightService
constructor
A new instance of HighlightService.
- #load_existing_selections(book_id) ⇒ Object
- #save_curated_highlights(book_id, highlights) ⇒ Object
Constructor Details
#initialize(site, options = {}) ⇒ HighlightService
Returns a new instance of HighlightService.
12 13 14 15 16 17 18 |
# File 'lib/readwise_curator/highlight_service.rb', line 12 def initialize(site, = {}) @site = site @api_key = [:api_key] || site.config.readwise_curator&.api_key || ENV.fetch( "READWISE_TOKEN", nil ) raise "Readwise API key not configured" unless @api_key end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
10 11 12 |
# File 'lib/readwise_curator/highlight_service.rb', line 10 def api_key @api_key end |
#site ⇒ Object (readonly)
Returns the value of attribute site.
10 11 12 |
# File 'lib/readwise_curator/highlight_service.rb', line 10 def site @site end |
Instance Method Details
#fetch_highlights_for_book(book_id) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/readwise_curator/highlight_service.rb', line 20 def fetch_highlights_for_book(book_id) highlights = [] next_url = "https://readwise.io/api/v2/highlights/?book_id=#{book_id}" while next_url response = make_request(next_url) highlights.concat(response["results"]) next_url = response["next"] end highlights end |
#load_existing_selections(book_id) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/readwise_curator/highlight_service.rb', line 44 def load_existing_selections(book_id) curated_dir = File.join(site.root_dir, "src/_data/readwise/curated_highlights") curated_file = File.join(curated_dir, "#{book_id}.json") return [] unless File.exist?(curated_file) JSON.parse(File.read(curated_file)).map { |h| h["id"] } end |
#save_curated_highlights(book_id, highlights) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/readwise_curator/highlight_service.rb', line 33 def save_curated_highlights(book_id, highlights) curated_dir = File.join(site.root_dir, "src/_data/readwise/curated_highlights") FileUtils.mkdir_p(curated_dir) curated_file = File.join(curated_dir, "#{book_id}.json") minimal = highlights.map { |h| h.slice("id", "text", "note", "highlighted_at") } File.write(curated_file, JSON.pretty_generate(minimal)) curated_file end |