Skip to content

Commit

Permalink
README
Browse files Browse the repository at this point in the history
  • Loading branch information
rossta committed Jan 20, 2013
1 parent b1b4f09 commit 309a35e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 7 deletions.
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,69 @@ 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 should
avoid using global configuration. Instead, 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
```

Or, if you prefer, you can specify all configuration options when instantiating
a `Trello::Client`:

```ruby
@client = Trello::Client.new(
:consumer_key => "an application's consumer key",
:consumer_secret => "an application's consumer secret",
:oauth_token => "a user's access token",
:oauth_token_secret => "a user's access secret"
)
```

This may be useful if you're using multiple consumer key/secret pairs.

## Special thanks

A special thanks goes out to [Ben Biddington](https://github.com/ben-biddington) who has contributed a significant amount
Expand Down
16 changes: 9 additions & 7 deletions lib/trello/basic_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ def parse(response)
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

def self.register_attributes(*names)
Expand Down Expand Up @@ -73,18 +81,12 @@ 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 = objects_from_response klass, client.get("/#{resource}/#{id}/#{name}", params)
resources = client.find_many(klass, "/#{resource}/#{id}/#{name}", params)
MultiAssociation.new(self, resources).proxy
end
end
end

def objects_from_response(klass, response)
response.json_into(klass).map do |data|
data.tap { |d| d.client = self.client }
end
end

def self.client
Trello.client
end
Expand Down
10 changes: 10 additions & 0 deletions lib/trello/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,23 @@ def delete(path)
invoke_verb(:delete, uri)
end

# Finds given resource by id
def find(path, id)
response = get("/#{path.to_s.pluralize}/#{id}")
class_from_path(path).parse(response) do |data|
data.client = self
end
end

# Finds given resource by path with 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)
def create(path, options)
class_from_path(path).save(options) do |data|
data.client = self
Expand Down

0 comments on commit 309a35e

Please sign in to comment.