Class: Podio::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/podio/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

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

  setup_connections
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



3
4
5
# File 'lib/podio/client.rb', line 3

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



3
4
5
# File 'lib/podio/client.rb', line 3

def api_secret
  @api_secret
end

#api_urlObject (readonly)

Returns the value of attribute api_url.



3
4
5
# File 'lib/podio/client.rb', line 3

def api_url
  @api_url
end

#connectionObject (readonly)

Returns the value of attribute connection.



3
4
5
# File 'lib/podio/client.rb', line 3

def connection
  @connection
end

#current_http_clientObject

Returns the value of attribute current_http_client.



4
5
6
# File 'lib/podio/client.rb', line 4

def current_http_client
  @current_http_client
end

#headersObject

Returns the value of attribute headers.



4
5
6
# File 'lib/podio/client.rb', line 4

def headers
  @headers
end

#oauth_tokenObject

Returns the value of attribute oauth_token.



3
4
5
# File 'lib/podio/client.rb', line 3

def oauth_token
  @oauth_token
end

#stubsObject

Returns the value of attribute stubs.



4
5
6
# File 'lib/podio/client.rb', line 4

def stubs
  @stubs
end

#trusted_connectionObject (readonly)

Returns the value of attribute trusted_connection.



3
4
5
# File 'lib/podio/client.rb', line 3

def trusted_connection
  @trusted_connection
end

Instance Method Details

#authenticate_with_app(app_id, app_token) ⇒ Object

Sign in as an app



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/podio/client.rb', line 68

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) ⇒ Object

sign in as a user using the server side flow



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/podio/client.rb', line 42

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) ⇒ Object

Sign in as a user using credentials



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/podio/client.rb', line 55

def authenticate_with_credentials(username, password)
  response = @oauth_connection.post do |req|
    req.url '/oauth/token'
    req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    req.body = {:grant_type => 'password', :client_id => api_key, :client_secret => api_secret, :username => username, :password => password}
  end

  @oauth_token = OAuthToken.new(response.body)
  configure_oauth
  @oauth_token
end

#authenticate_with_openid(identifier, type) ⇒ Object

Sign in with an OpenID, only available for Podio



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/podio/client.rb', line 106

def authenticate_with_openid(identifier, type)
  response = @oauth_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) ⇒ Object

Sign in with SSO



94
95
96
97
98
99
100
101
102
103
# File 'lib/podio/client.rb', line 94

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) ⇒ Object

Sign in with an transfer token, only available for Podio



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/podio/client.rb', line 81

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 = {}) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/podio/client.rb', line 32

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_headersObject



136
137
138
139
140
141
142
143
# File 'lib/podio/client.rb', line 136

def configured_headers
  headers = @headers.dup
  headers['User-Agent']      = 'Podio Ruby Library'
  headers['authorization']   = "OAuth2 #{oauth_token.access_token}" if oauth_token
  headers['X-Podio-Dry-Run'] = @test_mode.to_s                      if @test_mode

  headers
end

#log(env, &block) ⇒ Object



24
25
26
# File 'lib/podio/client.rb', line 24

def log(env, &block)
  @logger.log(env, &block)
end

#refresh_access_tokenObject



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/podio/client.rb', line 124

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

#resetObject



28
29
30
# File 'lib/podio/client.rb', line 28

def reset
  setup_connections
end