Class: Strava::Webhooks::Client

Inherits:
Strava::Web::Client
  • Object
show all
Defined in:
lib/strava/webhooks/client.rb

Overview

Webhooks client for Strava push subscriptions.

Strava provides webhooks for receiving real-time updates about athlete activities and other events. This client manages webhook subscriptions (create, list, delete).

Before creating a subscription, you must implement an HTTP endpoint that:

  1. Handles GET requests for subscription validation (Challenge)
  2. Handles POST requests for webhook events (Event)

Examples:

Create and manage a webhook subscription

client = Strava::Webhooks::Client.new(
  client_id: "your_client_id",
  client_secret: "your_client_secret"
)

# Create subscription
subscription = client.create_push_subscription(
  callback_url: 'https://myapp.com/strava/webhook',
  verify_token: 'my_verify_token'
)

# List subscriptions
subscriptions = client.push_subscriptions

# Delete subscription
client.delete_push_subscription(subscription.id)

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Initialize a new Webhooks client.

Parameters:

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

    Configuration options

Options Hash (options):

  • :client_id (String)

    Strava application client ID (required)

  • :client_secret (String)

    Strava application client secret (required)

  • :endpoint (String)

    API endpoint URL (defaults to https://www.strava.com/api/v3)



48
49
50
51
52
53
# File 'lib/strava/webhooks/client.rb', line 48

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

Class Method Details

.configModule

Returns the current Webhooks client configuration.

Returns:

  • (Module)

    The Config module



155
156
157
# File 'lib/strava/webhooks/client.rb', line 155

def config
  Config
end

.configure {|Config| ... } ⇒ Module

Configure the Webhooks client with a block.

Examples:

Strava::Webhooks::Client.configure do |config|
  config.client_id = ENV['STRAVA_CLIENT_ID']
  config.client_secret = ENV['STRAVA_CLIENT_SECRET']
end

Yields:

  • (Config)

    Yields the configuration module for setup

Returns:

  • (Module)

    The Config module



146
147
148
# File 'lib/strava/webhooks/client.rb', line 146

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

Instance Method Details

#create_push_subscription(options = {}) ⇒ Strava::Webhooks::Models::Subscription

Create a new push subscription.

Creates a webhook subscription for receiving real-time activity updates. Your callback URL must be publicly accessible and handle both GET (for validation) and POST (for events) requests.

Strava will send a GET request to validate your callback URL before creating the subscription. Your endpoint must respond with the challenge token.

Examples:

Create subscription

subscription = client.create_push_subscription(
  callback_url: 'https://myapp.com/strava/webhook',
  verify_token: 'my_secret_token_123'
)
puts "Subscription ID: #{subscription.id}"

Parameters:

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

    Subscription parameters

Options Hash (options):

  • :callback_url (String)

    Public HTTPS URL where webhook events will be sent (required)

  • :verify_token (String)

    Token for validating webhook challenges (required)

Returns:

See Also:



129
130
131
# File 'lib/strava/webhooks/client.rb', line 129

def create_push_subscription(options = {})
  Strava::Webhooks::Models::Subscription.new(post('push_subscriptions', options))
end

#delete_push_subscription(id_or_options, options = {}) ⇒ nil

Delete a push subscription.

Removes an existing webhook subscription by ID. After deletion, your application will no longer receive webhook events.

Examples:

Delete subscription

client.delete_push_subscription(12345)

Parameters:

  • id_or_options (Integer, Hash)

    Subscription ID or options hash with :id key

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

    Additional options

Returns:

  • (nil)

See Also:



97
98
99
100
101
# File 'lib/strava/webhooks/client.rb', line 97

def delete_push_subscription(id_or_options, options = {})
  id, options = parse_args(id_or_options, options)
  delete("push_subscriptions/#{id}", options)
  nil
end

#push_subscriptions(options = {}) ⇒ Array<Strava::Webhooks::Models::Subscription>

List existing push subscriptions.

Returns all active webhook subscriptions for your application. Typically, Strava only allows one subscription per application.

Examples:

List subscriptions

subscriptions = client.push_subscriptions
subscriptions.each { |sub| puts "ID: #{sub.id}, URL: #{sub.callback_url}" }

Parameters:

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

    Additional options

Returns:

See Also:



75
76
77
78
79
# File 'lib/strava/webhooks/client.rb', line 75

def push_subscriptions(options = {})
  get('push_subscriptions', options).map do |row|
    Strava::Webhooks::Models::Subscription.new(row)
  end
end

#request(method, path, options) ⇒ Object



55
56
57
# File 'lib/strava/webhooks/client.rb', line 55

def request(method, path, options)
  super(method, path, { client_id: client_id, client_secret: client_secret }.merge(options))
end