update bookmark implementation

This commit is contained in:
stve 2015-02-10 00:39:35 -05:00
parent ed59ddefdf
commit 000144ec60
7 changed files with 69 additions and 75 deletions

View file

@ -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.

View file

@ -1,3 +1,5 @@
require 'instapaper/object'
module Instapaper
module API
module Utils

View file

@ -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

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

@ -0,0 +1,6 @@
require 'values'
module Instapaper
class Meta < Value.new(:type)
end
end

11
lib/instapaper/object.rb Normal file
View file

@ -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

View file

@ -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

View file

@ -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