Class: Readwise::Client
- Inherits:
-
Object
- Object
- Readwise::Client
- Defined in:
- lib/readwise/client.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- BASE_URL =
"https://readwise.io/api/v2/"
Instance Method Summary collapse
- #add_highlight_tag(highlight:, tag:) ⇒ Object
- #create_highlight(highlight:) ⇒ Object
- #create_highlights(highlights: []) ⇒ Object
- #export(updated_after: nil, book_ids: []) ⇒ Object
- #get_book(book_id:) ⇒ Object
- #get_highlight(highlight_id:) ⇒ Object
-
#initialize(token: nil) ⇒ Client
constructor
A new instance of Client.
- #remove_highlight_tag(highlight:, tag:) ⇒ Object
- #update_highlight(highlight:, update:) ⇒ Object
- #update_highlight_tag(highlight:, tag:) ⇒ Object
Constructor Details
#initialize(token: nil) ⇒ Client
Returns a new instance of Client.
13 14 15 16 17 |
# File 'lib/readwise/client.rb', line 13 def initialize(token: nil) raise ArgumentError unless token @token = token.to_s end |
Instance Method Details
#add_highlight_tag(highlight:, tag:) ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/readwise/client.rb', line 60 def add_highlight_tag(highlight:, tag:) raise ArgumentError unless tag.is_a?(Readwise::Tag) url = BASE_URL + "highlights/#{highlight.highlight_id}/tags" payload = tag.serialize.select { |k, v| k == :name } res = post_readwise_request(url, payload: payload) transform_tag(res) end |
#create_highlight(highlight:) ⇒ Object
19 20 21 |
# File 'lib/readwise/client.rb', line 19 def create_highlight(highlight:) create_highlights(highlights: [highlight]).first end |
#create_highlights(highlights: []) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/readwise/client.rb', line 23 def create_highlights(highlights: []) raise ArgumentError unless highlights.all? { |item| item.is_a?(Readwise::HighlightCreate) } return [] unless highlights.any? url = BASE_URL + 'highlights/' payload = { highlights: highlights.map(&:serialize) } res = post_readwise_request(url, payload: payload) modified_ids = res.map { |book| book['modified_highlights'] }.flatten modified_ids.map { |id| get_highlight(highlight_id: id) } end |
#export(updated_after: nil, book_ids: []) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/readwise/client.rb', line 90 def export(updated_after: nil, book_ids: []) resp = export_page(updated_after: updated_after, book_ids: book_ids) next_page_cursor = resp[:next_page_cursor] results = resp[:results] while next_page_cursor resp = export_page(updated_after: updated_after, book_ids: book_ids, page_cursor: next_page_cursor) results.concat(resp[:results]) next_page_cursor = resp[:next_page_cursor] end results.sort_by(&:highlighted_at_time) end |
#get_book(book_id:) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/readwise/client.rb', line 82 def get_book(book_id:) url = BASE_URL + "books/#{book_id}" res = get_readwise_request(url) transform_book(res) end |
#get_highlight(highlight_id:) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/readwise/client.rb', line 36 def get_highlight(highlight_id:) url = BASE_URL + "highlights/#{highlight_id}" res = get_readwise_request(url) transform_highlight(res) end |
#remove_highlight_tag(highlight:, tag:) ⇒ Object
54 55 56 57 58 |
# File 'lib/readwise/client.rb', line 54 def remove_highlight_tag(highlight:, tag:) url = BASE_URL + "highlights/#{highlight.highlight_id}/tags/#{tag.tag_id}" delete_readwise_request(url) end |
#update_highlight(highlight:, update:) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/readwise/client.rb', line 44 def update_highlight(highlight:, update:) raise ArgumentError unless update.is_a?(Readwise::HighlightUpdate) url = BASE_URL + "highlights/#{highlight.highlight_id}" res = patch_readwise_request(url, payload: update.serialize) transform_highlight(res) end |
#update_highlight_tag(highlight:, tag:) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/readwise/client.rb', line 71 def update_highlight_tag(highlight:, tag:) raise ArgumentError unless tag.is_a?(Readwise::Tag) url = BASE_URL + "highlights/#{highlight.highlight_id}/tags/#{tag.tag_id}" payload = tag.serialize.select { |k, v| k == :name } res = patch_readwise_request(url, payload: payload) transform_tag(res) end |