Class: Readwise::Client
- Inherits:
-
Object
show all
- Defined in:
- lib/readwise/client.rb
Defined Under Namespace
Classes: Error, TooManyRequests
Constant Summary
collapse
- BASE_URL =
"https://readwise.io/api/v2/"
- V3_BASE_URL =
"https://readwise.io/api/v3/"
Instance Method Summary
collapse
Constructor Details
#initialize(token: nil) ⇒ Client
Returns a new instance of Client.
25
26
27
28
29
|
# File 'lib/readwise/client.rb', line 25
def initialize(token: nil)
raise ArgumentError unless token
@token = token.to_s
end
|
Instance Method Details
#add_highlight_tag(highlight:, tag:) ⇒ Object
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/readwise/client.rb', line 109
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_document(document:) ⇒ Object
31
32
33
34
35
36
37
38
|
# File 'lib/readwise/client.rb', line 31
def create_document(document:)
raise ArgumentError unless document.is_a?(Readwise::DocumentCreate)
url = V3_BASE_URL + 'save/'
res = post_readwise_request(url, payload: document.serialize)
get_document(document_id: res['id'])
end
|
#create_documents(documents: []) ⇒ Object
40
41
42
43
44
45
46
|
# File 'lib/readwise/client.rb', line 40
def create_documents(documents: [])
return [] unless documents.any?
documents.map do |document|
create_document(document: document)
end
end
|
#create_highlight(highlight:) ⇒ Object
68
69
70
|
# File 'lib/readwise/client.rb', line 68
def create_highlight(highlight:)
create_highlights(highlights: [highlight]).first
end
|
#create_highlights(highlights: []) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/readwise/client.rb', line 72
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
|
#daily_review ⇒ Object
139
140
141
142
143
144
|
# File 'lib/readwise/client.rb', line 139
def daily_review
url = BASE_URL + 'review/'
res = get_readwise_request(url)
transform_review(res)
end
|
#export(updated_after: nil, book_ids: []) ⇒ Object
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/readwise/client.rb', line 146
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
131
132
133
134
135
136
137
|
# File 'lib/readwise/client.rb', line 131
def get_book(book_id:)
url = BASE_URL + "books/#{book_id}"
res = get_readwise_request(url)
transform_book(res)
end
|
#get_document(document_id:) ⇒ Object
48
49
50
51
52
53
54
|
# File 'lib/readwise/client.rb', line 48
def get_document(document_id:)
url = V3_BASE_URL + "list?id=#{document_id}"
res = get_readwise_request(url)
res['results'].map { |item| transform_document(item) }.first
end
|
#get_documents(updated_after: nil, location: nil, category: nil) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/readwise/client.rb', line 56
def get_documents(updated_after: nil, location: nil, category: nil)
resp = documents_page(updated_after: updated_after, location: location, category: category)
next_page_cursor = resp[:next_page_cursor]
results = resp[:results]
while next_page_cursor
resp = documents_page(updated_after: updated_after, location: location, category: category, page_cursor: next_page_cursor)
results.concat(resp[:results])
next_page_cursor = resp[:next_page_cursor]
end
results.sort_by(&:created_at)
end
|
#get_highlight(highlight_id:) ⇒ Object
85
86
87
88
89
90
91
|
# File 'lib/readwise/client.rb', line 85
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
103
104
105
106
107
|
# File 'lib/readwise/client.rb', line 103
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
93
94
95
96
97
98
99
100
101
|
# File 'lib/readwise/client.rb', line 93
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
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/readwise/client.rb', line 120
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
|