diff --git a/lib/readwise/client.rb b/lib/readwise/client.rb index 268d4f3..3fab13e 100644 --- a/lib/readwise/client.rb +++ b/lib/readwise/client.rb @@ -44,12 +44,12 @@ def get_document(document_id:) res['results'].map { |item| transform_document(item) }.first end - def get_documents(updated_after: nil, location: nil, category: nil) - resp = documents_page(updated_after: updated_after, location: location, category: category) + def get_documents(updated_after: nil, location: nil, category: nil, tags: nil) + resp = documents_page(updated_after: updated_after, location: location, category: category, tags: tags) 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) + resp = documents_page(updated_after: updated_after, location: location, category: category, tags: tags, page_cursor: next_page_cursor) results.concat(resp[:results]) next_page_cursor = resp[:next_page_cursor] end @@ -148,8 +148,8 @@ def export(updated_after: nil, book_ids: []) private - def documents_page(page_cursor: nil, updated_after:, location:, category:) - parsed_body = get_documents_page(page_cursor: page_cursor, updated_after: updated_after, location: location, category: category) + def documents_page(page_cursor: nil, updated_after:, location:, category:, tags:) + parsed_body = get_documents_page(page_cursor: page_cursor, updated_after: updated_after, location: location, category: category, tags: tags) results = parsed_body.dig('results').map do |item| transform_document(item) end @@ -159,12 +159,15 @@ def documents_page(page_cursor: nil, updated_after:, location:, category:) } end - def get_documents_page(page_cursor: nil, updated_after:, location:, category:) - params = {} - params['updatedAfter'] = updated_after if updated_after - params['location'] = location if location - params['category'] = category if category - params['pageCursor'] = page_cursor if page_cursor + def get_documents_page(page_cursor: nil, updated_after:, location:, category:, tags:) + params = [] + params << ['updatedAfter', updated_after] if updated_after + params << ['location', location] if location + params << ['category', category] if category + if tags && tags.any? + tags.each { |tag| params << ['tag', tag] } + end + params << ['pageCursor', page_cursor] if page_cursor url = V3_BASE_URL + 'list/?' + URI.encode_www_form(params) get_readwise_request(url) diff --git a/spec/readwise/client_spec.rb b/spec/readwise/client_spec.rb index def52b6..d153d99 100644 --- a/spec/readwise/client_spec.rb +++ b/spec/readwise/client_spec.rb @@ -188,11 +188,11 @@ page1_with_cursor = page1_response.merge('nextPageCursor' => 'cursor123') expect(subject).to receive(:get_documents_page) - .with(updated_after: nil, location: nil, category: nil, page_cursor: nil) + .with(updated_after: nil, location: nil, category: nil, tags: nil, page_cursor: nil) .and_return(page1_with_cursor) expect(subject).to receive(:get_documents_page) - .with(updated_after: nil, location: nil, category: nil, page_cursor: 'cursor123') + .with(updated_after: nil, location: nil, category: nil, tags: nil, page_cursor: 'cursor123') .and_return(page2_response) documents = subject.get_documents @@ -210,11 +210,77 @@ it 'passes filter parameters' do expect(subject).to receive(:get_documents_page) - .with(updated_after: '2023-01-01', location: 'new', category: 'article', page_cursor: nil) + .with(updated_after: '2023-01-01', location: 'new', category: 'article', tags: nil, page_cursor: nil) .and_return(page1_response) subject.get_documents(updated_after: '2023-01-01', location: 'new', category: 'article') end + + it 'passes tags parameter' do + expect(subject).to receive(:get_documents_page) + .with(updated_after: nil, location: nil, category: nil, tags: ['tech', 'ruby'], page_cursor: nil) + .and_return(page1_response) + + subject.get_documents(tags: ['tech', 'ruby']) + end + + it 'handles single tag in array' do + expect(subject).to receive(:get_documents_page) + .with(updated_after: nil, location: nil, category: nil, tags: ['important'], page_cursor: nil) + .and_return(page1_response) + + subject.get_documents(tags: ['important']) + end + + it 'handles empty tags array' do + expect(subject).to receive(:get_documents_page) + .with(updated_after: nil, location: nil, category: nil, tags: [], page_cursor: nil) + .and_return(page1_response) + + subject.get_documents(tags: []) + end + end + + context 'URL building with tags' do + let(:page1_response) { fixture('document_list.json').from_json(false) } + + it 'builds URL with single tag parameter' do + expect(subject).to receive(:get_readwise_request) do |url| + expect(url).to include('tag=tech') + page1_response + end + + subject.send(:get_documents_page, updated_after: nil, location: nil, category: nil, tags: ['tech']) + end + + it 'builds URL with multiple tag parameters' do + expect(subject).to receive(:get_readwise_request) do |url| + expect(url).to include('tag=tech') + expect(url).to include('tag=ruby') + expect(url).to include('tag=programming') + page1_response + end + + subject.send(:get_documents_page, updated_after: nil, location: nil, category: nil, tags: ['tech', 'ruby', 'programming']) + end + + it 'builds URL without tag parameters when tags is nil' do + expect(subject).to receive(:get_readwise_request) do |url| + expect(url).not_to include('tag=') + page1_response + end + + subject.send(:get_documents_page, updated_after: nil, location: nil, category: nil, tags: nil) + end + + it 'builds URL without tag parameters when tags is empty' do + expect(subject).to receive(:get_readwise_request) do |url| + expect(url).not_to include('tag=') + page1_response + end + + subject.send(:get_documents_page, updated_after: nil, location: nil, category: nil, tags: []) + end end context 'transform methods' do