Sha256: 4f4ccc052d163f554fbc2f6e34253a4de5ddb5a0879fa84dea6912a74109ca2e

Contents?: true

Size: 1.42 KB

Versions: 12

Compression:

Stored size: 1.42 KB

Contents

class HomeController < ApplicationController
  before_action :authorize

  def index
    @target_url = url_for(controller: :webhooks, action: :callback, only_path: false, protocol: 'https')
  end

  def start
    ::Hubspot.configure do |config|
      config.api_key = { 'hapikey' => ENV['HUBSPOT_DEVELOPER_API_KEY'] }
    end

    app_id = ENV['HUBSPOT_APPLICATION_ID']
    Services::Hubspot::Webhooks::PauseActiveSubscriptions.new(app_id: app_id).call

    target_url = url_for(controller: :webhooks, action: :callback, only_path: false, protocol: 'https')
    Services::Hubspot::Webhooks::ConfigureTargetUrl.new(app_id: app_id, target_url: target_url).call

    subscriptions = [
      {"event_type": "contact.propertyChange", "property_name": "firstname"},
      {"event_type": "contact.creation"},
      {"event_type": "contact.deletion"},
    ]

    subscriptions.each do |subscription|
      Services::Hubspot::Webhooks::CreateOrActivateSubscription.new(app_id: app_id, **subscription).call
    end

    redirect_to controller: :events, action: :index
  end

  private

  def authorize
    unless authorized?
      redirect_to login_path and return unless Token.any?

      session['tokens'] = Token.instance.attributes
    end

    session['tokens'] = Services::Hubspot::Authorization::Tokens::Refresh.new(tokens: session['tokens'], request: request).call
    Services::Hubspot::Authorization::Authorize.new(tokens: session['tokens']).call
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
hubspot-api-client-9.0.0 sample-apps/webhooks-contacts-app/app/controllers/home_controller.rb
hubspot-api-client-8.0.1 sample-apps/webhooks-contacts-app/app/controllers/home_controller.rb
hubspot-api-client-8.0.0 sample-apps/webhooks-contacts-app/app/controllers/home_controller.rb
hubspot-api-client-7.3.0 sample-apps/webhooks-contacts-app/app/controllers/home_controller.rb
hubspot-api-client-7.2.0 sample-apps/webhooks-contacts-app/app/controllers/home_controller.rb
hubspot-api-client-7.1.1 sample-apps/webhooks-contacts-app/app/controllers/home_controller.rb
hubspot-api-client-7.1.0 sample-apps/webhooks-contacts-app/app/controllers/home_controller.rb
hubspot-api-client-7.0.0 sample-apps/webhooks-contacts-app/app/controllers/home_controller.rb
hubspot-api-client-6.0.0 sample-apps/webhooks-contacts-app/app/controllers/home_controller.rb
hubspot-api-client-5.0.0 sample-apps/webhooks-contacts-app/app/controllers/home_controller.rb
hubspot-api-client-4.0.0 sample-apps/webhooks-contacts-app/app/controllers/home_controller.rb
hubspot-api-client-3.3.0 sample-apps/webhooks-contacts-app/app/controllers/home_controller.rb