mirror of
https://github.com/samsonjs/instapaper.git
synced 2026-03-25 08:55:49 +00:00
return Instapaper::Credentials instead of a hash from oauth
This commit is contained in:
parent
76b09b30ae
commit
26f526fe07
7 changed files with 46 additions and 21 deletions
|
|
@ -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
|
||||
|
|
|
|||
12
lib/instapaper/credentials.rb
Normal file
12
lib/instapaper/credentials.rb
Normal file
|
|
@ -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
|
||||
|
|
@ -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',
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
18
lib/instapaper/utils.rb
Normal file
18
lib/instapaper/utils.rb
Normal file
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue