ruby-trello

repository·master·Indexed 20 days ago

https://github.com/jeremytregunna/ruby-trello

A Ruby implementation of the Trello API that allows developers to interact with Trello boards, lists, cards, and members. It supports basic authorization as well as 2-legged and 3-legged OAuth, and provides a flexible client for managing resources and checklist items.

Tokens
6.6K
Snippets
37
Records
43
Agent score
72%

What's inside ruby-trello

  1. Use Trello::Client for multiple users

    master

    If your application needs to make requests on behalf of different users, avoid global Trello.configure. Instead, instantiate a Trello::Client for each user's specific credentials. This allows for threadsafe requests using the specific user's context.

    # Create a client for a specific user
    @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"
    )
    
    # Use the client to perform find operations
    @client_bob.find(:members, "bobtester")
  2. Access specific Trello items by ID

    master

    Since the Trello API does not support finding items by name, you must use the unique ID. A common pattern is to use pp (pretty print) to inspect collections and find the desired ID.

    # Find a board by ID
    Trello::Board.find( board_id ).lists
    
    # Find a list by ID and get its cards
    Trello::List.find( list_id ).cards
    
    # Find a card by ID and get its checklists
    Trello::Card.find( card_id ).checklists
  3. Configure Basic Authorization

    master

    To use basic authorization, you need a Trello developer public key and a member token.

    1. Obtain your public key and member token via the Trello website or by running the following in an IRB session:
    require 'trello'
    Trello.open_public_key_url                         # Copy this key
    Trello.open_authorization_url key: 'yourpublickey' # Copy this token
    1. Configure the library in your application:
    require 'trello'
    
    Trello.configure do |config|
      config.developer_public_key = TRELLO_DEVELOPER_PUBLIC_KEY
      config.member_token = TRELLO_MEMBER_TOKEN
    end
  4. Configure 3-legged OAuth authorization

    master

    Use 3-legged OAuth by providing the consumer key, consumer secret, a return URL, and a callback lambda to handle the request token.

    Trello.configure do |config|
      config.consumer_key    = TRELLO_CONSUMER_KEY
      config.consumer_secret = TRELLO_CONSUMER_SECRET
      config.return_url      = "http://your.site.com/path/to/receive/post"
      config.callback        = lambda { |request_token| DB.save(request_token.key, request_token.secret) }
    end
  5. Configure the HTTP Client

    master

    The library requires either rest-client or faraday to be present. If both are installed, faraday is the default. You can explicitly set the client via configuration.

    Trello.configure do |config|
      config.http_client = 'faraday'
      # OR
      config.http_client = 'rest-client'
    end
  6. Configure 2-legged OAuth authorization

    master

    Use 2-legged OAuth by providing the consumer key, consumer secret, and the OAuth token/secret pair.

    Trello.configure do |config|
      config.consumer_key = TRELLO_CONSUMER_KEY
      config.consumer_secret = TRELLO_CONSUMER_SECRET
      config.oauth_token = TRELLO_OAUTH_TOKEN
      config.oauth_token_secret = TRELLO_OAUTH_TOKEN_SECRET
    end
  7. Configure 2-legged OAuth authentication

    master

    For 2-legged OAuth (using a developer key and a member token directly), configure the OAuthPolicy with your consumer credentials and the specific member token. This is used when you already have a Trello API token and want to make authorized requests without a full OAuth flow.

    OAuthCredential = Struct.new "OAuthCredential", :key, :secret
    
    # Configure consumer credentials and the member token
    OAuthPolicy.consumer_credential = OAuthCredential.new "public_key", "secret"
    OAuthPolicy.token               = OAuthCredential.new "token_key", nil
  8. Configure 3-legged OAuth authentication

    master

    For 3-legged OAuth (where a user authorizes your application), you must configure the OAuthPolicy with your consumer credentials, a return_url to receive the callback, and a callback Proc. The callback is responsible for saving the request_token and redirecting the user to the Trello authorization URL.

    After the user authorizes, you must recreate the request token using the saved key and secret, call the #get_access_token method on the consumer, and store the resulting access token in OAuthPolicy.token as an OAuthCredential object.

    # Setup for 3-legged OAuth
    OAuthPolicy.consumer_credential = OAuthCredential.new "public_key", "secret"
    OAuthPolicy.return_url          = "http://your.site.com/path/to/receive/post"
    OAuthPolicy.callback            = Proc.new do |request_token|
      # Save the request token credentials to your database
      DB.save(request_token.key, request_token.secret)
      # Redirect the user to Trello to authorize
      redirect_to request_token.authorize_url
    end
  9. Retrieve Trello Member and Board information

    master

    You can find members by their username and traverse their associations (boards, lists, cards, etc.).

    # Find a member
    bob = Trello::Member.find("bobtester")
    
    # Access member attributes
    puts bob.full_name
    puts bob.bio
    
    # Access member's boards
    bob.boards
    
    # Access lists within the first board
    bob.boards.first.lists
    bob = Trello::Member.find("bobtester")
    puts bob.full_name
    puts bob.bio
    bob.boards
    bob.boards.first.lists