lib/instapaper_full.rb in instapaper_full-0.2.0 vs lib/instapaper_full.rb in instapaper_full-0.3.0

- old
+ new

@@ -1,9 +1,9 @@ require 'errors' require 'json' -require 'faraday/request/oauth' -require 'faraday/response/parse_json' +require 'faraday' +require 'faraday_middleware' module InstapaperFull class API attr_accessor :options def initialize(options = {}) @@ -11,12 +11,12 @@ end def connection(options = {}) options.merge!({ :proxy => @options[:proxy], - :ssl => {:verify => false}, - :url => "https://www.instapaper.com/api/1/" + :ssl => { :verify => @options.fetch(:verify, true) }, + :url => "https://www.instapaper.com/api/1.1/" }) oauth_params = { :consumer_key => @options[:consumer_key], :consumer_secret => @options[:consumer_secret] @@ -26,10 +26,11 @@ oauth_params[:token] = @options[:oauth_token] oauth_params[:token_secret] = @options[:oauth_token_secret] end Faraday.new(options) do |builder| + builder.use Faraday::Request::Multipart builder.use Faraday::Request::OAuth, oauth_params builder.use Faraday::Request::UrlEncoded builder.adapter Faraday.default_adapter end end @@ -62,16 +63,19 @@ 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' + begin JSON.parse(result.body).tap do |d| - if error = d.find { |e| e['type'] == 'error' } + # Errors are always returned as an array with a single element. + # bookmarks/list is the only method which returns a Hash, not an + # Array. + if d.kind_of?(Array) && error = d.find { |e| e['type'] == 'error' } raise InstapaperFull::API::Error.new(error['error_code'], error['message']) end end - else + rescue JSON::ParserError raise InstapaperFull::API::Error.new(-1, result.body) if result.status != 200 result.body end end