diff --git a/lib/instapaper/api/bookmark.rb b/lib/instapaper/api/bookmark.rb index 3a27f2c..26d8ffd 100644 --- a/lib/instapaper/api/bookmark.rb +++ b/lib/instapaper/api/bookmark.rb @@ -1,3 +1,5 @@ +require 'instapaper/bookmark' + module Instapaper module API # Defines methods related to bookmarks @@ -7,7 +9,7 @@ module Instapaper # @option folder_id: Optional. Possible values are unread (default), starred, archive, or a folder_id value from /api/1/folders/list. # @option have: Optional. A concatenation of bookmark_id values that the client already has from the specified folder. See below. def bookmarks(options = {}) - post('/api/1/bookmarks/list', options)[2..-1] + perform_post_with_objects('/api/1/bookmarks/list', options, Instapaper::Object)[2..-1] end # Updates the user's reading progress on a single article. @@ -15,57 +17,52 @@ module Instapaper # @param progress [Float] The user's progress, as a floating-point number between 0.0 and 1.0, defined as the top edge of the user's current viewport, expressed as a percentage of the article's total length. # @param progress_timestamp [Integer] The Unix timestamp value of the time that the progress was recorded. def update_read_progress(bookmark_id, progress, progress_timestamp = Time.now) - post('/api/1/bookmarks/update_read_progress', bookmark_id: bookmark_id, progress: progress, progress_timestamp: progress_timestamp.to_i).first + perform_post_with_object('/api/1/bookmarks/update_read_progress', {bookmark_id: bookmark_id, progress: progress, progress_timestamp: progress_timestamp.to_i}, Instapaper::Bookmark) end # Adds a new unread bookmark to the user's account. # @param url [String] The url of the bookmark. def add_bookmark(url, options = {}) - post('/api/1/bookmarks/add', options.merge(url: url)).first + perform_post_with_object('/api/1/bookmarks/add', options.merge(url: url), Instapaper::Bookmark) end # Permanently deletes the specified bookmark. # This is NOT the same as Archive. Please be clear to users if you're going to do this. # @param bookmark_id [String] The id of the bookmark. def delete_bookmark(bookmark_id) - post('/api/1/bookmarks/delete', bookmark_id: bookmark_id) + perform_post_with_objects('/api/1/bookmarks/delete', {bookmark_id: bookmark_id}, Array) end # Stars the specified bookmark. # @param bookmark_id [String] The id of the bookmark. - def star(bookmark_id) - post('/api/1/bookmarks/star', bookmark_id: bookmark_id).first + def star_bookmark(bookmark_id) + perform_post_with_object('/api/1/bookmarks/star', {bookmark_id: bookmark_id}, Instapaper::Bookmark) end - alias_method :star_bookmark, :star # Un-stars the specified bookmark. # @param bookmark_id [String] The id of the bookmark. - def unstar(bookmark_id) - post('/api/1/bookmarks/unstar', bookmark_id: bookmark_id).first + def unstar_bookmark(bookmark_id) + perform_post_with_object('/api/1/bookmarks/unstar', {bookmark_id: bookmark_id}, Instapaper::Bookmark) end - alias_method :unstar_bookmark, :unstar # Moves the specified bookmark to the Archive. # @param bookmark_id [String] The id of the bookmark. - def archive(bookmark_id) - post('/api/1/bookmarks/archive', bookmark_id: bookmark_id).first + def archive_bookmark(bookmark_id) + perform_post_with_object('/api/1/bookmarks/archive', {bookmark_id: bookmark_id}, Instapaper::Bookmark) end - alias_method :archive_bookmark, :archive # Moves the specified bookmark to the top of the Unread folder. # @param bookmark_id [String] The id of the bookmark. - def unarchive(bookmark_id) - post('/api/1/bookmarks/unarchive', bookmark_id: bookmark_id).first + def unarchive_bookmark(bookmark_id) + perform_post_with_object('/api/1/bookmarks/unarchive', {bookmark_id: bookmark_id}, Instapaper::Bookmark) end - alias_method :unarchive_bookmark, :unarchive # Moves the specified bookmark to a user-created folder. # @param bookmark_id [String] The id of the bookmark. # @param folder_id [String] The id of the folder to move the bookmark to. - def move(bookmark_id, folder_id) - post('/api/1/bookmarks/move', bookmark_id: bookmark_id, folder_id: folder_id).first + def move_bookmark(bookmark_id, folder_id) + perform_post_with_object('/api/1/bookmarks/move', {bookmark_id: bookmark_id, folder_id: folder_id}, Instapaper::Bookmark) end - alias_method :move_bookmark, :move # Returns the specified bookmark's processed text-view HTML, which is # always text/html encoded as UTF-8. diff --git a/lib/instapaper/api/utils.rb b/lib/instapaper/api/utils.rb index dd98933..f7482c0 100644 --- a/lib/instapaper/api/utils.rb +++ b/lib/instapaper/api/utils.rb @@ -1,3 +1,5 @@ +require 'instapaper/object' + module Instapaper module API module Utils diff --git a/lib/instapaper/bookmark.rb b/lib/instapaper/bookmark.rb new file mode 100644 index 0000000..2a56b7e --- /dev/null +++ b/lib/instapaper/bookmark.rb @@ -0,0 +1,6 @@ +require 'values' + +module Instapaper + class Bookmark < Value.new(:type, :bookmark_id, :url, :title, :description, :time, :starred, :private_source, :hash, :progress, :progress_timestamp) + end +end diff --git a/lib/instapaper/meta.rb b/lib/instapaper/meta.rb new file mode 100644 index 0000000..898219c --- /dev/null +++ b/lib/instapaper/meta.rb @@ -0,0 +1,6 @@ +require 'values' + +module Instapaper + class Meta < Value.new(:type) + end +end diff --git a/lib/instapaper/object.rb b/lib/instapaper/object.rb new file mode 100644 index 0000000..0a4fc82 --- /dev/null +++ b/lib/instapaper/object.rb @@ -0,0 +1,11 @@ +require 'instapaper/meta' + +module Instapaper + class Object + def self.with(data) + type = data[:type] + type[0] = type[0].upcase + Instapaper.const_get(type).with(data) + end + end +end diff --git a/lib/instapaper/user.rb b/lib/instapaper/user.rb index b07ead1..51c45fb 100644 --- a/lib/instapaper/user.rb +++ b/lib/instapaper/user.rb @@ -1,6 +1,6 @@ require 'values' module Instapaper - class User < Value.new(:type, :user_id, :username) + class User < Value.new(:type, :user_id, :username, :subscription_is_active) end end diff --git a/spec/instapaper/api/bookmark_spec.rb b/spec/instapaper/api/bookmark_spec.rb index d1083b4..8c9005a 100644 --- a/spec/instapaper/api/bookmark_spec.rb +++ b/spec/instapaper/api/bookmark_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe Instapaper::Client::Bookmark do let(:client) { Instapaper::Client.new(consumer_key: 'CK', consumer_secret: 'CS', oauth_token: 'OT', oauth_token_secret: 'OS') } - describe '.bookmarks' do + describe '#bookmarks' do before do stub_post('/api/1/bookmarks/list') .to_return(body: fixture('bookmarks_list.json'), headers: {content_type: 'application/json; charset=utf-8'}) @@ -24,13 +24,12 @@ describe Instapaper::Client::Bookmark do it 'should remove the meta and current user objects from the array' do bookmarks = client.bookmarks bookmarks.each do |bookmark| - expect(bookmark).to be_a Hashie::Rash - expect(bookmark.type).to eq('bookmark') + expect(bookmark).to be_an Instapaper::Bookmark end end end - describe '.update_read_progress' do + describe '#update_read_progress' do before do @time = Time.now stub_post('/api/1/bookmarks/update_read_progress') @@ -45,13 +44,12 @@ describe Instapaper::Client::Bookmark do it 'should return an array containing bookmarks on success' do bookmark = client.update_read_progress(123, 0.5, @time) - expect(bookmark).to be_a Hashie::Rash - expect(bookmark.type).to eq('bookmark') + expect(bookmark).to be_an Instapaper::Bookmark expect(bookmark.progress).to eq('0.5') end end - describe '.add_bookmark' do + describe '#add_bookmark' do before do stub_post('/api/1/bookmarks/add') .to_return(body: fixture('bookmarks_add.json'), headers: {content_type: 'application/json; charset=utf-8'}) @@ -65,12 +63,11 @@ describe Instapaper::Client::Bookmark do it 'should return the bookmark on success' do bookmark = client.add_bookmark('http://someurl.com', title: 'This is the title', description: 'This is the description') - expect(bookmark).to be_a Hashie::Rash - expect(bookmark.type).to eq('bookmark') + expect(bookmark).to be_an Instapaper::Bookmark end end - describe '.delete_bookmark' do + describe '#delete_bookmark' do before do stub_post('/api/1/bookmarks/delete') .to_return(body: '[]', headers: {content_type: 'application/json; charset=utf-8'}) @@ -89,120 +86,95 @@ describe Instapaper::Client::Bookmark do end end - describe '.star' do + describe '#star_bookmark' do before do stub_post('/api/1/bookmarks/star') .to_return(body: fixture('bookmarks_star.json'), headers: {content_type: 'application/json; charset=utf-8'}) end it 'should get the correct resource' do - client.star(123) + client.star_bookmark(123) expect(a_post('/api/1/bookmarks/star').with(body: {bookmark_id: '123'})) .to have_been_made end it 'should return a starred bookmark on success' do - bookmark = client.star(123) - expect(bookmark).to be_a Hashie::Rash - expect(bookmark.type).to eq('bookmark') + bookmark = client.star_bookmark(123) + expect(bookmark).to be_an Instapaper::Bookmark expect(bookmark.starred).to eq('1') end - - it 'should be aliased as .star_bookmark' do - expect(client.star(123)).to eq(client.star_bookmark(123)) - end end - describe '.unstar' do + describe '#unstar_bookmark' do before do stub_post('/api/1/bookmarks/unstar') .to_return(body: fixture('bookmarks_unstar.json'), headers: {content_type: 'application/json; charset=utf-8'}) end it 'should get the correct resource' do - client.unstar(123) + client.unstar_bookmark(123) expect(a_post('/api/1/bookmarks/unstar').with(body: {bookmark_id: '123'})) .to have_been_made end it 'should return an unstarred bookmark on success' do - bookmark = client.unstar(123) - expect(bookmark).to be_a Hashie::Rash - expect(bookmark.type).to eq('bookmark') + bookmark = client.unstar_bookmark(123) + expect(bookmark).to be_an Instapaper::Bookmark expect(bookmark.starred).to eq('0') end - - it 'should be aliased as .unstar_bookmark' do - expect(client.unstar(123)).to eq(client.unstar_bookmark(123)) - end end - describe '.archive' do + describe '#archive_bookmark' do before do stub_post('/api/1/bookmarks/archive') .to_return(body: fixture('bookmarks_archive.json'), headers: {content_type: 'application/json; charset=utf-8'}) end it 'should get the correct resource' do - client.archive(123) + client.archive_bookmark(123) expect(a_post('/api/1/bookmarks/archive').with(body: {bookmark_id: '123'})) .to have_been_made end it 'should return the bookmark on success' do - bookmark = client.archive(123) - expect(bookmark).to be_a Hashie::Rash - expect(bookmark.type).to eq('bookmark') - end - - it 'should be aliased as .archive_bookmark' do - expect(client.archive(123)).to eq(client.archive_bookmark(123)) + bookmark = client.archive_bookmark(123) + expect(bookmark).to be_an Instapaper::Bookmark end end - describe '.unarchive' do + describe '#unarchive_bookmark' do before do stub_post('/api/1/bookmarks/unarchive') .to_return(body: fixture('bookmarks_unarchive.json'), headers: {content_type: 'application/json; charset=utf-8'}) end it 'should get the correct resource' do - client.unarchive(123) + client.unarchive_bookmark(123) expect(a_post('/api/1/bookmarks/unarchive').with(body: {bookmark_id: '123'})) .to have_been_made end it 'should return the bookmark on success' do - bookmark = client.unarchive(123) - expect(bookmark).to be_a Hashie::Rash - expect(bookmark.type).to eq('bookmark') - end - - it 'should be aliased as .unarchive_bookmark' do - expect(client.unarchive(123)).to eq(client.unarchive_bookmark(123)) + bookmark = client.unarchive_bookmark(123) + expect(bookmark).to be_an Instapaper::Bookmark end end - describe '.move' do + describe '#move_bookmark' do before do stub_post('/api/1/bookmarks/move') .to_return(body: fixture('bookmarks_move.json'), headers: {content_type: 'application/json; charset=utf-8'}) end it 'should get the correct resource' do - client.move(123, 12_345) + client.move_bookmark(123, 12_345) expect(a_post('/api/1/bookmarks/move').with(body: {bookmark_id: '123', folder_id: '12345'})) .to have_been_made end it 'should return the bookmark on success' do - bookmark = client.move(123, 12_345) - expect(bookmark).to be_a Hashie::Rash - expect(bookmark.type).to eq('bookmark') - end - - it 'should be aliased as .move_bookmark' do - expect(client.move(123, 12_345)).to eq(client.move_bookmark(123, 12_345)) + bookmark = client.move_bookmark(123, 12_345) + expect(bookmark).to be_an Instapaper::Bookmark end end