class Podio::Client

Attributes

api_key[R]
api_secret[R]
api_url[R]
connection[R]
current_http_client[RW]
headers[RW]
oauth_token[R]
stubs[RW]
trusted_connection[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/podio/client.rb, line 6
def initialize(options = {})
  @api_url = options[:api_url] || 'https://api.podio.com'
  @api_key = options[:api_key]
  @api_secret = options[:api_secret]
  @logger = options[:logger] || Podio::StdoutLogger.new(options[:debug])
  @oauth_token = options[:oauth_token]
  @headers = options[:custom_headers] || {}
  @adapter = options[:adapter] || Faraday.default_adapter
  @request_options = options[:request_options] || {}

  if options[:enable_stubs]
    @enable_stubs = true
    @stubs = Faraday::Adapter::Test::Stubs.new
  end
  @test_mode   = options[:test_mode]

  setup_connections
end

Public Instance Methods

authenticate_with_activation_code(activation_code) click to toggle source

Sign in with an activation code, only available for Podio

# File lib/podio/client.rb, line 123
def authenticate_with_activation_code(activation_code)
  response = @oauth_connection.post do |req|
    req.url '/oauth/token'
    req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    req.body = {:grant_type => 'activation_code', :client_id => api_key, :client_secret => api_secret, :activation_code => activation_code}
  end

  @oauth_token = OAuthToken.new(response.body)
  configure_oauth
  @oauth_token
end
authenticate_with_app(app_id, app_token) click to toggle source

Sign in as an app

# File lib/podio/client.rb, line 72
def authenticate_with_app(app_id, app_token)
  response = @oauth_connection.post do |req|
    req.url '/oauth/token'
    req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    req.body = {:grant_type => 'app', :client_id => api_key, :client_secret => api_secret, :app_id => app_id, :app_token => app_token}
  end

  @oauth_token = OAuthToken.new(response.body)
  configure_oauth
  @oauth_token
end
authenticate_with_auth_code(authorization_code, redirect_uri) click to toggle source

sign in as a user using the server side flow

# File lib/podio/client.rb, line 43
def authenticate_with_auth_code(authorization_code, redirect_uri)
  response = @oauth_connection.post do |req|
    req.url '/oauth/token'
    req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    req.body = {:grant_type => 'authorization_code', :client_id => api_key, :client_secret => api_secret, :code => authorization_code, :redirect_uri => redirect_uri}
  end

  @oauth_token = OAuthToken.new(response.body)
  configure_oauth
  @oauth_token
end
authenticate_with_credentials(username, password, offering_id=nil) click to toggle source

Sign in as a user using credentials

# File lib/podio/client.rb, line 56
def authenticate_with_credentials(username, password, offering_id=nil)
  body = {:grant_type => 'password', :client_id => api_key, :client_secret => api_secret, :username => username, :password => password}
  body[:offering_id] = offering_id if offering_id.present?

  response = @oauth_connection.post do |req|
    req.url '/oauth/token'
    req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    req.body = body
  end

  @oauth_token = OAuthToken.new(response.body)
  configure_oauth
  @oauth_token
end
authenticate_with_openid(identifier, type) click to toggle source

Sign in with an OpenID, only available for Podio

# File lib/podio/client.rb, line 110
def authenticate_with_openid(identifier, type)
  response = @trusted_connection.post do |req|
    req.url '/oauth/token_by_openid'
    req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    req.body = {:grant_type => type, :client_id => api_key, :client_secret => api_secret, :identifier => identifier}
  end

  @oauth_token = OAuthToken.new(response.body)
  configure_oauth
  @oauth_token
end
authenticate_with_sso(attributes) click to toggle source

Sign in with SSO

# File lib/podio/client.rb, line 98
def authenticate_with_sso(attributes)
  response = @oauth_connection.post do |req|
    req.url '/oauth/token', :grant_type => 'sso', :client_id => api_key, :client_secret => api_secret
    req.body = attributes
  end

  @oauth_token = OAuthToken.new(response.body)
  configure_oauth
  [@oauth_token, response.body['new_user_created']]
end
authenticate_with_transfer_token(transfer_token) click to toggle source

Sign in with an transfer token, only available for Podio

# File lib/podio/client.rb, line 85
def authenticate_with_transfer_token(transfer_token)
  response = @oauth_connection.post do |req|
    req.url '/oauth/token'
    req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    req.body = {:grant_type => 'transfer_token', :client_id => api_key, :client_secret => api_secret, :transfer_token => transfer_token}
  end

  @oauth_token = OAuthToken.new(response.body)
  configure_oauth
  @oauth_token
end
authorize_url(params={}) click to toggle source
# File lib/podio/client.rb, line 33
def authorize_url(params={})
  uri = URI.parse(@api_url)
  uri.host  = uri.host.gsub('api.', '')
  uri.path  = '/oauth/authorize'
  uri.query = Rack::Utils.build_query(params.merge(:client_id => api_key))

  uri.to_s
end
configured_headers() click to toggle source
# File lib/podio/client.rb, line 159
def configured_headers
  headers = @headers.dup
  headers['User-Agent'] = "Podio Ruby Library (#{Podio::VERSION})"
  headers['X-Podio-Dry-Run'] = @test_mode.to_s if @test_mode

  if oauth_token
    # if we have a token, set up Oauth2
    headers['authorization'] = "OAuth2 #{oauth_token.access_token}"
  elsif api_key && api_secret
    # if we have an auth_client, set up public authentication (only works with trusted auth clients)
    headers['authorization'] = Faraday::Request::BasicAuthentication.header(api_key, api_secret)
  end

  headers
end
locale=(new_locale) click to toggle source
# File lib/podio/client.rb, line 141
def locale=(new_locale)
  @connection.headers['Accept-Language'] = new_locale
  @oauth_connection.headers['Accept-Language'] = new_locale
  @trusted_connection.headers['Accept-Language'] = new_locale
end
log(env, &block) click to toggle source
# File lib/podio/client.rb, line 25
def log(env, &block)
  @logger.log(env, &block)
end
oauth_token=(new_oauth_token) click to toggle source

reconfigure the client with a different access token

# File lib/podio/client.rb, line 136
def oauth_token=(new_oauth_token)
  @oauth_token = new_oauth_token.is_a?(Hash) ? OAuthToken.new(new_oauth_token) : new_oauth_token
  configure_oauth
end
refresh_access_token() click to toggle source
# File lib/podio/client.rb, line 147
def refresh_access_token
  response = @oauth_connection.post do |req|
    req.url '/oauth/token'
    req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    req.body = {:grant_type => 'refresh_token', :refresh_token => oauth_token.refresh_token, :client_id => api_key, :client_secret => api_secret}
  end

  @oauth_token = OAuthToken.new(response.body)
  @oauth_token.refreshed = true
  configure_oauth
end
reset() click to toggle source
# File lib/podio/client.rb, line 29
def reset
  setup_connections
end

Private Instance Methods

configure_connection() click to toggle source
# File lib/podio/client.rb, line 183
def configure_connection
  Faraday::Connection.new(:url => api_url, :headers => configured_headers, :request => @request_options) do |builder|
    builder.use Middleware::JsonRequest
    builder.use Faraday::Request::Multipart
    builder.use Faraday::Request::UrlEncoded
    builder.use Middleware::OAuth2, :podio_client => self
    builder.use Middleware::Logger, :podio_client => self

    builder.adapter(*default_adapter)

    # first response middleware defined get's executed last
    builder.use Middleware::ErrorResponse
    builder.use Middleware::JsonResponse
  end
end
configure_oauth() click to toggle source
# File lib/podio/client.rb, line 219
def configure_oauth
  @connection = configure_connection
end
configure_oauth_connection() click to toggle source
# File lib/podio/client.rb, line 203
def configure_oauth_connection
  conn = @connection.dup
  conn.options.update(@request_options)
  conn.headers.delete('authorization')
  conn.headers.delete('X-Podio-Dry-Run') if @test_mode # oauth requests don't really work well in test mode
  conn
end
configure_trusted_connection() click to toggle source
# File lib/podio/client.rb, line 211
def configure_trusted_connection
  conn = @connection.dup
  conn.options.update(@request_options)
  conn.headers.delete('authorization')
  conn.basic_auth(api_key, api_secret)
  conn
end
default_adapter() click to toggle source
# File lib/podio/client.rb, line 199
def default_adapter
  @enable_stubs ? [:test, @stubs] : @adapter
end
setup_connections() click to toggle source
# File lib/podio/client.rb, line 177
def setup_connections
  @connection = configure_connection
  @oauth_connection = configure_oauth_connection
  @trusted_connection = configure_trusted_connection
end