From 26f526fe072df7be58026ef60292600f5646fe02 Mon Sep 17 00:00:00 2001 From: stve Date: Wed, 25 Feb 2015 22:34:57 -0500 Subject: [PATCH] return Instapaper::Credentials instead of a hash from oauth --- lib/instapaper/api/oauth.rb | 5 ++++- lib/instapaper/credentials.rb | 12 ++++++++++++ lib/instapaper/error.rb | 1 + lib/instapaper/http/qline_parser.rb | 6 +++++- lib/instapaper/http/request.rb | 15 ++------------- lib/instapaper/utils.rb | 18 ++++++++++++++++++ spec/instapaper/api/oauth_spec.rb | 10 ++++------ 7 files changed, 46 insertions(+), 21 deletions(-) create mode 100644 lib/instapaper/credentials.rb create mode 100644 lib/instapaper/utils.rb diff --git a/lib/instapaper/api/oauth.rb b/lib/instapaper/api/oauth.rb index 08ff667..0eeacc5 100644 --- a/lib/instapaper/api/oauth.rb +++ b/lib/instapaper/api/oauth.rb @@ -1,3 +1,4 @@ +require 'instapaper/credentials' require 'instapaper/http/qline_parser' module Instapaper @@ -7,7 +8,9 @@ module Instapaper # Gets an OAuth access token for a user. def access_token(username, password) response = perform_post_with_unparsed_response('/api/1/oauth/access_token', x_auth_username: username, x_auth_password: password, x_auth_mode: 'client_auth') - QLineParser.parse(response) + parsed_response = QLineParser.parse(response) + fail Instapaper::Error::OAuthError, parsed_response[:error] if parsed_response.key?(:error) + Instapaper::Credentials.new(parsed_response) end end end diff --git a/lib/instapaper/credentials.rb b/lib/instapaper/credentials.rb new file mode 100644 index 0000000..85db341 --- /dev/null +++ b/lib/instapaper/credentials.rb @@ -0,0 +1,12 @@ +require 'virtus' + +module Instapaper + class Credentials + include Virtus.value_object + + values do + attribute :oauth_token, String + attribute :oauth_token_secret, String + end + end +end diff --git a/lib/instapaper/error.rb b/lib/instapaper/error.rb index bddcce2..ddcfd3a 100644 --- a/lib/instapaper/error.rb +++ b/lib/instapaper/error.rb @@ -7,6 +7,7 @@ module Instapaper BookmarkError = Class.new(self) FolderError = Class.new(self) HighlightError = Class.new(self) + OAuthError = Class.new(self) ERRORS = { 1040 => 'Rate-limit exceeded', diff --git a/lib/instapaper/http/qline_parser.rb b/lib/instapaper/http/qline_parser.rb index 6c7181c..557967f 100644 --- a/lib/instapaper/http/qline_parser.rb +++ b/lib/instapaper/http/qline_parser.rb @@ -1,9 +1,13 @@ +require 'instapaper/utils' + module Instapaper class QLineParser + extend Instapaper::Utils + def self.parse(response) values = response.split('&').map { |part| part.split('=') }.flatten values.unshift('error') if values.length == 1 - Hash[*values] + symbolize_keys!(Hash[*values]) end end end diff --git a/lib/instapaper/http/request.rb b/lib/instapaper/http/request.rb index 437b19b..01022cd 100644 --- a/lib/instapaper/http/request.rb +++ b/lib/instapaper/http/request.rb @@ -5,10 +5,12 @@ require 'net/https' require 'openssl' require 'instapaper/error' require 'instapaper/http/headers' +require 'instapaper/utils' module Instapaper module HTTP class Request + include Instapaper::Utils BASE_URL = 'https://www.instapaper.com' attr_accessor :client, :headers, :multipart, :options, :path, :rate_limit, :request_method, :uri @@ -63,19 +65,6 @@ module Instapaper response.to_s end end - - def symbolize_keys!(object) - if object.is_a?(Array) - object.each_with_index do |val, index| - object[index] = symbolize_keys!(val) - end - elsif object.is_a?(Hash) - object.keys.each do |key| - object[key.to_sym] = symbolize_keys!(object.delete(key)) - end - end - object - end end end end diff --git a/lib/instapaper/utils.rb b/lib/instapaper/utils.rb new file mode 100644 index 0000000..617f6d9 --- /dev/null +++ b/lib/instapaper/utils.rb @@ -0,0 +1,18 @@ +module Instapaper + module Utils + private + + def symbolize_keys!(object) + if object.is_a?(Array) + object.each_with_index do |val, index| + object[index] = symbolize_keys!(val) + end + elsif object.is_a?(Hash) + object.keys.each do |key| + object[key.to_sym] = symbolize_keys!(object.delete(key)) + end + end + object + end + end +end diff --git a/spec/instapaper/api/oauth_spec.rb b/spec/instapaper/api/oauth_spec.rb index fb53e99..b5c2f43 100644 --- a/spec/instapaper/api/oauth_spec.rb +++ b/spec/instapaper/api/oauth_spec.rb @@ -19,15 +19,13 @@ describe Instapaper::Client::OAuth do it 'returns the a hash containing an oauth token and secret' do tokens = client.access_token('ohai', 'p455w0rd') - expect(tokens).to be_a Hash - expect(tokens.key?('oauth_token')).to be true - expect(tokens.key?('oauth_token_secret')).to be true + expect(tokens).to be_an Instapaper::Credentials end it 'returns a hash containing the error on invalid credentials' do - tokens = client.access_token('inval1d', 'cr3dentials') - expect(tokens).to be_a Hash - expect(tokens.key?('error')).to be true + expect { + client.access_token('inval1d', 'cr3dentials') + }.to raise_error(Instapaper::Error::OAuthError) end end end