class Trello::Client

Public Class Methods

new(attrs = {}) click to toggle source
# File lib/trello/client.rb, line 12
def initialize(attrs = {})
  self.configuration.attributes = attrs
end

Public Instance Methods

auth_policy() click to toggle source
# File lib/trello/client.rb, line 80
def auth_policy
  @auth_policy ||= auth_policy_class.new(credentials)
end
configuration() click to toggle source
# File lib/trello/client.rb, line 76
def configuration
  @configuration ||= Configuration.new
end
configure() { |configuration| ... } click to toggle source
# File lib/trello/client.rb, line 72
def configure
  yield configuration if block_given?
end
create(path, options) click to toggle source

Creates resource with given options (attributes)

Examples:

client.create(:member, options)
client.create(:board, options)
# File lib/trello/client.rb, line 65
def create(path, options)
  trello_class = class_from_path(path)
  trello_class.save options do |data|
    data.client = self
  end
end
delete(path) click to toggle source
# File lib/trello/client.rb, line 32
def delete(path)
  uri = Addressable::URI.parse("https://api.trello.com/#{API_VERSION}#{path}")
  invoke_verb(:delete, uri)
end
find(path, id, params = {}) click to toggle source

Finds given resource by id

Examples:

client.find(:board, "board1234")
client.find(:member, "user1234")
# File lib/trello/client.rb, line 43
def find(path, id, params = {})
  response = get("/#{path.to_s.pluralize}/#{id}", params)
  trello_class = class_from_path(path)
  trello_class.parse response do |data|
    data.client = self
  end
end
find_many(trello_class, path, params = {}) click to toggle source

Finds given resource by path with params

# File lib/trello/client.rb, line 52
def find_many(trello_class, path, params = {})
  response = get(path, params)
  trello_class.parse_many response do |data|
    data.client = self
  end
end
get(path, params = {}) click to toggle source
# File lib/trello/client.rb, line 16
def get(path, params = {})
  uri = Addressable::URI.parse("https://api.trello.com/#{API_VERSION}#{path}")
  uri.query_values = params unless params.empty?
  invoke_verb(:get, uri)
end
post(path, body = {}) click to toggle source
# File lib/trello/client.rb, line 22
def post(path, body = {})
  uri = Addressable::URI.parse("https://api.trello.com/#{API_VERSION}#{path}")
  invoke_verb(:post, uri, body)
end
put(path, body = {}) click to toggle source
# File lib/trello/client.rb, line 27
def put(path, body = {})
  uri = Addressable::URI.parse("https://api.trello.com/#{API_VERSION}#{path}")
  invoke_verb(:put, uri, body)
end

Private Instance Methods

auth_policy_class() click to toggle source
# File lib/trello/client.rb, line 105
def auth_policy_class
  if configuration.oauth?
    OAuthPolicy
  elsif configuration.basic?
    BasicAuthPolicy
  else
    AuthPolicy
  end
end
class_from_path(path_or_class) click to toggle source
# File lib/trello/client.rb, line 115
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
invoke_verb(name, uri, body = nil) click to toggle source
# File lib/trello/client.rb, line 86
def invoke_verb(name, uri, body = nil)
  request = Request.new name, uri, {}, body
  response = TInternet.execute auth_policy.authorize(request)

  return '' unless response

  if response.code.to_i == 401 && response.body =~ /expired token/
    Trello.logger.error("[401 #{name.to_s.upcase} #{uri}]: Your access token has expired.")
    raise InvalidAccessToken, response.body
  end

  unless [200, 201].include? response.code
    Trello.logger.error("[#{response.code} #{name.to_s.upcase} #{uri}]: #{response.body}")
    raise Error, response.body
  end

  response.body
end