Class: Strava::OAuth::Client

Inherits:
Web::Client show all
Defined in:
lib/strava/oauth/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Web::Client

#endpoint, #parse_args

Methods included from Web::Request

#delete, #get, #post, #put

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
11
# File 'lib/strava/oauth/client.rb', line 6

def initialize(options = {})
  Strava::OAuth::Config::ATTRIBUTES.each do |key|
    send("#{key}=", options[key] || Strava::OAuth.config.send(key))
  end
  super
end

Class Method Details

.configObject



64
65
66
# File 'lib/strava/oauth/client.rb', line 64

def config
  Config
end

.configureObject



60
61
62
# File 'lib/strava/oauth/client.rb', line 60

def configure
  block_given? ? yield(Config) : Config
end

Instance Method Details

#authorize_url(options = {}) ⇒ String

Obtain the request access URL.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :redirect_uri (Object)

    URL to which the user will be redirected after authentication.

  • :response_type (Object)

    Must be code.

  • :approval_prompt (Object)

    Prompt behavior, force or auto.

  • :scope (Object)

    Requested scopes, as a comma delimited string.

  • :state (Object)

    Returned in the redirect URI.

Returns:

  • (String)

    URL to redirect the user to.

See Also:



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/strava/oauth/client.rb', line 28

def authorize_url(options = {})
  query = options.merge(
    client_id: client_id || raise(ArgumentError, 'Missing Strava client id.'),
    response_type: options[:response_type] || 'code',
    redirect_uri: options[:redirect_uri] || 'http://localhost',
    approval_prompt: options[:approval_prompt] || 'auto',
    scope: options[:scope] || 'read'
  )

  [endpoint, "authorize?#{query.to_query}"].join('/')
end

#oauth_token(options = {}) ⇒ Hash

Complete the authentication process.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :code (Object)

    The code parameter obtained in the redirect.

  • :grant_type (Object)

    The grant type for the request.

Returns:

  • (Hash)

    Token information.

See Also:



49
50
51
52
53
54
55
56
57
# File 'lib/strava/oauth/client.rb', line 49

def oauth_token(options = {})
  query = options.merge(
    client_id: client_id || raise(ArgumentError, 'Missing Strava client id.'),
    client_secret: client_secret || raise(ArgumentError, 'Missing Strava client secret.'),
    grant_type: options[:grant_type] || 'authorization_code'
  )

  Strava::Models::Token.new(post('token', query))
end