Permalink
Browse files

Merge pull request #4 from newspaperclub/master

Error handling, tests, tidying up some bits
  • Loading branch information...
2 parents 62889ea + 13c9779 commit f3f83bf821ee789fdbbb9c8d5ae56474e522fbc4 Matt Biddulph committed Dec 2, 2011
View
@@ -15,4 +15,8 @@ Note that you need to [request OAuth Application tokens manually](http://www.ins
ip = InstapaperFull::API.new :consumer_key => "my key", :consumer_secret => "my secret"
ip.authenticate "someone@example.com", "password"
puts ip.options.user_id
- puts ip.bookmarks_list(:limit => 1)[0]['url']
+ ip.bookmarks_list(:limit => 1) do |b|
+ if b['type'] == 'bookmark'
+ puts b['url']
+ end
+ end
View
@@ -1,2 +1,10 @@
require 'bundler'
Bundler::GemHelper.install_tasks
+require 'rake/testtask'
+
+Rake::TestTask.new(:test) do |test|
+ test.libs << 'lib' << 'test'
+ test.pattern = 'test/*_test.rb'
+end
+
+task :default => 'test'
@@ -6,8 +6,8 @@ Gem::Specification.new do |s|
s.name = "instapaper_full"
s.version = InstapaperFull::VERSION
s.platform = Gem::Platform::RUBY
- s.authors = ["Matt Biddulph"]
- s.email = ["mb@hackdiary.com"]
+ s.authors = ["Matt Biddulph", "Tom Taylor"]
+ s.email = ["mb@hackdiary.com", "tom@tomtaylor.co.uk"]
s.homepage = "https://github.com/mattb/instapaper_full"
s.summary = %q{Wrapper for the Instapaper Full Developer API}
s.description = %q{See http://www.instapaper.com/api/full}
@@ -26,4 +26,6 @@ Gem::Specification.new do |s|
s.add_dependency("yajl-ruby", "~> 1.1.0")
s.add_development_dependency("rake")
+ s.add_development_dependency("test-unit", "~> 2.4.2")
+ s.add_development_dependency("webmock", "~> 1.7.8")
end
View
@@ -0,0 +1,13 @@
+module InstapaperFull
+ class API
+ class Error < RuntimeError
+
+ attr_reader :code, :message
+
+ def initialize(code, message)
+ @code, @message = code, message
+ end
+
+ end
+ end
+end
@@ -1,52 +1,47 @@
+require 'errors'
require 'json'
require 'faraday/request/oauth'
require 'faraday/response/parse_json'
module InstapaperFull
class API
attr_accessor :options
- def initialize(options={})
+ def initialize(options = {})
@options = options
end
def connection(options = {})
- skip_json = options.delete(:skip_json)
-
options.merge!({
:proxy => @options[:proxy],
:ssl => {:verify => false},
:url => "https://www.instapaper.com/api/1/"
})
- oauth_options = {
+ oauth_params = {
:consumer_key => @options[:consumer_key],
:consumer_secret => @options[:consumer_secret]
}
if authenticated?
- oauth_options[:token] = @options[:oauth_token]
- oauth_options[:token_secret] = @options[:oauth_token_secret]
+ oauth_params[:token] = @options[:oauth_token]
+ oauth_params[:token_secret] = @options[:oauth_token_secret]
end
Faraday.new(options) do |builder|
- builder.use Faraday::Request::OAuth, oauth_options
+ builder.use Faraday::Request::OAuth, oauth_params
builder.use Faraday::Request::UrlEncoded
- builder.use Faraday::Response::Logger
builder.adapter Faraday.default_adapter
- if authenticated? && !skip_json
- builder.use Faraday::Response::ParseJson
- end
end
end
def authenticated?
@options.has_key? :oauth_token and @options.has_key? :oauth_token_secret
end
- def authenticate(username,password)
+ def authenticate(username, password)
@options.delete(:oauth_token)
@options.delete(:oauth_token_secret)
- result = connection.post 'oauth/access_token' do |r|
+ result = connection.post 'oauth/access_token' do |r|
r.body = { :x_auth_username => username, :x_auth_password => password, :x_auth_mode => "client_auth" }
end
if result.status == 200
@@ -64,73 +59,81 @@ def authenticate(username,password)
end
end
- def call(method, body = {})
- skip_json = body.delete(:skip_json)
- result = connection({:skip_json => skip_json}).post(method) do |r|
- r.body = body unless body.empty?
+ def call(method, params = {}, connection_options = {})
+ result = connection(connection_options).post(method) do |r|
+ r.body = params unless params.empty?
+ end
+
+ if result.headers['content-type'] == 'application/json'
+ JSON.parse(result.body).tap do |d|
+ if error = d.find { |e| e['type'] == 'error' }
+ raise InstapaperFull::API::Error.new(error['error_code'], error['message'])
+ end
+ end
+ else
+ raise InstapaperFull::API::Error.new(-1, result.body) if result.status != 200
+ result.body
end
- return result.body
end
def verify_credentials
call('account/verify_credentials')[0]
end
- def bookmarks_list(options = {})
- call('bookmarks/list', options)[2..-1] # slice off the 'meta' and 'user' from the front of the array
+ def bookmarks_list(params = {})
+ call('bookmarks/list', params)
end
- def bookmarks_update_read_progress(options = {})
- call('bookmarks/update_read_progress', options)
+ def bookmarks_update_read_progress(params = {})
+ call('bookmarks/update_read_progress', params)
end
- def bookmarks_add(options = {})
- call('bookmarks/add',options)
+ def bookmarks_add(params = {})
+ call('bookmarks/add', params)
end
- def bookmarks_delete(options = {})
- call('bookmarks/delete', options)
+ def bookmarks_delete(params = {})
+ call('bookmarks/delete', params)
end
- def bookmarks_star(options = {})
- call('bookmarks/star', options)
+ def bookmarks_star(params = {})
+ call('bookmarks/star', params)
end
- def bookmarks_unstar(options = {})
- call('bookmarks/unstar', options)
+ def bookmarks_unstar(params = {})
+ call('bookmarks/unstar', params)
end
- def bookmarks_archive(options = {})
- call('bookmarks/archive', options)
+ def bookmarks_archive(params = {})
+ call('bookmarks/archive', params)
end
- def bookmarks_unarchive(options = {})
- call('bookmarks/unarchive', options)
+ def bookmarks_unarchive(params = {})
+ call('bookmarks/unarchive', params)
end
- def bookmarks_move(options = {})
- call('bookmarks/move', options)
+ def bookmarks_move(params = {})
+ call('bookmarks/move', params)
end
- def bookmarks_get_text(options = {})
- options[:skip_json] = true
- call('bookmarks/get_text', options)
+ def bookmarks_get_text(params = {})
+ call('bookmarks/get_text', params)
end
def folders_list
call('folders/list')
end
- def folders_add(options = {})
- call('folders/add', options)
+ def folders_add(params = {})
+ call('folders/add', params)
end
- def folders_delete(options = {})
- call('folders/delete', options)
+ def folders_delete(params = {})
+ call('folders/delete', params)
end
- def folders_set_order(options = {})
- call('folders/set_order', options)
+ def folders_set_order(params = {})
+ call('folders/set_order', params)
end
end
end
@@ -0,0 +1,9 @@
+module AssetHelpers
+
+ def http_response(name)
+ name += ".txt"
+ path = File.join(File.dirname(__FILE__), 'http_responses', name)
+ File.read(path)
+ end
+
+end
@@ -0,0 +1,14 @@
+HTTP/1.1 401 Unauthorized
+Date: Thu, 01 Dec 2011 16:32:47 GMT
+Server: Apache
+P3P: CP="ALL ADM DEV PSAi COM OUR OTRo STP IND ONL"
+X-Robots-Tag: noindex
+Cache-Control: no-cache
+Pragma: no-cache
+X-Powered-By: a lot of coffee and Phish
+Vary: Accept-Encoding
+Content-Length: 26
+Connection: close
+Content-Type: text/html; charset=UTF-8
+
+Invalid xAuth credentials.
@@ -0,0 +1,14 @@
+HTTP/1.1 200 OK
+Date: Thu, 01 Dec 2011 16:31:04 GMT
+Server: Apache
+P3P: CP="ALL ADM DEV PSAi COM OUR OTRo STP IND ONL"
+X-Robots-Tag: noindex
+Cache-Control: no-cache
+Pragma: no-cache
+X-Powered-By: a lot of coffee and Phish
+Vary: Accept-Encoding
+Content-Length: 132
+Connection: close
+Content-Type: application/x-www-form-urlencoded
+
+oauth_token=thisisatoken&oauth_token_secret=thisisasecret
@@ -0,0 +1,14 @@
+HTTP/1.1 400 Bad Request
+Date: Thu, 01 Dec 2011 16:43:12 GMT
+Server: Apache
+P3P: CP="ALL ADM DEV PSAi COM OUR OTRo STP IND ONL"
+X-Robots-Tag: noindex
+Cache-Control: no-cache
+Pragma: no-cache
+X-Powered-By: a lot of coffee and Phish
+Vary: Accept-Encoding
+Content-Length: 70
+Connection: close
+Content-Type: application/json
+
+[{"type":"error","error_code":1240,"message":"Invalid URL specified"}]
@@ -0,0 +1,14 @@
+HTTP/1.1 400 Bad Request
+Date: Thu, 01 Dec 2011 16:36:08 GMT
+Server: Apache
+P3P: CP="ALL ADM DEV PSAi COM OUR OTRo STP IND ONL"
+X-Robots-Tag: noindex
+Cache-Control: no-cache
+Pragma: no-cache
+X-Powered-By: a lot of coffee and Phish
+Vary: Accept-Encoding
+Content-Length: 79
+Connection: close
+Content-Type: application/json
+
+[{"type":"error","error_code":1241,"message":"Invalid or missing bookmark_id"}]
Oops, something went wrong.

0 comments on commit f3f83bf

Please sign in to comment.