Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Threadsafe client #43

Merged
merged 19 commits into from

2 participants

@rossta

This pull request provides the ability to make requests on behalf of multiple trello users from a single application without messing with global configuration. In doing so, it addresses issue #36.

Now all API requests go through a configured client, which may be either through global configuration (as before), or through individually instantiated clients. The additions to the README explain how applications using the gem can take advantage. Of course, those who don't need multiple users can continue making requests as they have been.

@jeremytregunna jeremytregunna merged commit c1add74 into jeremytregunna:master
@rossta

Fixes #36

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 20, 2013
  1. @rossta
  2. @rossta
  3. @rossta

    Use Client#find for Board

    rossta authored
  4. @rossta

    Use Client#find for Card

    rossta authored
  5. @rossta
  6. @rossta

    Use Client#create for Board

    rossta authored
  7. @rossta
  8. @rossta
  9. @rossta
  10. @rossta

    Use Client#find for Token

    rossta authored
  11. @rossta
  12. @rossta

    README

    rossta authored
  13. @rossta
  14. @rossta
  15. @rossta

    syntax updates for ruby 1.8.7

    rossta authored
  16. @rossta
  17. @rossta
  18. @rossta

    README

    rossta authored
  19. @rossta

    README

    rossta authored
This page is out of date. Refresh to see the latest.
View
47 README.md
@@ -49,6 +49,53 @@ Trello.configure do |config|
end
```
+All the calls this library make to Trello require authentication using these keys. Be sure to protect them.
+
+So lets say you want to get information about the user *bobtester*. We can do something like this:
+
+```ruby
+bob = Trello::Member.find("bobtester")
+# Print out his name
+puts bob.full_name # "Bob Tester"
+# Print his bio
+puts bob.bio # A wonderfully delightful test user
+# How about a list of his boards?
+bob.boards
+```
+
+#### Multiple Users
+
+Applications that make requests on behalf of multiple Trello users have an alternative to global configuration. For each user's access token/secret pair, instantiate a `Trello::Client`:
+
+```ruby
+@client_bob = Trello::Client.new(
+ :consumer_key => YOUR_CONSUMER_KEY,
+ :consumer_secret => YOUR_CONSUMER_SECRET,
+ :oauth_token => "Bob's access token",
+ :oauth_token_secret => "Bob's access secret"
+)
+
+@client_alice = Trello::Client.new(
+ :consumer_key => YOUR_CONSUMER_KEY,
+ :consumer_secret => YOUR_CONSUMER_SECRET,
+ :oauth_token => "Alice's access token",
+ :oauth_token_secret => "Alice's access secret"
+)
+```
+
+You can now make threadsafe requests as the authenticated user:
+
+```ruby
+Thread.new do
+ @client_bob.find(:members, "bobtester")
+ @client_bob.find(:boards, "bobs_board_id")
+end
+Thread.new do
+ @client_alice.find(:members, "alicetester")
+ @client_alice.find(:boards, "alices_board_id")
+end
+```
+
## Special thanks
A special thanks goes out to [Ben Biddington](https://github.com/ben-biddington) who has contributed a significant amount
View
11 lib/trello.rb
@@ -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 lib/trello/action.rb
@@ -8,7 +8,7 @@ class Action < BasicData
class << self
# Locate a specific action and return a new Action object.
def find(id)
- super(:actions, id)
+ client.find(:action, id)
end
end
@@ -41,6 +41,6 @@ def list
end
# Returns the member who created the action.
- one :member_creator, :via => Member, :using => :member_creator_id
+ one :member_creator, :via => Member, :path => :members, :using => :member_creator_id
end
end
View
46 lib/trello/basic_data.rb
@@ -7,8 +7,36 @@ class BasicData
include ActiveModel::Serializers::JSON
class << self
- def find(path, id)
- client.get("/#{path}/#{id}").json_into(self)
+ def path_name
+ name.split("::").last.underscore
+ end
+
+ def find(id)
+ client.find(path_name, id)
+ end
+
+ def create(options)
+ client.create(path_name, options)
+ end
+
+ def save(options)
+ new(options).tap do |basic_data|
+ yield basic_data if block_given?
+ end.save
+ end
+
+ def parse(response)
+ response.json_into(self).tap do |basic_data|
+ yield basic_data if block_given?
+ end
+ end
+
+ def parse_many(response)
+ response.json_into(self).map do |data|
+ data.tap do |d|
+ yield d if block_given?
+ end
+ end
end
end
@@ -43,7 +71,13 @@ def self.one(name, opts = {})
options = opts.dup
klass = options.delete(:via) || Trello.const_get(name.to_s.camelize)
ident = options.delete(:using) || :id
- klass.find(self.send(ident))
+ path = options.delete(:path)
+
+ if path
+ client.find(path, self.send(ident))
+ else
+ klass.find(self.send(ident))
+ end
end
end
end
@@ -55,7 +89,7 @@ def self.many(name, opts = {})
resource = options.delete(:in) || self.class.to_s.split("::").last.downcase.pluralize
klass = options.delete(:via) || Trello.const_get(name.to_s.singularize.camelize)
params = options.merge(args[0] || {})
- resources = client.get("/#{resource}/#{id}/#{name}", params).json_into(klass)
+ resources = client.find_many(klass, "/#{resource}/#{id}/#{name}", params)
MultiAssociation.new(self, resources).proxy
end
end
@@ -67,6 +101,8 @@ def self.client
register_attributes :id, :readonly => [ :id ]
+ attr_writer :client
+
def initialize(fields = {})
update_fields(fields)
end
@@ -86,7 +122,7 @@ def ==(other)
end
def client
- self.class.client
+ @client ||= self.class.client
end
end
end
View
11 lib/trello/board.rb
@@ -11,13 +11,14 @@ class Board < BasicData
class << self
# Finds a board.
def find(id)
- super(:boards, id)
+ client.find(:board, id)
end
def create(fields)
- new('name' => fields[:name],
- 'desc' => fields[:description],
- 'closed' => fields[:closed] || false).save
+ client.create(:board,
+ 'name' => fields[:name],
+ 'desc' => fields[:description],
+ 'closed' => fields[:closed] || false)
end
def all
@@ -93,7 +94,7 @@ def find_card(card_id)
many :members, :filter => :all
# Returns a reference to the organization this board belongs to.
- one :organization, :using => :organization_id
+ one :organization, :path => :organizations, :using => :organization_id
# :nodoc:
def request_prefix
View
11 lib/trello/card.rb
@@ -12,14 +12,15 @@ class Card < BasicData
class << self
# Find a specific card by its id.
def find(id)
- super(:cards, id)
+ client.find(:card, id)
end
# Create a new card and save it on Trello.
def create(options)
- new('name' => options[:name],
+ client.create(:card,
+ 'name' => options[:name],
'idList' => options[:list_id],
- 'desc' => options[:description]).save
+ 'desc' => options[:description])
end
end
@@ -43,7 +44,7 @@ def update_fields(fields)
end
# Returns a reference to the board this card is part of.
- one :board, :using => :board_id
+ one :board, :path => :boards, :using => :board_id
# Returns a list of checklists associated with the card.
#
@@ -59,7 +60,7 @@ def check_item_states
# Returns a reference to the list this card is currently in.
- one :list, :using => :list_id
+ one :list, :path => :lists, :using => :list_id
# Returns a list of members who are assigned to this card.
def members
View
12 lib/trello/checklist.rb
@@ -9,12 +9,13 @@ class Checklist < BasicData
class << self
# Locate a specific checklist by its id.
def find(id)
- super(:checklists, id)
+ client.find(:checklist, id)
end
def create(options)
- new('name' => options[:name],
- 'idBoard' => options[:board_id]).save
+ client.create(:checklist,
+ 'name' => options[:name],
+ 'idBoard' => options[:board_id])
end
end
@@ -30,6 +31,7 @@ def update_fields(fields)
attributes[:url] = fields['url']
attributes[:check_items] = fields['checkItems']
attributes[:board_id] = fields['idBoard']
+ attributes[:list_id] = fields['idList']
attributes[:member_ids] = fields['idMembers']
self
end
@@ -61,10 +63,10 @@ def items
end
# Return a reference to the board the checklist is on.
- one :board, :using => :board_id
+ one :board, :path => :checklists, :using => :board_id
# Return a reference to the list the checklist is on.
- one :list, :using => :list_id
+ one :list, :path => :lists, :using => :list_id
# Return a list of members active in this checklist.
def members
View
68 lib/trello/client.rb
@@ -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?
@@ -25,6 +32,55 @@ def delete(path)
invoke_verb(:delete, uri)
end
+ # Finds given resource by id
+ #
+ # Examples:
+ # client.find(:board, "board1234")
+ # client.find(:member, "user1234")
+ #
+ def find(path, id)
+ response = get("/#{path.to_s.pluralize}/#{id}")
+ trello_class = class_from_path(path)
+ trello_class.parse response do |data|
+ data.client = self
+ end
+ end
+
+ # Finds given resource by path with params
+ def find_many(trello_class, path, params = {})
+ response = get(path, params)
+ trello_class.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)
+ #
+ def create(path, options)
+ trello_class = class_from_path(path)
+ trello_class.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(credentials)
+ end
+
+ private
+
def invoke_verb(name, uri, body = nil)
request = Request.new name, uri, {}, body
response = TInternet.execute auth_policy.authorize(request)
@@ -44,14 +100,6 @@ def invoke_verb(name, uri, body = nil)
response.body
end
- def configuration
- @configuration ||= Configuration.new
- end
-
- def auth_policy
- @auth_policy ||= auth_policy_class.new(configuration.credentials)
- end
-
def auth_policy_class
if configuration.oauth?
OAuthPolicy
@@ -62,5 +110,9 @@ def auth_policy_class
end
end
+ def class_from_path(path_or_class)
+ return path_or_class if path_or_class.is_a?(Class)
+ Trello.const_get(path_or_class.to_s.singularize.camelize)
+ end
end
end
View
23 lib/trello/configuration.rb
@@ -1,10 +1,27 @@
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 self.configurable_attributes
+ CONFIGURABLE_ATTRIBUTES
+ end
def initialize(attrs = {})
+ self.attributes = attrs
+ end
+
+ def attributes=(attrs = {})
attrs.each { |key, value| instance_variable_set("@#{key}", value) }
end
View
9 lib/trello/list.rb
@@ -10,12 +10,13 @@ class List < BasicData
class << self
# Finds a specific list, given an id.
def find(id)
- super(:lists, id)
+ client.find(:list, id)
end
def create(options)
- new('name' => options[:name],
- 'idBoard' => options[:board_id]).save
+ client.create(:list,
+ 'name' => options[:name],
+ 'idBoard' => options[:board_id])
end
end
@@ -64,7 +65,7 @@ def close!
end
# Return the board the list is connected to.
- one :board, :using => :board_id
+ one :board, :path => :boards, :using => :board_id
# Returns all the cards on this list.
#
View
2  lib/trello/member.rb
@@ -13,7 +13,7 @@ class << self
#
# The argument may be specified as either an _id_ or a _username_.
def find(id_or_username)
- super(:members, id_or_username)
+ client.find(:member, id_or_username)
end
end
View
4 lib/trello/notification.rb
@@ -7,7 +7,7 @@ class Notification < BasicData
class << self
# Locate a notification by its id
def find(id)
- super(:notifications, id)
+ client.find(:notification, id)
end
end
@@ -23,7 +23,7 @@ def update_fields(fields)
alias :unread? :unread
- one :member_creator, :via => Member, :using => :member_creator_id
+ one :member_creator, :path => :members, :via => Member, :using => :member_creator_id
def board
client.get("/notifications/#{id}/board").json_into(Board)
View
2  lib/trello/organization.rb
@@ -10,7 +10,7 @@ class Organization < BasicData
class << self
# Find an organization by its id.
def find(id)
- super(:organizations, id)
+ client.find(:organization, id)
end
end
View
8 lib/trello/token.rb
@@ -2,11 +2,11 @@ module Trello
class Token < BasicData
register_attributes :id, :member_id, :created_at, :permissions,
:readonly => [ :id, :member_id, :created_at, :permissions ]
-
+
class << self
# Finds a token
def find(token)
- super(:tokens, token)
+ client.find(:token, token)
end
end
@@ -19,6 +19,6 @@ def update_fields(fields)
end
# Returns a reference to the user who authorized the token.
- one :member, :using => :member_id
+ one :member, :path => :members, :using => :member_id
end
-end
+end
View
46 spec/action_spec.rb
@@ -4,67 +4,79 @@ module Trello
describe Action do
include Helpers
+ let(:action) { client.find(:action, '4ee2482134a81a757a08af47') }
+ let(:client) { Client.new }
+
before(:each) do
- Trello.client.stub(:get).with("/actions/4ee2482134a81a757a08af47").
+ client.stub(:get).with("/actions/4ee2482134a81a757a08af47").
and_return JSON.generate(actions_details.first)
+ end
- @action = Action.find('4ee2482134a81a757a08af47')
+ context "finding" do
+ let(:client) { Trello.client }
+
+ it "delegates to Trello.client#find" do
+ client.should_receive(:find).with(:action, '4ee2482134a81a757a08af47')
+ Action.find('4ee2482134a81a757a08af47')
+ end
+
+ it "is equivalent to client#find" do
+ Action.find('4ee2482134a81a757a08af47').should eq(action)
+ end
end
context "fields" do
- before(:all) do
- @detail = actions_details.first
- end
+ let(:detail) { actions_details.first }
it "gets its id" do
- @action.id.should == @detail['id']
+ action.id.should == detail['id']
end
it "gets its type" do
- @action.type.should == @detail['type']
+ action.type.should == detail['type']
end
it "has the same data" do
- @action.data.should == @detail['data']
+ action.data.should == detail['data']
end
it "gets the date" do
- @action.date.utc.iso8601.should == @detail['date']
+ action.date.utc.iso8601.should == detail['date']
end
end
context "boards" do
it "has a board" do
- Trello.client.stub(:get).with("/actions/4ee2482134a81a757a08af47/board").
+ client.stub(:get).with("/actions/4ee2482134a81a757a08af47/board").
and_return JSON.generate(boards_details.first)
- @action.board.should_not be_nil
+ action.board.should_not be_nil
end
end
context "card" do
it "has a card" do
- Trello.client.stub(:get).with("/actions/4ee2482134a81a757a08af47/card").
+ client.stub(:get).with("/actions/4ee2482134a81a757a08af47/card").
and_return JSON.generate(cards_details.first)
- @action.card.should_not be_nil
+ action.card.should_not be_nil
end
end
context "list" do
it "has a list of lists" do
- Trello.client.stub(:get).with("/actions/4ee2482134a81a757a08af47/list").
+ client.stub(:get).with("/actions/4ee2482134a81a757a08af47/list").
and_return JSON.generate(lists_details.first)
- @action.list.should_not be_nil
+ action.list.should_not be_nil
end
end
context "member creator" do
it "knows its member creator" do
- Trello.client.stub(:get).with("/members/abcdef123456789123456789").and_return user_payload
+ client.stub(:get).with("/members/abcdef123456789123456789").and_return user_payload
- @action.member_creator.should_not be_nil
+ action.member_creator.should_not be_nil
end
end
end
View
261 spec/board_spec.rb
@@ -4,232 +4,253 @@ module Trello
describe Board do
include Helpers
+ let(:board) { client.find(:board, 'abcdef123456789123456789') }
+ let(:client) { Client.new }
+
before(:each) do
- Trello.client.stub(:get).with("/boards/abcdef123456789123456789").
+ client.stub(:get).with("/boards/abcdef123456789123456789").
and_return JSON.generate(boards_details.first)
+ end
+
+ context "finding" do
+ let(:client) { Trello.client }
- @board = Board.find('abcdef123456789123456789')
+ it "delegates to client#find" do
+ client.should_receive(:find).with(:board, 'abcdef123456789123456789')
+ Board.find('abcdef123456789123456789')
+ end
+
+ it "is equivalent to client#find" do
+ Board.find('abcdef123456789123456789').should eq(board)
+ end
end
- it "gets all boards" do
- Member.stub_chain(:find, :username).and_return "testuser"
- Trello.client.stub(:get).with("/members/testuser/boards").and_return boards_payload
+ context "self.all" do
+ let(:client) { Trello.client }
- expected = Board.new(boards_details.first)
- Board.all.first.should eq(expected)
+ it "gets all boards" do
+ Member.stub_chain(:find, :username).and_return "testuser"
+ client.stub(:get).with("/members/testuser/boards").and_return boards_payload
+
+ expected = Board.new(boards_details.first)
+ Board.all.first.should eq(expected)
+ end
end
context "fields" do
it "gets an id" do
- @board.id.should_not be_nil
+ board.id.should_not be_nil
end
it "gets a name" do
- @board.name.should_not be_nil
+ board.name.should_not be_nil
end
it "gets the description" do
- @board.description.should_not be_nil
+ board.description.should_not be_nil
end
it "knows if it is closed or open" do
- @board.closed?.should_not be_nil
+ board.closed?.should_not be_nil
end
it "gets its url" do
- @board.url.should_not be_nil
+ board.url.should_not be_nil
end
end
context "actions" do
it "has a list of actions" do
- Trello.client.stub(:get).with("/boards/abcdef123456789123456789/actions", {:filter => :all}).
+ client.stub(:get).with("/boards/abcdef123456789123456789/actions", {:filter => :all}).
and_return actions_payload
- @board.actions.count.should be > 0
+ board.actions.count.should be > 0
end
end
context "cards" do
it "gets its list of cards" do
- Trello.client.stub(:get).with("/boards/abcdef123456789123456789/cards", { :filter => :open }).
+ client.stub(:get).with("/boards/abcdef123456789123456789/cards", { :filter => :open }).
and_return cards_payload
- @board.cards.count.should be > 0
+ board.cards.count.should be > 0
end
end
context "find_card" do
it "gets a card" do
- Trello.client.stub(:get).with("/boards/abcdef123456789123456789/cards/1").
+ client.stub(:get).with("/boards/abcdef123456789123456789/cards/1").
and_return card_payload
- @board.find_card(1).should be_a(Card)
+ board.find_card(1).should be_a(Card)
end
end
context "lists" do
it "has a list of lists" do
- Trello.client.stub(:get).with("/boards/abcdef123456789123456789/lists", hash_including(:filter => :open)).
+ client.stub(:get).with("/boards/abcdef123456789123456789/lists", hash_including(:filter => :open)).
and_return lists_payload
- @board.has_lists?.should be true
+ board.has_lists?.should be true
end
end
context "members" do
it "has a list of members" do
- Trello.client.stub(:get).with("/boards/abcdef123456789123456789/members", hash_including(:filter => :all)).
+ client.stub(:get).with("/boards/abcdef123456789123456789/members", hash_including(:filter => :all)).
and_return JSON.generate([user_details])
- @board.members.count.should be > 0
+ board.members.count.should be > 0
end
end
context "organization" do
it "belongs to an organization" do
- Trello.client.stub(:get).with("/organizations/abcdef123456789123456789").
+ client.stub(:get).with("/organizations/abcdef123456789123456789").
and_return JSON.generate(orgs_details.first)
- @board.organization.should_not be_nil
+ board.organization.should_not be_nil
end
end
it "is not closed" do
- @board.closed?.should_not be_true
+ board.closed?.should_not be_true
end
- end
-
- describe "#update_fields" do
- it "does not set any fields when the fields argument is empty" do
- expected = {
- 'id' => "id",
- 'name' => "name",
- 'desc' => "desc",
- 'closed' => false,
- 'url' => "url",
- 'idOrganization' => "org_id"
- }
-
- board = Board.new(expected)
- board.update_fields({})
-
- expected.each_pair do |key, value|
- if board.respond_to?(key)
- board.send(key).should == value
+ describe "#update_fields" do
+ it "does not set any fields when the fields argument is empty" do
+ expected = {
+ 'id' => "id",
+ 'name' => "name",
+ 'desc' => "desc",
+ 'closed' => false,
+ 'url' => "url",
+ 'idOrganization' => "org_id"
+ }
+
+ board = Board.new(expected)
+ board.client = client
+
+ board.update_fields({})
+
+ expected.each_pair do |key, value|
+ if board.respond_to?(key)
+ board.send(key).should == value
+ end
end
+
+ board.description.should == expected['desc']
+ board.organization_id.should == expected['idOrganization']
end
- board.description.should == expected['desc']
- board.organization_id.should == expected['idOrganization']
+ it "sets any attributes supplied in the fields argument"
end
- it "sets any attributes supplied in the fields argument"
- end
+ describe "#save" do
+ let(:client) { Trello.client }
- describe "#save" do
- include Helpers
+ let(:any_board_json) do
+ JSON.generate(boards_details.first)
+ end
- let(:any_board_json) do
- JSON.generate(boards_details.first)
- end
+ it "cannot currently save a new instance" do
+ client.should_not_receive :put
- it "cannot currently save a new instance" do
- Trello.client.should_not_receive :put
+ the_new_board = Board.new
+ lambda{the_new_board.save}.should raise_error
+ end
- the_new_board = Board.new
- lambda{the_new_board.save}.should raise_error
- end
+ it "puts all fields except id" do
+ expected_fields = %w{name description closed}.map{|s| s.to_sym}
- it "puts all fields except id" do
- expected_fields = %w{name description closed}.map{|s| s.to_sym}
+ client.should_receive(:put) do |anything, body|
+ body.keys.should =~ expected_fields
+ any_board_json
+ end
- Trello.client.should_receive(:put) do |anything, body|
- body.keys.should =~ expected_fields
- any_board_json
+ the_new_board = Board.new 'id' => "xxx"
+ the_new_board.save
end
- the_new_board = Board.new 'id' => "xxx"
- the_new_board.save
- end
+ it "mutates the current instance" do
+ client.stub(:put).and_return any_board_json
- it "mutates the current instance" do
- Trello.client.stub(:put).and_return any_board_json
+ board = Board.new 'id' => "xxx"
- board = Board.new 'id' => "xxx"
+ the_result_of_save = board.save
- the_result_of_save = board.save
+ the_result_of_save.should equal board
+ end
- the_result_of_save.should equal board
- end
+ it "uses the correct resource" do
+ expected_resource_id = "xxx_board_id_xxx"
- it "uses the correct resource" do
- expected_resource_id = "xxx_board_id_xxx"
+ client.should_receive(:put) do |path, anything|
+ path.should =~ /#{expected_resource_id}\/$/
+ any_board_json
+ end
- Trello.client.should_receive(:put) do |path, anything|
- path.should =~ /#{expected_resource_id}\/$/
- any_board_json
+ the_new_board = Board.new 'id' => expected_resource_id
+ the_new_board.save
end
- the_new_board = Board.new 'id' => expected_resource_id
- the_new_board.save
+ it "saves OR updates depending on whether or not it has an id set"
end
- it "saves OR updates depending on whether or not it has an id set"
- end
-
- describe '#update!' do
- include Helpers
+ describe '#update!' do
+ let(:client) { Trello.client }
- let(:any_board_json) do
- JSON.generate(boards_details.first)
- end
+ let(:any_board_json) do
+ JSON.generate(boards_details.first)
+ end
- it "puts basic attributes" do
- board = Board.new 'id' => "board_id"
+ it "puts basic attributes" do
+ board = Board.new 'id' => "board_id"
- board.name = "new name"
- board.description = "new description"
- board.closed = true
+ board.name = "new name"
+ board.description = "new description"
+ board.closed = true
- Trello.client.should_receive(:put).with("/boards/#{board.id}/", {
- :name => "new name",
- :description => "new description",
- :closed => true
- }).and_return any_board_json
- board.update!
+ client.should_receive(:put).with("/boards/#{board.id}/", {
+ :name => "new name",
+ :description => "new description",
+ :closed => true
+ }).and_return any_board_json
+ board.update!
+ end
end
- end
- describe "Repository" do
- include Helpers
+ describe "Repository" do
+ include Helpers
- let(:any_board_json) do
- JSON.generate(boards_details.first)
- end
+ let(:client) { Trello.client }
- it "creates a new board with whatever attributes are supplied " do
- expected_attributes = { :name => "Any new board name", :description => "Any new board desription" }
- sent_attributes = { :name => expected_attributes[:name], :desc => expected_attributes[:description] }
+ let(:any_board_json) do
+ JSON.generate(boards_details.first)
+ end
- Trello.client.should_receive(:post).with("/boards", sent_attributes).and_return any_board_json
+ it "creates a new board with whatever attributes are supplied " do
+ expected_attributes = { :name => "Any new board name", :description => "Any new board desription" }
+ sent_attributes = { :name => expected_attributes[:name], :desc => expected_attributes[:description] }
- Board.create expected_attributes
- end
+ client.should_receive(:post).with("/boards", sent_attributes).and_return any_board_json
- it "posts to the boards collection" do
- Trello.client.should_receive(:post).with("/boards", anything).and_return any_board_json
+ Board.create expected_attributes
+ end
- Board.create :xxx => ""
- end
+ it "posts to the boards collection" do
+ client.should_receive(:post).with("/boards", anything).and_return any_board_json
- it "returns a board" do
- Trello.client.stub(:post).with("/boards", anything).and_return any_board_json
+ Board.create :xxx => ""
+ end
- the_new_board = Board.create :xxx => ""
- the_new_board.should be_a Board
- end
+ it "returns a board" do
+ client.stub(:post).with("/boards", anything).and_return any_board_json
- it "at least name is required"
+ the_new_board = Board.create :xxx => ""
+ the_new_board.should be_a Board
+ end
+
+ it "at least name is required"
+ end
end
end
View
78 spec/card_spec.rb
@@ -4,14 +4,30 @@ module Trello
describe Card do
include Helpers
- let(:card) { Card.find('abcdef123456789123456789') }
+ let(:card) { client.find(:card, 'abcdef123456789123456789') }
+ let(:client) { Client.new }
before(:each) do
- Trello.client.stub(:get).with("/cards/abcdef123456789123456789").
+ client.stub(:get).with("/cards/abcdef123456789123456789").
and_return JSON.generate(cards_details.first)
end
+ context "finding" do
+ let(:client) { Trello.client }
+
+ it "delegates to Trello.client#find" do
+ client.should_receive(:find).with(:card, 'abcdef123456789123456789')
+ Card.find('abcdef123456789123456789')
+ end
+
+ it "is equivalent to client#find" do
+ Card.find('abcdef123456789123456789').should eq(card)
+ end
+ end
+
context "creating" do
+ let(:client) { Trello.client }
+
it "creates a new record" do
card = Card.new(cards_details.first)
card.should be_valid
@@ -37,7 +53,7 @@ module Trello
expected_payload = {:name => "Test Card", :desc => nil, :idList => "abcdef123456789123456789"}
- Trello.client.should_receive(:post).with("/cards", expected_payload).and_return result
+ client.should_receive(:post).with("/cards", expected_payload).and_return result
card = Card.create(cards_details.first.merge(payload.merge(:list_id => lists_details.first['id'])))
@@ -53,7 +69,7 @@ module Trello
:name => expected_new_name,
}
- Trello.client.should_receive(:put).once.with("/cards/abcdef123456789123456789", payload)
+ client.should_receive(:put).once.with("/cards/abcdef123456789123456789", payload)
card.name = expected_new_name
card.save
@@ -88,54 +104,54 @@ module Trello
context "actions" do
it "asks for all actions by default" do
- Trello.client.stub(:get).with("/cards/abcdef123456789123456789/actions", { :filter => :all }).and_return actions_payload
+ client.stub(:get).with("/cards/abcdef123456789123456789/actions", { :filter => :all }).and_return actions_payload
card.actions.count.should be > 0
end
it "allows overriding the filter" do
- Trello.client.stub(:get).with("/cards/abcdef123456789123456789/actions", { :filter => :updateCard }).and_return actions_payload
+ client.stub(:get).with("/cards/abcdef123456789123456789/actions", { :filter => :updateCard }).and_return actions_payload
card.actions(:filter => :updateCard).count.should be > 0
end
end
context "boards" do
it "has a board" do
- Trello.client.stub(:get).with("/boards/abcdef123456789123456789").and_return JSON.generate(boards_details.first)
+ client.stub(:get).with("/boards/abcdef123456789123456789").and_return JSON.generate(boards_details.first)
card.board.should_not be_nil
end
end
context "checklists" do
it "has a list of checklists" do
- Trello.client.stub(:get).with("/cards/abcdef123456789123456789/checklists", { :filter => :all }).and_return checklists_payload
+ client.stub(:get).with("/cards/abcdef123456789123456789/checklists", { :filter => :all }).and_return checklists_payload
card.checklists.count.should be > 0
end
end
context "list" do
it 'has a list' do
- Trello.client.stub(:get).with("/lists/abcdef123456789123456789").and_return JSON.generate(lists_details.first)
+ client.stub(:get).with("/lists/abcdef123456789123456789").and_return JSON.generate(lists_details.first)
card.list.should_not be_nil
end
it 'can be moved to another list' do
other_list = stub(:id => '987654321987654321fedcba')
payload = {:value => other_list.id}
- Trello.client.should_receive(:put).with("/cards/abcdef123456789123456789/idList", payload)
+ client.should_receive(:put).with("/cards/abcdef123456789123456789/idList", payload)
card.move_to_list(other_list)
end
it 'should not be moved if new list is identical to old list' do
other_list = stub(:id => 'abcdef123456789123456789')
payload = {:value => other_list.id}
- Client.should_not_receive(:put)
+ client.should_not_receive(:put)
card.move_to_list(other_list)
end
it 'can be moved to another board' do
other_board = stub(:id => '987654321987654321fedcba')
payload = {:value => other_board.id}
- Trello.client.should_receive(:put).with("/cards/abcdef123456789123456789/idBoard", payload)
+ client.should_receive(:put).with("/cards/abcdef123456789123456789/idBoard", payload)
card.move_to_board(other_board)
end
@@ -143,21 +159,21 @@ module Trello
other_board = stub(:id => '987654321987654321fedcba')
other_list = stub(:id => '987654321987654321aalist')
payload = {:value => other_board.id, :idList => other_list.id}
- Trello.client.should_receive(:put).with("/cards/abcdef123456789123456789/idBoard", payload)
+ client.should_receive(:put).with("/cards/abcdef123456789123456789/idBoard", payload)
card.move_to_board(other_board, other_list)
end
it 'should not be moved if new board is identical with old board', :focus => true do
other_board = stub(:id => 'abcdef123456789123456789')
- Client.should_not_receive(:put)
+ client.should_not_receive(:put)
card.move_to_board(other_board)
end
end
context "members" do
it "has a list of members" do
- Trello.client.stub(:get).with("/boards/abcdef123456789123456789").and_return JSON.generate(boards_details.first)
- Trello.client.stub(:get).with("/members/abcdef123456789123456789").and_return user_payload
+ client.stub(:get).with("/boards/abcdef123456789123456789").and_return JSON.generate(boards_details.first)
+ client.stub(:get).with("/members/abcdef123456789123456789").and_return user_payload
card.board.should_not be_nil
card.members.should_not be_nil
@@ -168,20 +184,20 @@ module Trello
payload = {
:value => new_member.id
}
- Trello.client.should_receive(:post).with("/cards/abcdef123456789123456789/members", payload)
+ client.should_receive(:post).with("/cards/abcdef123456789123456789/members", payload)
card.add_member(new_member)
end
it "allows a member to be removed from a card" do
existing_member = stub(:id => '4ee7df3ce582acdec80000b2')
- Trello.client.should_receive(:delete).with("/cards/abcdef123456789123456789/members/#{existing_member.id}")
+ client.should_receive(:delete).with("/cards/abcdef123456789123456789/members/#{existing_member.id}")
card.remove_member(existing_member)
end
end
context "comments" do
it "posts a comment" do
- Trello.client.should_receive(:post).
+ client.should_receive(:post).
with("/cards/abcdef123456789123456789/actions/comments", { :text => 'testing' }).
and_return JSON.generate(boards_details.first)
@@ -191,7 +207,7 @@ module Trello
context "labels" do
it "can retrieve labels" do
- Trello.client.stub(:get).with("/cards/abcdef123456789123456789/labels").
+ client.stub(:get).with("/cards/abcdef123456789123456789/labels").
and_return label_payload
labels = card.labels
labels.size.should == 2
@@ -204,28 +220,28 @@ module Trello
end
it "can add a label" do
- Trello.client.stub(:post).with("/cards/abcdef123456789123456789/labels", { :value => 'green' }).
+ client.stub(:post).with("/cards/abcdef123456789123456789/labels", { :value => 'green' }).
and_return "not important"
card.add_label('green')
card.errors.should be_empty
end
it "can remove a label" do
- Trello.client.stub(:delete).with("/cards/abcdef123456789123456789/labels/green").
+ client.stub(:delete).with("/cards/abcdef123456789123456789/labels/green").
and_return "not important"
card.remove_label('green')
card.errors.should be_empty
end
it "throws an error when trying to add a label with an unknown colour" do
- Trello.client.stub(:post).with("/cards/abcdef123456789123456789/labels", { :value => 'green' }).
+ client.stub(:post).with("/cards/abcdef123456789123456789/labels", { :value => 'green' }).
and_return "not important"
card.add_label('mauve')
card.errors.full_messages.to_sentence.should == "Label colour 'mauve' does not exist"
end
it "throws an error when trying to remove a label with an unknown colour" do
- Trello.client.stub(:delete).with("/cards/abcdef123456789123456789/labels/mauve").
+ client.stub(:delete).with("/cards/abcdef123456789123456789/labels/mauve").
and_return "not important"
card.remove_label('mauve')
card.errors.full_messages.to_sentence.should == "Label colour 'mauve' does not exist"
@@ -235,8 +251,8 @@ module Trello
context "attachments" do
it "can add an attachment" do
f = File.new('spec/list_spec.rb', 'r')
- Trello.client.stub(:get).with("/cards/abcdef123456789123456789/attachments").and_return attachments_payload
- Trello.client.stub(:post).with("/cards/abcdef123456789123456789/attachments",
+ client.stub(:get).with("/cards/abcdef123456789123456789/attachments").and_return attachments_payload
+ client.stub(:post).with("/cards/abcdef123456789123456789/attachments",
{ :file => f, :name => '' }).
and_return "not important"
@@ -246,17 +262,17 @@ module Trello
end
it "can list the existing attachments" do
- Trello.client.stub(:get).with("/boards/abcdef123456789123456789").and_return JSON.generate(boards_details.first)
- Trello.client.stub(:get).with("/cards/abcdef123456789123456789/attachments").and_return attachments_payload
+ client.stub(:get).with("/boards/abcdef123456789123456789").and_return JSON.generate(boards_details.first)
+ client.stub(:get).with("/cards/abcdef123456789123456789/attachments").and_return attachments_payload
card.board.should_not be_nil
card.attachments.should_not be_nil
end
it "can remove an attachment" do
- Trello.client.stub(:delete).with("/cards/abcdef123456789123456789/attachments/abcdef123456789123456789").
+ client.stub(:delete).with("/cards/abcdef123456789123456789/attachments/abcdef123456789123456789").
and_return "not important"
- Trello.client.stub(:get).with("/cards/abcdef123456789123456789/attachments").and_return attachments_payload
+ client.stub(:get).with("/cards/abcdef123456789123456789/attachments").and_return attachments_payload
card.remove_attachment(card.attachments.first)
card.errors.should be_empty
@@ -282,7 +298,7 @@ module Trello
:closed => true,
}
- Trello.client.should_receive(:put).once.with("/cards/abcdef123456789123456789", payload)
+ client.should_receive(:put).once.with("/cards/abcdef123456789123456789", payload)
card.close!
end
View
37 spec/checklist_spec.rb
@@ -4,14 +4,30 @@ module Trello
describe Checklist do
include Helpers
- let(:checklist) { Checklist.find('abcdef123456789123456789') }
+ let(:checklist) { client.find(:checklist, 'abcdef123456789123456789') }
+ let(:client) { Client.new }
before(:each) do
- Trello.client.stub(:get).with("/checklists/abcdef123456789123456789").
+ client.stub(:get).with("/checklists/abcdef123456789123456789").
and_return JSON.generate(checklists_details.first)
end
+ context "finding" do
+ let(:client) { Trello.client }
+
+ it "delegates to Trello.client#find" do
+ client.should_receive(:find).with(:checklist, 'abcdef123456789123456789')
+ Checklist.find('abcdef123456789123456789')
+ end
+
+ it "is equivalent to client#find" do
+ Checklist.find('abcdef123456789123456789').should eq(checklist)
+ end
+ end
+
context "creating" do
+ let(:client) { Trello.client }
+
it 'creates a new record and saves it on Trello', :refactor => true do
payload = {
:name => 'Test Checklist',
@@ -22,7 +38,7 @@ module Trello
expected_payload = {:name => "Test Checklist", :idBoard => "abcdef123456789123456789"}
- Trello.client.should_receive(:post).with("/checklists", expected_payload).and_return result
+ client.should_receive(:post).with("/checklists", expected_payload).and_return result
checklist = Checklist.create(checklists_details.first.merge(payload.merge(:board_id => boards_details.first['id'])))
@@ -39,12 +55,25 @@ module Trello
}
result = JSON.generate(checklists_details.first)
- Trello.client.should_receive(:put).once.with("/checklists/abcdef123456789123456789", payload).and_return result
+ client.should_receive(:put).once.with("/checklists/abcdef123456789123456789", payload).and_return result
checklist.name = expected_new_name
checklist.save
end
end
+ context "board" do
+ it "has a board" do
+ client.stub(:get).with("/boards/abcdef123456789123456789").and_return JSON.generate(boards_details.first)
+ checklist.board.should_not be_nil
+ end
+ end
+
+ context "list" do
+ it 'has a list' do
+ client.stub(:get).with("/lists/abcdef123456789123456789").and_return JSON.generate(lists_details.first)
+ checklist.list.should_not be_nil
+ end
+ end
end
end
View
34 spec/client_spec.rb
@@ -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
92 spec/list_spec.rb
@@ -4,11 +4,61 @@ module Trello
describe List do
include Helpers
+ let(:list) { client.find(:list, "abcdef123456789123456789") }
+ let(:client) { Client.new }
+
before(:each) do
- Trello.client.stub(:get).with("/lists/abcdef123456789123456789").and_return JSON.generate(lists_details.first)
- Trello.client.stub(:get).with("/boards/abcdef123456789123456789").and_return JSON.generate(boards_details.first)
+ client.stub(:get).with("/lists/abcdef123456789123456789").and_return JSON.generate(lists_details.first)
+ client.stub(:get).with("/boards/abcdef123456789123456789").and_return JSON.generate(boards_details.first)
+ end
+
+ context "finding" do
+ let(:client) { Trello.client }
+
+ it "delegates to client#find" do
+ client.should_receive(:find).with(:list, 'abcdef123456789123456789')
+ List.find('abcdef123456789123456789')
+ end
+
+ it "is equivalent to client#find" do
+ List.find('abcdef123456789123456789').should eq(list)
+ end
+ end
+
+ context "creating" do
+ let(:client) { Trello.client }
- @list = List.find("abcdef123456789123456789")
+ it "creates a new record" do
+ list = List.new(lists_details.first)
+ list.should be_valid
+ end
+
+ it 'must not be valid if not given a name' do
+ list = List.new(lists_details.first.except('name'))
+ list.should_not be_valid
+ end
+
+ it 'must not be valid if not given a list id' do
+ list = List.new(lists_details.first.except('id'))
+ list.should_not be_valid
+ end
+
+ it 'creates a new record and saves it on Trello', :refactor => true do
+ payload = {
+ :name => 'Test List',
+ :board_id => 'abcdef123456789123456789'
+ }
+
+ result = JSON.generate(payload)
+
+ expected_payload = {:name => "Test List", :closed => false, :idBoard => "abcdef123456789123456789"}
+
+ client.should_receive(:post).with("/lists", expected_payload).and_return result
+
+ list = List.create(payload)
+
+ list.class.should be List
+ end
end
context "updating" do
@@ -20,69 +70,69 @@ module Trello
:closed => false
}
- Trello.client.should_receive(:put).once.with("/lists/abcdef123456789123456789", payload)
- @list.name = expected_new_name
- @list.save
+ client.should_receive(:put).once.with("/lists/abcdef123456789123456789", payload)
+ list.name = expected_new_name
+ list.save
end
end
context "fields" do
it "gets its id" do
- @list.id.should == lists_details.first['id']
+ list.id.should == lists_details.first['id']
end
it "gets its name" do
- @list.name.should == lists_details.first['name']
+ list.name.should == lists_details.first['name']
end
it "knows if it is open or closed" do
- @list.closed.should == lists_details.first['closed']
+ list.closed.should == lists_details.first['closed']
end
it "has a board" do
- @list.board.should == Board.new(boards_details.first)
+ list.board.should == Board.new(boards_details.first)
end
it "gets its position" do
- @list.pos.should == lists_details.first['pos']
+ list.pos.should == lists_details.first['pos']
end
end
context "actions" do
it "has a list of actions" do
- Trello.client.stub(:get).with("/lists/abcdef123456789123456789/actions", { :filter => :all }).and_return actions_payload
- @list.actions.count.should be > 0
+ client.stub(:get).with("/lists/abcdef123456789123456789/actions", { :filter => :all }).and_return actions_payload
+ list.actions.count.should be > 0
end
end
context "cards" do
it "has a list of cards" do
- Trello.client.stub(:get).with("/lists/abcdef123456789123456789/cards", { :filter => :open }).and_return cards_payload
- @list.cards.count.should be > 0
+ client.stub(:get).with("/lists/abcdef123456789123456789/cards", { :filter => :open }).and_return cards_payload
+ list.cards.count.should be > 0
end
end
describe "#closed?" do
it "returns the closed attribute" do
- @list.closed?.should_not be_true
+ list.closed?.should_not be_true
end
end
describe "#close" do
it "updates the close attribute to true" do
- @list.close
- @list.closed.should be_true
+ list.close
+ list.closed.should be_true
end
end
describe "#close!" do
it "updates the close attribute to true and saves the list" do
- Trello.client.should_receive(:put).once.with("/lists/abcdef123456789123456789", {
- :name => @list.name,
+ client.should_receive(:put).once.with("/lists/abcdef123456789123456789", {
+ :name => list.name,
:closed => true
})
- @list.close!
+ list.close!
end
end
end
View
60 spec/member_spec.rb
@@ -6,86 +6,100 @@ module Trello
describe Member do
include Helpers
+ let(:member) { client.find(:member, 'abcdef123456789012345678') }
+ let(:client) { Client.new }
+
before(:each) do
- Trello.client.stub(:get).with("/members/abcdef123456789012345678").and_return user_payload
+ client.stub(:get).with("/members/abcdef123456789012345678").and_return user_payload
+ end
+
+ context "finding" do
+ let(:client) { Trello.client }
- @member = Member.find('abcdef123456789012345678')
+ it "delegates to Trello.client#find" do
+ client.should_receive(:find).with(:member, 'abcdef123456789012345678')
+ Member.find('abcdef123456789012345678')
+ end
+
+ it "is equivalent to client#find" do
+ Member.find('abcdef123456789012345678').should eq(member)
+ end
end
context "actions" do
it "retrieves a list of actions", :refactor => true do
- Trello.client.stub(:get).with("/members/abcdef123456789012345678/actions", { :filter => :all }).and_return actions_payload
- @member.actions.count.should be > 0
+ client.stub(:get).with("/members/abcdef123456789012345678/actions", { :filter => :all }).and_return actions_payload
+ member.actions.count.should be > 0
end
end
context "boards" do
it "has a list of boards" do
- Trello.client.stub(:get).with("/members/abcdef123456789012345678/boards", { :filter => :all }).and_return boards_payload
- boards = @member.boards
+ client.stub(:get).with("/members/abcdef123456789012345678/boards", { :filter => :all }).and_return boards_payload
+ boards = member.boards
boards.count.should be > 0
end
end
context "cards" do
it "has a list of cards" do
- Trello.client.stub(:get).with("/members/abcdef123456789012345678/cards", { :filter => :open }).and_return cards_payload
- cards = @member.cards
+ client.stub(:get).with("/members/abcdef123456789012345678/cards", { :filter => :open }).and_return cards_payload
+ cards = member.cards
cards.count.should be > 0
end
end
context "organizations" do
it "has a list of organizations" do
- Trello.client.stub(:get).with("/members/abcdef123456789012345678/organizations", { :filter => :all }).and_return orgs_payload
- orgs = @member.organizations
+ client.stub(:get).with("/members/abcdef123456789012345678/organizations", { :filter => :all }).and_return orgs_payload
+ orgs = member.organizations
orgs.count.should be > 0
end
end
context "notifications" do
it "has a list of notifications" do
- Trello.client.stub(:get).with("/members/abcdef123456789012345678/notifications", {}).and_return "[" << notification_payload << "]"
- @member.notifications.count.should be 1
+ client.stub(:get).with("/members/abcdef123456789012345678/notifications", {}).and_return "[" << notification_payload << "]"
+ member.notifications.count.should be 1
end
end
context "personal" do
it "gets the members bio" do
- @member.bio.should == user_details['bio']
+ member.bio.should == user_details['bio']
end
it "gets the full name" do
- @member.full_name.should == user_details['fullName']
+ member.full_name.should == user_details['fullName']
end
it "gets the avatar id" do
- @member.avatar_id.should == user_details['avatarHash']
+ member.avatar_id.should == user_details['avatarHash']
end
it "returns a valid url for the avatar" do
- @member.avatar_url(:size => :large).should == "https://trello-avatars.s3.amazonaws.com/abcdef1234567890abcdef1234567890/170.png"
- @member.avatar_url(:size => :small).should == "https://trello-avatars.s3.amazonaws.com/abcdef1234567890abcdef1234567890/30.png"
+ member.avatar_url(:size => :large).should == "https://trello-avatars.s3.amazonaws.com/abcdef1234567890abcdef1234567890/170.png"
+ member.avatar_url(:size => :small).should == "https://trello-avatars.s3.amazonaws.com/abcdef1234567890abcdef1234567890/30.png"
end
it "gets the url" do
- @member.url.should == user_details['url']
+ member.url.should == user_details['url']
end
it "gets the username" do
- @member.username.should == user_details['username']
+ member.username.should == user_details['username']
end
end
context "modification" do
it "lets us know a field has changed without committing it" do
- @member.changed?.should be_false
- @member.bio = "New and amazing"
- @member.changed?.should be_true
+ member.changed?.should be_false
+ member.bio = "New and amazing"
+ member.changed?.should be_true
end
it "doesn't understand the #id= method" do
- lambda { @member.id = "42" }.should raise_error NoMethodError
+ lambda { member.id = "42" }.should raise_error NoMethodError
end
end
end
View
50 spec/notification_spec.rb
@@ -4,79 +4,83 @@ module Trello
describe Notification do
include Helpers
+ let(:notification) { member.notifications.first }
+ let(:member) { client.find(:member, "abcdef123456789012345678") }
+ let(:client) { Client.new }
+
before(:each) do
- Trello.client.stub(:get).with("/members/abcdef123456789012345678").and_return user_payload
- member = Member.find("abcdef123456789012345678")
- Trello.client.stub(:get).with("/members/abcdef123456789012345678/notifications", {}).and_return "[" << notification_payload << "]"
- @notification = member.notifications.first
+ client.stub(:get).with("/members/abcdef123456789012345678").and_return user_payload
+ client.stub(:get).with("/members/abcdef123456789012345678/notifications", {}).and_return "[" << notification_payload << "]"
end
context "finding" do
+ let(:client) { Trello.client }
+
it "can find a specific notification" do
- Trello.client.stub(:get).with("/notifications/#{notification_details['id']}").and_return notification_payload
- Notification.find(notification_details['id']).should == @notification
+ client.stub(:get).with("/notifications/#{notification_details['id']}").and_return notification_payload
+ Notification.find(notification_details['id']).should == notification
end
end
context "boards" do
it "can retrieve the board" do
- Trello.client.stub(:get).with("/notifications/#{notification_details['id']}/board").and_return JSON.generate(boards_details.first)
- @notification.board.id.should == boards_details.first['id']
+ client.stub(:get).with("/notifications/#{notification_details['id']}/board").and_return JSON.generate(boards_details.first)
+ notification.board.id.should == boards_details.first['id']
end
end
context "lists" do
it "can retrieve the list" do
- Trello.client.stub(:get).with("/notifications/#{notification_details['id']}/list").and_return JSON.generate(lists_details.first)
- @notification.list.id.should == lists_details.first['id']
+ client.stub(:get).with("/notifications/#{notification_details['id']}/list").and_return JSON.generate(lists_details.first)
+ notification.list.id.should == lists_details.first['id']
end
end
context "cards" do
it "can retrieve the card" do
- Trello.client.stub(:get).with("/notifications/#{notification_details['id']}/card").and_return JSON.generate(cards_details.first)
- @notification.card.id.should == cards_details.first['id']
+ client.stub(:get).with("/notifications/#{notification_details['id']}/card").and_return JSON.generate(cards_details.first)
+ notification.card.id.should == cards_details.first['id']
end
end
context "members" do
it "can retrieve the member" do
- Trello.client.stub(:get).with("/notifications/#{notification_details['id']}/member").and_return user_payload
- @notification.member.id.should == user_details['id']
+ client.stub(:get).with("/notifications/#{notification_details['id']}/member").and_return user_payload
+ notification.member.id.should == user_details['id']
end
it "can retrieve the member creator" do
- Trello.client.stub(:get).with("/members/#{user_details['id']}").and_return user_payload
- @notification.member_creator.id.should == user_details['id']
+ client.stub(:get).with("/members/#{user_details['id']}").and_return user_payload
+ notification.member_creator.id.should == user_details['id']
end
end
context "organization" do
it "can retrieve the organization" do
- Trello.client.stub(:get).with("/notifications/#{notification_details['id']}/organization").and_return JSON.generate(orgs_details.first)
- @notification.organization.id.should == orgs_details.first['id']
+ client.stub(:get).with("/notifications/#{notification_details['id']}/organization").and_return JSON.generate(orgs_details.first)
+ notification.organization.id.should == orgs_details.first['id']
end
end
context "local" do
it "gets the read status" do
- @notification.unread?.should == notification_details['unread']
+ notification.unread?.should == notification_details['unread']
end
it "gets the type" do
- @notification.type.should == notification_details['type']
+ notification.type.should == notification_details['type']
end
it "gets the date" do
- @notification.date.should == notification_details['date']
+ notification.date.should == notification_details['date']
end
it "gets the data" do
- @notification.data.should == notification_details['data']
+ notification.data.should == notification_details['data']
end
it "gets the member creator id" do
- @notification.member_creator_id.should == notification_details['idMemberCreator']
+ notification.member_creator_id.should == notification_details['idMemberCreator']
end
end
end
View
25 spec/organization_spec.rb
@@ -5,22 +5,33 @@ module Trello
describe Organization do
include Helpers
+ let(:organization) { client.find(:organization, "4ee7e59ae582acdec8000291") }
+ let(:client) { Client.new }
+
before(:each) do
- Trello.client.stub(:get).with("/organizations/4ee7e59ae582acdec8000291").
+ client.stub(:get).with("/organizations/4ee7e59ae582acdec8000291").
and_return organization_payload
+ end
+
+ context "finding" do
+ let(:client) { Trello.client }
+
+ it "delegates to Trello.client#find" do
+ client.should_receive(:find).with(:organization, '4ee7e59ae582acdec8000291')
+ Organization.find('4ee7e59ae582acdec8000291')
+ end
- @organization = Organization.find('4ee7e59ae582acdec8000291')
+ it "is equivalent to client#find" do
+ Organization.find('4ee7e59ae582acdec8000291').should eq(organization)
+ end
end
context "actions" do
it "retrieves actions" do
- Trello.client.stub(:get).with("/organizations/4ee7e59ae582acdec8000291/actions", { :filter => :all }).and_return actions_payload
- @organization.actions.count.should be > 0
+ client.stub(:get).with("/organizations/4ee7e59ae582acdec8000291/actions", { :filter => :all }).and_return actions_payload
+ organization.actions.count.should be > 0
end
-
end
-
end
-
end
View
5 spec/spec_helper.rb
@@ -26,6 +26,10 @@
RSpec.configure do |c|
c.filter_run_excluding :broken => true
+
+ c.before :each do
+ Trello.reset!
+ end
end
module Helpers
@@ -67,6 +71,7 @@ def checklists_details
"closed" => false,
"url" => "https://trello.com/blah/blah",
"idBoard" => "abcdef123456789123456789",
+ "idList" => "abcdef123456789123456789",
"idMembers" => ["abcdef123456789123456789"],
"checkItems" => {}
}]
View
29 spec/token_spec.rb
@@ -4,29 +4,46 @@ module Trello
describe Token do
include Helpers
+ let(:token) { client.find(:token, "1234") }
+ let(:client) { Client.new }
+
before(:each) do
- Trello.client.stub(:get).with("/tokens/1234").and_return token_payload
- @token = Token.find("1234")
+ client.stub(:get).with("/tokens/1234").and_return token_payload
+ end
+
+ context "finding" do
+ let(:client) { Trello.client }
+
+ it "delegates to Trello.client#find" do
+ client.should_receive(:find).with(:token, '1234')
+ Token.find('1234')
+ end
+
+ it "is equivalent to client#find" do
+ Token.find('1234').should eq(token)
+ end
end
context "attributes" do
it "has an id" do
- @token.id.should == "4f2c10c7b3eb95a45b294cd5"
+ token.id.should == "4f2c10c7b3eb95a45b294cd5"
end
it "gets its created_at date" do
- @token.created_at.should == Time.iso8601("2012-02-03T16:52:23.661Z")
+ token.created_at.should == Time.iso8601("2012-02-03T16:52:23.661Z")
end
it "has a permission grant" do
- @token.permissions.count.should be 3
+ token.permissions.count.should be 3
end