From 49ae0497c664e81522abbe7f4e077ac4d1fb1ea3 Mon Sep 17 00:00:00 2001 From: stve Date: Sat, 14 Feb 2015 14:46:46 -0500 Subject: [PATCH] update how defaults are handled and set --- lib/instapaper/api/oauth.rb | 2 +- lib/instapaper/client.rb | 83 +++++++------------------------ lib/instapaper/http/headers.rb | 5 +- spec/instapaper/api/oauth_spec.rb | 8 +-- spec/spec_helper.rb | 16 +++--- 5 files changed, 34 insertions(+), 80 deletions(-) diff --git a/lib/instapaper/api/oauth.rb b/lib/instapaper/api/oauth.rb index 1d9d6ce..d79a667 100644 --- a/lib/instapaper/api/oauth.rb +++ b/lib/instapaper/api/oauth.rb @@ -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 diff --git a/lib/instapaper/client.rb b/lib/instapaper/client.rb index 9c1a59e..1d655cb 100644 --- a/lib/instapaper/client.rb +++ b/lib/instapaper/client.rb @@ -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 diff --git a/lib/instapaper/http/headers.rb b/lib/instapaper/http/headers.rb index 1260568..61afc64 100644 --- a/lib/instapaper/http/headers.rb +++ b/lib/instapaper/http/headers.rb @@ -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 diff --git a/spec/instapaper/api/oauth_spec.rb b/spec/instapaper/api/oauth_spec.rb index 7bef975..3dc18e7 100644 --- a/spec/instapaper/api/oauth_spec.rb +++ b/spec/instapaper/api/oauth_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1774f0b..106e298 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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