Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add configuration class
for configuration auth policy and credentials
  • Loading branch information
rossta committed Jan 16, 2013
1 parent 0a92b36 commit 268ad81
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 1 deletion.
24 changes: 24 additions & 0 deletions README.md
Expand Up @@ -16,6 +16,30 @@ Seriously, [check it out](http://www.trello.com/).
Full Disclosure: This library is mostly complete, if you do find anything missing or not functioning as you expect it
to, please [let us know](https://trello.com/card/spot-a-bug-report-it/4f092b2ee23cb6fe6d1aaabd/17).

## Configuration

Basic authorization

```ruby
Trello.configure do |config|
config.strategy = :basic
config.consumer_key = YOUR_CONSUMER_KEY
config.consumer_secret = YOUR_CONSUMER_SECRET
end
```

OAuth authorization

```ruby
Trello.configure do |config|
config.strategy = :oauth
config.consumer_key = YOUR_CONSUMER_KEY
config.consumer_secret = YOUR_CONSUMER_SECRET
config.oauth_token = YOUR_OAUTH_TOKEN
config.oauth_token_secret = YOUR_OAUTH_TOKEN_SECRET
end
```

## Special thanks

A special thanks goes out to [Ben Biddington](https://github.com/ben-biddington) who has contributed a significant amount
Expand Down
9 changes: 9 additions & 0 deletions lib/trello.rb
Expand Up @@ -47,6 +47,7 @@ module Trello
autoload :Card, 'trello/card'
autoload :Checklist, 'trello/checklist'
autoload :Client, 'trello/client'
autoload :Configuration, 'trello/configuration'
autoload :HasActions, 'trello/has_actions'
autoload :Item, 'trello/item'
autoload :CheckItemState, 'trello/item_state'
Expand Down Expand Up @@ -85,4 +86,12 @@ def self.logger=(logger)
def self.client
@client ||= Client.new
end

def self.configuration
client.configuration
end

def self.configure
yield configuration
end
end
4 changes: 4 additions & 0 deletions lib/trello/client.rb
Expand Up @@ -44,5 +44,9 @@ def invoke_verb(name, uri, body = nil)
response.body
end

def configuration
@configuration ||= Configuration.new
end

end
end
41 changes: 41 additions & 0 deletions lib/trello/configuration.rb
@@ -0,0 +1,41 @@
module Trello
class Configuration
attr_accessor :auth_policy
attr_accessor :developer_public_key, :member_token
attr_accessor :consumer_key, :consumer_secret, :oauth_token, :oauth_token_secret

def initialize(attrs = {})
attrs.each { |key, value| instance_variable_set("@#{key}", value) }
end

def credentials
case auth_policy
when :oauth
oauth_credentials
when :basic
basic_credentials
else
{}
end
end

private

def oauth_credentials
{
:consumer_key => consumer_key,
:consumer_secret => consumer_secret,
:oauth_token => oauth_token,
:oauth_token_secret => oauth_token_secret,
}
end

def basic_credentials
{
:developer_public_key => developer_public_key,
:member_token => member_token
}
end

end
end
93 changes: 93 additions & 0 deletions spec/configuration_spec.rb
@@ -0,0 +1,93 @@
require "spec_helper"

include Trello

describe Configuration do
let(:configuration) { Configuration.new }

it "has a consumer_key attribute" do
configuration.consumer_key = 'consumer_key'
configuration.consumer_key.should eq('consumer_key')
end

it "has a consumer_secret attribute" do
configuration.consumer_secret = 'consumer_secret'
configuration.consumer_secret.should eq('consumer_secret')
end

it "has a oauth_token attribute" do
configuration.oauth_token = 'oauth_token'
configuration.oauth_token.should eq('oauth_token')
end

it "has a oauth_token_secret attribute" do
configuration.oauth_token_secret = 'oauth_token_secret'
configuration.oauth_token_secret.should eq('oauth_token_secret')
end

it "has a developer public key attribute" do
configuration.developer_public_key = 'developer_public_key'
configuration.developer_public_key.should eq('developer_public_key')
end

it "has a member token attribute" do
configuration.member_token = 'member_token'
configuration.member_token.should eq('member_token')
end

it "has an auth_policy attribute" do
configuration.auth_policy = 'auth_policy'
configuration.auth_policy.should eq('auth_policy')
end

describe "initialize" do
it "sets key attributes provided as a hash" do
configuration = Configuration.new(
:consumer_key => 'consumer_key',
:consumer_secret => 'consumer_secret',
:oauth_token => 'oauth_token',
:oauth_token_secret => 'oauth_token_secret'
)
configuration.consumer_key.should eq('consumer_key')
configuration.consumer_secret.should eq('consumer_secret')
configuration.oauth_token.should eq('oauth_token')
configuration.oauth_token_secret.should eq('oauth_token_secret')
end
end

describe "#credentials" do
let(:configuration) {
Configuration.new(
:consumer_key => 'consumer_key',
:consumer_secret => 'consumer_secret',
:oauth_token => 'oauth_token',
:oauth_token_secret => 'oauth_token_secret',
:developer_public_key => 'developer_public_key',
:member_token => 'member_token'
)
}

it 'returns a hash of oauth attributes' do
configuration.auth_policy = :oauth
configuration.credentials.should eq(
:consumer_key => 'consumer_key',
:consumer_secret => 'consumer_secret',
:oauth_token => 'oauth_token',
:oauth_token_secret => 'oauth_token_secret'
)
end

it "returns a hash of basic auth policy attributes" do
configuration.auth_policy = :basic
configuration.credentials.should eq(
:developer_public_key => 'developer_public_key',
:member_token => 'member_token'
)
end

it "returns a hash of basic auth policy attributes" do
configuration.auth_policy = nil
configuration.credentials.should eq({})
end
end
end
2 changes: 1 addition & 1 deletion spec/integration/how_to_authorize_spec.rb
Expand Up @@ -45,7 +45,7 @@
it "[!] actually does not enforce signature at all, only the keys are required" do
OAuthPolicy.consumer_credential = OAuthCredential.new @developer_public_key, nil
OAuthPolicy.token = OAuthCredential.new @access_token_key, nil

pending "I would expect this to fail because I have signed with nil secrets" do
lambda{Client.get("/boards/#{@welcome_board}/")}.should raise_error
end
Expand Down

0 comments on commit 268ad81

Please sign in to comment.