update folder implementation

This commit is contained in:
stve 2015-02-10 00:03:47 -05:00
parent 39bc1dc1e7
commit 9f2f9648ac
4 changed files with 23 additions and 19 deletions

View file

@ -1,3 +1,5 @@
require 'instapaper/folder'
module Instapaper
module API
# Defines methods related to folders
@ -5,19 +7,20 @@ module Instapaper
# List the account's user-created folders.
# @note This only includes organizational folders and does not include RSS-feed folders or starred-subscription folders
def folders
post('/api/1/folders/list')
perform_post_with_objects('/api/1/folders/list', {}, Instapaper::Folder)
end
# Creates an organizational folder.
# @param title [String] The title of the folder to create
def add_folder(title)
post('/api/1/folders/add', title: title)
perform_post_with_object('/api/1/folders/add', {title: title}, Instapaper::Folder)
end
# Deletes the folder and moves any articles in it to the Archive.
# @param folder_id [String] The id of the folder.
def delete_folder(folder_id)
post('/api/1/folders/delete', folder_id: folder_id)
perform_post_with_empty_response('/api/1/folders/delete', folder_id: folder_id)
true
end
# Re-orders a user's folders.
@ -25,7 +28,7 @@ module Instapaper
# @example Ordering folder_ids 100, 200, and 300
# Instapaper.set_order(['100:1','200:2','300:3'])
def set_order(order = []) # rubocop:disable Style/AccessorMethodName
post('/api/1/folders/set_order', order: order.join(','))
perform_post_with_objects('/api/1/folders/set_order', {order: order.join(',')}, Instapaper::Folder)
end
end
end

View file

@ -33,6 +33,7 @@ module Instapaper
# @param klass [Class]
def perform_request_with_object(request_method, path, options, klass)
response = perform_request(request_method, path, options)
response = response.is_a?(Array) ? response.first : response
klass.with(response)
end

6
lib/instapaper/folder.rb Normal file
View file

@ -0,0 +1,6 @@
require 'values'
module Instapaper
class Folder < Value.new(:type, :folder_id, :title, :sync_to_mobile, :position)
end
end

View file

@ -3,7 +3,7 @@ require 'spec_helper'
describe Instapaper::Client::Folder do
let(:client) { Instapaper::Client.new(consumer_key: 'CK', consumer_secret: 'CS', oauth_token: 'OT', oauth_token_secret: 'OS') }
describe '.folders' do
describe '#folders' do
before do
stub_post('/api/1/folders/list')
.to_return(body: fixture('folders_list.json'), headers: {content_type: 'application/json; charset=utf-8'})
@ -19,12 +19,11 @@ describe Instapaper::Client::Folder do
folders = client.folders
expect(folders).to be_an Array
expect(folders.size).to eq(2)
expect(folders.first).to be_a Hashie::Rash
expect(folders.first['title']).to eq('Ruby')
expect(folders.first).to be_a Instapaper::Folder
end
end
describe '.add_folder' do
describe '#add_folder' do
before do
stub_post('/api/1/folders/add').with(body: {title: 'Ruby'})
.to_return(body: fixture('folders_add.json'), headers: {content_type: 'application/json; charset=utf-8'})
@ -37,15 +36,12 @@ describe Instapaper::Client::Folder do
end
it 'should return an array containing the new folder on success' do
folders = client.add_folder('Ruby')
expect(folders).to be_an Array
expect(folders).not_to be_empty
expect(folders.first).to be_a Hashie::Rash
expect(folders.first['title']).to eq('Ruby')
folder = client.add_folder('Ruby')
expect(folder).to be_a Instapaper::Folder
end
end
describe '.delete_folder' do
describe '#delete_folder' do
before do
stub_post('/api/1/folders/delete'). with(body: {folder_id: '1'})
.to_return(body: fixture('folders_delete.json'), headers: {content_type: 'application/json; charset=utf-8'})
@ -59,12 +55,11 @@ describe Instapaper::Client::Folder do
it 'should return an empty array on success' do
confirm = client.delete_folder('1')
expect(confirm).to be_an Array
expect(confirm).to be_empty
expect(confirm).to be true
end
end
describe '.set_order' do
describe '#set_order' do
before do
stub_post('/api/1/folders/set_order'). with(body: {order: '1121173:2,1121174:1'})
.to_return(body: fixture('folders_set_order.json'), headers: {content_type: 'application/json; charset=utf-8'})
@ -79,8 +74,7 @@ describe Instapaper::Client::Folder do
it 'should return an array reflecting the new order on success' do
folders = client.set_order(['1121173:2', '1121174:1'])
expect(folders).to be_an Array
expect(folders.first).to be_a Hashie::Rash
expect(folders.first['position']).to eq(1)
expect(folders.first).to be_a Instapaper::Folder
end
end
end