Sha256: e36e198be5c618464cd5a4269724109942a805bf9af5b2eaea3ff216cb65f7b1

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

#!/usr/bin/env ruby
# frozen_string_literal: true

require 'dotenv/load'
require 'strava-ruby-client'
require 'webrick'

logger = ::Logger.new($stdout)
logger.level = Logger::INFO

client = Strava::Webhooks::Client.new(
  client_id: ENV.fetch('STRAVA_CLIENT_ID', nil),
  client_secret: ENV.fetch('STRAVA_CLIENT_SECRET', nil),
  logger: logger
)

case ARGV[0]
when 'create'
  callback_url = ARGV[1]
  raise 'Missing callback_url.' unless callback_url

  client.logger.info "Subscribing to #{callback_url} ..."
  subscription = client.create_push_subscription(callback_url: callback_url, verify_token: 'token')
  puts subscription
when 'delete'
  id = ARGV[1]
  raise 'Missing id.' unless id

  client.logger.info "Unsubscribing #{id} ..."
  client.delete_push_subscription(id: id)
  client.logger.info 'Done.'
when 'handle'
  server = WEBrick::HTTPServer.new(Port: 4242)

  trap 'INT' do
    server.shutdown
  end

  server.mount_proc '/' do |req, res|
    case req.request_method
    when 'GET'
      challenge = Strava::Webhooks::Models::Challenge.new(req.query)
      res.content_type = 'application/json'
      res.body = challenge.response.to_json
    when 'POST'
      event = Strava::Webhooks::Models::Event.new(JSON.parse(req.body))
      server.logger.info event
      res.content_type = 'application/json'
      res.body = { ok: true }.to_json
    end
  end
  server.start
else
  client.push_subscriptions.each do |s|
    puts s
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
strava-ruby-client-1.1.0 bin/strava-webhooks
strava-ruby-client-1.0.1 bin/strava-webhooks
strava-ruby-client-1.0.0 bin/strava-webhooks