Skip to content

Commit

Permalink
Refactor strategies to remove .build(server)
Browse files Browse the repository at this point in the history
These objects don't ever seem to be initialized outside of the .build
method, so we can simplify things and make them conform to a more 
consistent interface by allowing their initializers to take a server
instead of individual parameters.
  • Loading branch information
reidab committed Oct 27, 2015
1 parent 2465e2e commit e676684
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 39 deletions.
8 changes: 2 additions & 6 deletions lib/doorkeeper/request/authorization_code.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
module Doorkeeper
module Request
class AuthorizationCode
def self.build(server)
new(server.grant, server.client, server)
end

attr_accessor :grant, :client, :server

def initialize(grant, client, server)
@grant, @client, @server = grant, client, server
def initialize(server)
@grant, @client, @server = server.grant, server.client, server
end

def request
Expand Down
8 changes: 2 additions & 6 deletions lib/doorkeeper/request/client_credentials.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
module Doorkeeper
module Request
class ClientCredentials
def self.build(server)
new(server.client, server)
end

attr_accessor :client, :server

def initialize(client, server)
@client, @server = client, server
def initialize(server)
@client, @server = server.client, server
end

def request
Expand Down
9 changes: 3 additions & 6 deletions lib/doorkeeper/request/code.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
module Doorkeeper
module Request
class Code
def self.build(server)
new(server.context.send(:pre_auth), server)
end

attr_accessor :pre_auth, :server

def initialize(pre_auth, server)
@pre_auth, @server = pre_auth, server
def initialize(server)
@pre_auth = server.context.send(:pre_auth)
@server = server
end

def request
Expand Down
10 changes: 4 additions & 6 deletions lib/doorkeeper/request/password.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
module Doorkeeper
module Request
class Password
def self.build(server)
new(server.credentials, server.resource_owner, server)
end

attr_accessor :credentials, :resource_owner, :server

def initialize(credentials, resource_owner, server)
@credentials, @resource_owner, @server = credentials, resource_owner, server
def initialize(server)
@credentials = server.credentials
@resource_owner = server.resource_owner
@server = server
end

def request
Expand Down
10 changes: 4 additions & 6 deletions lib/doorkeeper/request/refresh_token.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
module Doorkeeper
module Request
class RefreshToken
def self.build(server)
new(server.current_refresh_token, server.credentials, server)
end

attr_accessor :refresh_token, :credentials, :server

def initialize(refresh_token, credentials, server)
@refresh_token, @credentials, @server = refresh_token, credentials, server
def initialize(server)
@refresh_token = server.current_refresh_token
@credentials = server.credentials
@server = server
end

def request
Expand Down
9 changes: 3 additions & 6 deletions lib/doorkeeper/request/token.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
module Doorkeeper
module Request
class Token
def self.build(server)
new(server.context.send(:pre_auth), server)
end

attr_accessor :pre_auth, :server

def initialize(pre_auth, server)
@pre_auth, @server = pre_auth, server
def initialize(server)
@pre_auth = server.context.send(:pre_auth)
@server = server
end

def request
Expand Down
4 changes: 2 additions & 2 deletions lib/doorkeeper/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ def initialize(context = nil)

def authorization_request(strategy)
klass = Request.authorization_strategy strategy
klass.build self
klass.new self
end

def token_request(strategy)
klass = Request.token_strategy strategy
klass.build self
klass.new self
end

# TODO: context should be the request
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

it 'builds the request with selected strategy' do
stub_const 'Doorkeeper::Request::Code', fake_class
expect(fake_class).to receive(:build).with(subject)
expect(fake_class).to receive(:new).with(subject)
subject.authorization_request :code
end
end
Expand Down

0 comments on commit e676684

Please sign in to comment.