Permalink
Browse files

Client now accepts configuration via initialize and configure block

  • Loading branch information...
1 parent 1f3d3e5 commit 0625235d32c186eeb17da17bc6887d052afae340 @rossta committed Jan 20, 2013
Showing with 86 additions and 14 deletions.
  1. +5 −6 lib/trello.rb
  2. +27 −2 lib/trello/client.rb
  3. +16 −3 lib/trello/configuration.rb
  4. +34 −0 spec/client_spec.rb
  5. +4 −0 spec/spec_helper.rb
  6. +0 −3 spec/trello_spec.rb
View
@@ -86,16 +86,15 @@ def self.client
@client ||= Client.new
end
- def self.configure
+ def self.configure(&block)
reset!
- yield client.configuration
- end
-
- def self.auth_policy
- client.auth_policy
+ client.configure(&block)
end
def self.reset!
@client = nil
end
+
+ def self.auth_policy; client.auth_policy; end
+ def self.configuration; client.configuration; end
end
View
@@ -4,6 +4,13 @@ module Trello
class Client
include Authorization
+ delegate *Configuration::CONFIGURABLE_ATTRIBUTES, to: :configuration
+ delegate :credentials, to: :configuration
+
+ def initialize(attrs = {})
+ self.configuration.attributes = attrs
+ end
+
def get(path, params = {})
uri = Addressable::URI.parse("https://api.trello.com/#{API_VERSION}#{path}")
uri.query_values = params unless params.empty?
@@ -26,6 +33,13 @@ def delete(path)
end
# Finds given resource by id
+ #
+ # Examples:
+ # client.find(:board, "board1234")
+ # client.find(:member, "user1234")
+ # client.find(Board, "board1234")
+ # client.find(Member, "member1234")
+ #
def find(path, id)
response = get("/#{path.to_s.pluralize}/#{id}")
class_from_path(path).parse(response) do |data|
@@ -34,26 +48,37 @@ def find(path, id)
end
# Finds given resource by path with params
- def find_many(klass, path, params)
+ def find_many(klass, path, params = {})
response = get(path, params)
klass.parse_many(response) do |data|
data.client = self
end
end
# Creates resource with given options (attributes)
+ #
+ # Examples:
+ # client.create(:member, options)
+ # client.create(:board, options)
+ # client.create(Member, options)
+ # client.create(Board, options)
+ #
def create(path, options)
class_from_path(path).save(options) do |data|
data.client = self
end
end
+ def configure
+ yield configuration if block_given?
+ end
+
def configuration
@configuration ||= Configuration.new
end
def auth_policy
- @auth_policy ||= auth_policy_class.new(configuration.credentials)
+ @auth_policy ||= auth_policy_class.new(credentials)
end
private
@@ -1,10 +1,23 @@
module Trello
class Configuration
- attr_accessor :developer_public_key, :member_token
- attr_accessor :consumer_key, :consumer_secret, :oauth_token, :oauth_token_secret
- attr_accessor :callback, :return_url
+ CONFIGURABLE_ATTRIBUTES = [
+ :developer_public_key,
+ :member_token,
+ :consumer_key,
+ :consumer_secret,
+ :oauth_token,
+ :oauth_token_secret,
+ :callback,
+ :return_url
+ ]
+
+ attr_accessor *CONFIGURABLE_ATTRIBUTES
def initialize(attrs = {})
+ self.attributes = attrs
+ end
+
+ def attributes=(attrs = {})
attrs.each { |key, value| instance_variable_set("@#{key}", value) }
end
View
@@ -131,4 +131,38 @@
client.put "/xxx", {}
end
+
+ context "initialize" do
+ let(:client) {
+ Client.new(
+ :consumer_key => 'consumer_key',
+ :consumer_secret => 'consumer_secret',
+ :oauth_token => 'oauth_token',
+ :oauth_token_secret => 'oauth_token_secret'
+ )
+ }
+
+ it "is configurable" do
+ client.consumer_key.should eq('consumer_key')
+ client.consumer_secret.should eq('consumer_secret')
+ client.oauth_token.should eq('oauth_token')
+ client.oauth_token_secret.should eq('oauth_token_secret')
+ end
+ end
+
+ describe "configure" do
+ it "sets key attributes through config block" do
+ client.configure do |config|
+ config.consumer_key = 'consumer_key'
+ config.consumer_secret = 'consumer_secret'
+ config.oauth_token = 'oauth_token'
+ config.oauth_token_secret = 'oauth_token_secret'
+ end
+
+ client.consumer_key.should eq('consumer_key')
+ client.consumer_secret.should eq('consumer_secret')
+ client.oauth_token.should eq('oauth_token')
+ client.oauth_token_secret.should eq('oauth_token_secret')
+ end
+ end
end
View
@@ -26,6 +26,10 @@
RSpec.configure do |c|
c.filter_run_excluding :broken => true
+
+ c.before :each do
+ Trello.reset!
+ end
end
module Helpers
View
@@ -4,9 +4,6 @@
include Trello::Authorization
describe Trello do
- before do
- Trello.reset!
- end
describe "self.configure" do
it "builds auth policy client uses to make requests" do

0 comments on commit 0625235

Please sign in to comment.