Class: Strava::Webhooks::Models::Challenge

Inherits:
Hashie::Trash
  • Object
show all
Defined in:
lib/strava/webhooks/models/challenge.rb

Overview

Represents a webhook subscription validation challenge.

When creating a webhook subscription, Strava sends a GET request to your callback URL with challenge parameters. Your application must validate the verify_token and respond with the challenge value.

Examples:

Handle webhook challenge in Rails

def webhook
  if request.get?
    challenge = Strava::Webhooks::Models::Challenge.new(request.query_parameters)
    if challenge.verify_token == ENV['STRAVA_VERIFY_TOKEN']
      render json: challenge.response
    else
      head :forbidden
    end
  end
end

Handle challenge in Sinatra

get '/webhook' do
  challenge = Strava::Webhooks::Models::Challenge.new(params)
  halt 403 unless challenge.verify_token == ENV['STRAVA_VERIFY_TOKEN']
  content_type :json
  challenge.response.to_json
end

See Also:

Instance Method Summary collapse

Instance Method Details

#challengeString

Returns Challenge value that must be echoed back.

Returns:

  • (String)

    Challenge value that must be echoed back



44
# File 'lib/strava/webhooks/models/challenge.rb', line 44

property 'challenge', from: 'hub.challenge'

#modeString

Returns Subscription mode, typically "subscribe".

Returns:

  • (String)

    Subscription mode, typically "subscribe"



38
# File 'lib/strava/webhooks/models/challenge.rb', line 38

property 'mode', from: 'hub.mode'

#responseHash

Returns the response hash that should be sent back to Strava.

Returns:

  • (Hash)

    Response containing the challenge value



51
52
53
# File 'lib/strava/webhooks/models/challenge.rb', line 51

def response
  { 'hub.challenge' => challenge }
end

#verify_tokenString

Returns Verification token to validate the request.

Returns:

  • (String)

    Verification token to validate the request



41
# File 'lib/strava/webhooks/models/challenge.rb', line 41

property 'verify_token', from: 'hub.verify_token'