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

View file

@ -8,53 +8,23 @@ module Instapaper
include Instapaper::API include Instapaper::API
include Instapaper::HTTP::Utils include Instapaper::HTTP::Utils
# An array of valid keys in the options hash when configuring a {Instapaper::API} attr_accessor :access_token, :access_token_secret, :consumer_key, :consumer_secret, :proxy
VALID_OPTIONS_KEYS = [ attr_writer :user_agent
:consumer_key,
:consumer_secret,
:endpoint,
:oauth_token,
:oauth_token_secret,
:proxy,
:user_agent,
].freeze
# By default, don't set an application key # Initializes a new Client object
DEFAULT_CONSUMER_KEY = nil #
# @param options [Hash]
# By default, don't set an application secret # @return [Instapaper::Client]
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
def initialize(options = {}) def initialize(options = {})
reset options.each do |key, value|
options.keys.each do |key| instance_variable_set("@#{key}", value)
send("#{key}=", options[key]) end
end yield(self) if block_given?
end
# @return [String]
def user_agent
@user_agent ||= "InstapaperRubyGem/#{Instapaper::VERSION}"
end end
# Authentication hash # Authentication hash
@ -64,8 +34,8 @@ module Instapaper
{ {
consumer_key: @consumer_key, consumer_key: @consumer_key,
consumer_secret: @consumer_secret, consumer_secret: @consumer_secret,
oauth_token: @oauth_token, access_token: @access_token,
oauth_token_secret: @oauth_token_secret, access_token_secret: @access_token_secret,
} }
end end
@ -76,24 +46,9 @@ module Instapaper
} }
end end
# Check whether user is authenticated
#
# @return [Boolean] # @return [Boolean]
def authenticated? def credentials?
authentication.values.all? credentials.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
end end
end end
end end

View file

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

View file

@ -3,7 +3,7 @@ require 'spec_helper'
describe Instapaper::Client::OAuth do describe Instapaper::Client::OAuth do
let(:client) { Instapaper::Client.new } let(:client) { Instapaper::Client.new }
describe '.access_token' do describe '#token' do
before 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'}) 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'}) .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 end
it 'should get the correct resource' do it 'should get the correct resource' do
client.access_token('ohai', 'p455w0rd') client.token('ohai', 'p455w0rd')
expect(a_post('/api/1/oauth/access_token')) expect(a_post('/api/1/oauth/access_token'))
.to have_been_made .to have_been_made
end end
it 'should return the a hash containing an oauth token and secret' do 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).to be_a Hash
expect(tokens.key?('oauth_token')).to be true expect(tokens.key?('oauth_token')).to be true
expect(tokens.key?('oauth_token_secret')).to be true expect(tokens.key?('oauth_token_secret')).to be true
end end
it 'should return a hash containing the error on invalid credentials' do 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).to be_a Hash
expect(tokens.key?('error')).to be true expect(tokens.key?('error')).to be true
end end

View file

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