update how defaults are handled and set

This commit is contained in:
stve 2015-02-14 14:46:46 -05:00
parent d1d493226c
commit 49ae0497c6
5 changed files with 34 additions and 80 deletions

View file

@ -3,7 +3,7 @@ module Instapaper
# Defines methods related to OAuth
module OAuth
# Gets an OAuth access token for a user.
def access_token(username, password)
def 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')
params = response.split('&')
values = params.map { |part| part.split('=') }.flatten

View file

@ -8,53 +8,23 @@ module Instapaper
include Instapaper::API
include Instapaper::HTTP::Utils
# An array of valid keys in the options hash when configuring a {Instapaper::API}
VALID_OPTIONS_KEYS = [
:consumer_key,
:consumer_secret,
:endpoint,
:oauth_token,
:oauth_token_secret,
:proxy,
:user_agent,
].freeze
attr_accessor :access_token, :access_token_secret, :consumer_key, :consumer_secret, :proxy
attr_writer :user_agent
# By default, don't set an application key
DEFAULT_CONSUMER_KEY = nil
# By default, don't set an application secret
DEFAULT_CONSUMER_SECRET = nil
# The endpoint that will be used to connect if none is set
DEFAULT_ENDPOINT = 'https://www.instapaper.com'.freeze
# By default, don't set a user oauth token
DEFAULT_OAUTH_TOKEN = nil
# By default, don't set a user oauth secret
DEFAULT_OAUTH_TOKEN_SECRET = nil
# By default, don't use a proxy server
DEFAULT_PROXY = nil
# The user agent that will be sent to the API endpoint if none is set
DEFAULT_USER_AGENT = "Instapaper Ruby Gem #{Instapaper::VERSION}".freeze
# @private
attr_accessor :consumer_key
attr_accessor :consumer_secret
attr_accessor :endpoint
attr_accessor :oauth_token
attr_accessor :oauth_token_secret
attr_accessor :proxy
attr_accessor :user_agent
# Creates a new Instapaper::Client
# Initializes a new Client object
#
# @param options [Hash]
# @return [Instapaper::Client]
def initialize(options = {})
reset
options.keys.each do |key|
send("#{key}=", options[key])
end
options.each do |key, value|
instance_variable_set("@#{key}", value)
end
yield(self) if block_given?
end
# @return [String]
def user_agent
@user_agent ||= "InstapaperRubyGem/#{Instapaper::VERSION}"
end
# Authentication hash
@ -64,8 +34,8 @@ module Instapaper
{
consumer_key: @consumer_key,
consumer_secret: @consumer_secret,
oauth_token: @oauth_token,
oauth_token_secret: @oauth_token_secret,
access_token: @access_token,
access_token_secret: @access_token_secret,
}
end
@ -76,24 +46,9 @@ module Instapaper
}
end
# Check whether user is authenticated
#
# @return [Boolean]
def authenticated?
authentication.values.all?
end
private
# Reset all configuration options to defaults
def reset # rubocop:disable MethodLength
@consumer_key = DEFAULT_CONSUMER_KEY
@consumer_secret = DEFAULT_CONSUMER_SECRET
@endpoint = DEFAULT_ENDPOINT
@oauth_token = DEFAULT_OAUTH_TOKEN
@oauth_token_secret = DEFAULT_OAUTH_TOKEN_SECRET
@proxy = DEFAULT_PROXY
@user_agent = DEFAULT_USER_AGENT
def credentials?
credentials.values.all?
end
end
end

View file

@ -32,11 +32,10 @@ module Instapaper
{
consumer_key: @client.consumer_key,
consumer_secret: @client.consumer_secret,
token: @client.oauth_token,
token_secret: @client.oauth_token_secret,
token: @client.access_token,
token_secret: @client.access_token_secret,
}
end
end
end
end

View file

@ -3,7 +3,7 @@ require 'spec_helper'
describe Instapaper::Client::OAuth do
let(:client) { Instapaper::Client.new }
describe '.access_token' do
describe '#token' do
before do
stub_post('/api/1/oauth/access_token').with(body: {x_auth_username: 'ohai', x_auth_password: 'p455w0rd', x_auth_mode: 'client_auth'})
.to_return(body: fixture('access_token.qline'), headers: {content_type: 'text/plain; charset=utf-8'})
@ -12,20 +12,20 @@ describe Instapaper::Client::OAuth do
end
it 'should get the correct resource' do
client.access_token('ohai', 'p455w0rd')
client.token('ohai', 'p455w0rd')
expect(a_post('/api/1/oauth/access_token'))
.to have_been_made
end
it 'should return the a hash containing an oauth token and secret' do
tokens = client.access_token('ohai', 'p455w0rd')
tokens = client.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
end
it 'should return a hash containing the error on invalid credentials' do
tokens = client.access_token('inval1d', 'cr3dentials')
tokens = client.token('inval1d', 'cr3dentials')
expect(tokens).to be_a Hash
expect(tokens.key?('error')).to be true
end

View file

@ -12,35 +12,35 @@ require 'rspec'
require 'webmock/rspec'
def a_delete(path)
a_request(:delete, Instapaper::Client::DEFAULT_ENDPOINT + path)
a_request(:delete, Instapaper::HTTP::Request::BASE_URL + path)
end
def a_get(path)
a_request(:get, Instapaper::Client::DEFAULT_ENDPOINT + path)
a_request(:get, Instapaper::HTTP::Request::BASE_URL + path)
end
def a_post(path)
a_request(:post, Instapaper::Client::DEFAULT_ENDPOINT + path)
a_request(:post, Instapaper::HTTP::Request::BASE_URL + path)
end
def a_put(path)
a_request(:put, Instapaper::Client::DEFAULT_ENDPOINT + path)
a_request(:put, Instapaper::HTTP::Request::BASE_URL + path)
end
def stub_delete(path)
stub_request(:delete, Instapaper::Client::DEFAULT_ENDPOINT + path)
stub_request(:delete, Instapaper::HTTP::Request::BASE_URL + path)
end
def stub_get(path)
stub_request(:get, Instapaper::Client::DEFAULT_ENDPOINT + path)
stub_request(:get, Instapaper::HTTP::Request::BASE_URL + path)
end
def stub_post(path)
stub_request(:post, Instapaper::Client::DEFAULT_ENDPOINT + path)
stub_request(:post, Instapaper::HTTP::Request::BASE_URL + path)
end
def stub_put(path)
stub_request(:put, Instapaper::Client::DEFAULT_ENDPOINT + path)
stub_request(:put, Instapaper::HTTP::Request::BASE_URL + path)
end
def fixture_path