Skip to main content

Installation

To install the ActivitySmith Ruby SDK, you can use RubyGems:
Ruby
gem install activitysmith

Usage

  1. Create an API key
  2. Set the API key as an environment variable named ACTIVITYSMITH_API_KEY or pass it directly to ActivitySmith::Client.
Here’s an example of how to use the SDK with error handling:
Ruby
require "activitysmith"

api_key = ENV["ACTIVITYSMITH_API_KEY"] || "YOUR-API-KEY"
activitysmith = ActivitySmith::Client.new(api_key: api_key)

Send a Push Notification

Use activitysmith.notifications.send with a push payload. title is required; message and subtitle are optional. Push Notification
Ruby
response = activitysmith.notifications.send(
  {
    title: "New subscription 💸",
    message: "Customer upgraded to Pro plan"
  }
)

puts response.success
puts response.devices_notified

Start a Live Activity

Start a Live Activity with activitysmith.live_activities.start. For segmented_progress, send number_of_steps, current_step, and type. For progress, send type: "progress" with percentage or value plus upper_limit. Start Live Activity
Ruby
start = activitysmith.live_activities.start(
  {
    content_state: {
      title: "Nightly database backup",
      subtitle: "create snapshot",
      number_of_steps: 3,
      current_step: 1,
      type: "segmented_progress",
      color: "yellow"
    }
  }
)

activity_id = start.activity_id
Progress type
Ruby
start = activitysmith.live_activities.start(
  {
    content_state: {
      title: "EV Charging",
      subtitle: "Added 30 mi range",
      percentage: 15,
      type: "progress",
      color: "lime"
    }
  }
)

activity_id = start.activity_id

Update a Live Activity

Update a Live Activity with activitysmith.live_activities.update using the activity_id. For progress updates, send percentage or value plus upper_limit instead of current_step. Update Live Activity
Ruby
update = activitysmith.live_activities.update(
  {
    activity_id: activity_id,
    content_state: {
      title: "Nightly database backup",
      subtitle: "upload archive",
      current_step: 2
    }
  }
)

puts update.devices_notified
Progress type
Ruby
update = activitysmith.live_activities.update(
  {
    activity_id: activity_id,
    content_state: {
      title: "EV Charging",
      subtitle: "Added 120 mi range",
      percentage: 60
    }
  }
)

puts update.devices_notified

End a Live Activity

End a Live Activity with activitysmith.live_activities.end. You can optionally set auto_dismiss_minutes in the content_state. End Live Activity
Ruby
finish = activitysmith.live_activities.end(
  {
    activity_id: activity_id,
    content_state: {
      title: "Nightly database backup",
      subtitle: "verify restore",
      current_step: 3,
      auto_dismiss_minutes: 2
    }
  }
)

puts finish.success
Progress type
Ruby
finish = activitysmith.live_activities.end(
  {
    activity_id: activity_id,
    content_state: {
      title: "EV Charging",
      subtitle: "Added 200 mi range",
      percentage: 100,
      auto_dismiss_minutes: 2
    }
  }
)

puts finish.success

Channels

You can target specific channels when sending a push or starting a Live Activity.
Ruby
activitysmith.notifications.send(
  {
    title: "New subscription 💸",
    message: "Customer upgraded to Pro plan",
    channels: ["ios-builds", "engineering"]
  }
)

activitysmith.live_activities.start(
  {
    channels: ["ios-builds"],
    content_state: {
      title: "Nightly database backup",
      number_of_steps: 3,
      current_step: 1,
      type: "segmented_progress"
    }
  }
)

Error Handling

Handle API errors with begin/rescue around SDK calls:
Ruby
begin
  activitysmith.notifications.send(
    { title: "Hello" }
  )
rescue OpenapiClient::ApiError => err
  puts "Request failed: #{err.code} #{err.message}"
end

Additional Resources