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
23
# 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

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_activation_code(activation_code) ⇒ Object

Sign in with an activation code, only available for Podio



123
124
125
126
127
128
129
130
131
132
133
# 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) ⇒ Object

Sign in as an app



72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Object

sign in as a user using the server side flow



43
44
45
46
47
48
49
50
51
52
53
# 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) ⇒ Object

Sign in as a user using credentials



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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) ⇒ Object

Sign in with an OpenID, only available for Podio



110
111
112
113
114
115
116
117
118
119
120
# 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) ⇒ Object

Sign in with SSO



98
99
100
101
102
103
104
105
106
107
# 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) ⇒ Object

Sign in with an transfer token, only available for Podio



85
86
87
88
89
90
91
92
93
94
95
# 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 = {}) ⇒ Object



33
34
35
36
37
38
39
40
# 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_headersObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# 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) ⇒ Object



141
142
143
144
145
# 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) ⇒ Object



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

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

#refresh_access_tokenObject



147
148
149
150
151
152
153
154
155
156
157
# 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

#resetObject



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

def reset
  setup_connections
end