Module: Strava::Api::Endpoints::Activities

Included in:
Client
Defined in:
lib/strava/api/endpoints/activities.rb

Overview

API endpoints for Strava activities.

Activities represent workouts, rides, runs, and other athletic pursuits recorded on Strava. This module provides methods for creating, retrieving, updating, and listing activities, as well as accessing related data like comments, kudos, laps, and zones.

Instance Method Summary collapse

Instance Method Details

#activity(id_or_options, options = {}) ⇒ Strava::Models::DetailedActivity

Get detailed information about a specific activity.

Returns the given activity that is owned by the authenticated athlete. Includes all activity details such as splits, laps, segment efforts, and photos.

Examples:

Get activity by ID

activity = client.activity(1234567890)
puts activity.name
puts activity.distance_s

Get activity with options hash

activity = client.activity(id: 1234567890, include_all_efforts: true)

Parameters:

  • id_or_options (Integer, Hash)

    Activity ID or options hash with :id key

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

    Additional options

Options Hash (options):

  • :include_all_efforts (Boolean)

    Include all segment efforts

Returns:

See Also:



71
72
73
74
# File 'lib/strava/api/endpoints/activities.rb', line 71

def activity(id_or_options, options = {})
  id, options = parse_args(id_or_options, options)
  Strava::Models::DetailedActivity.new(get("activities/#{id}", options))
end

#activity_comments(id_or_options, options = {}) {|Strava::Models::Comment| ... } ⇒ Array<Strava::Models::Comment>

List comments for an activity.

Returns the comments on the given activity. Supports cursor-based pagination.

Examples:

Get all comments

comments = client.activity_comments(1234567890)

Paginate through comments

client.activity_comments(1234567890, page_size: 50) do |comment|
  puts "#{comment.athlete.username}: #{comment.text}"
end

Parameters:

  • id_or_options (Integer, Hash)

    Activity ID or options hash with :id key

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

    Pagination options

Options Hash (options):

  • :page_size (Integer)

    Number of items per page (default: 30)

  • :after_cursor (String)

    Cursor for pagination

  • :limit (Integer)

    Maximum number of items to return

  • :per_page (Integer) — default: Deprecated

    Use :page_size instead

Yields:

Returns:

See Also:



101
102
103
104
# File 'lib/strava/api/endpoints/activities.rb', line 101

def activity_comments(id_or_options, options = {}, &block)
  id, options = parse_args(id_or_options, options)
  paginate_with_cursor "activities/#{id}/comments", options, Strava::Models::Comment, &block
end

#activity_kudos(id_or_options, options = {}) {|Strava::Models::SummaryAthlete| ... } ⇒ Array<Strava::Models::SummaryAthlete>

List athletes who kudoed an activity.

Returns the athletes who kudoed an activity identified by an identifier.

Examples:

Get all kudoers

kudoers = client.activity_kudos(1234567890)
kudoers.each { |athlete| puts athlete.username }

Parameters:

  • id_or_options (Integer, Hash)

    Activity ID or options hash with :id key

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

    Pagination options

Options Hash (options):

  • :page (Integer)

    Page number

  • :per_page (Integer)

    Number of items per page (default: 30)

Yields:

Returns:

See Also:



153
154
155
156
# File 'lib/strava/api/endpoints/activities.rb', line 153

def activity_kudos(id_or_options, options = {}, &block)
  id, options = parse_args(id_or_options, options)
  paginate "activities/#{id}/kudos", options, Strava::Models::SummaryAthlete, &block
end

#activity_laps(id_or_options, options = {}) ⇒ Array<Strava::Models::Lap>

List laps for an activity.

Returns the laps of an activity identified by an identifier. Laps are split segments either created manually or auto-generated.

Examples:

Get activity laps

laps = client.activity_laps(1234567890)
laps.each { |lap| puts "#{lap.name}: #{lap.distance_s}" }

Parameters:

  • id_or_options (Integer, Hash)

    Activity ID or options hash with :id key

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

    Additional options

Returns:

See Also:



175
176
177
178
179
180
# File 'lib/strava/api/endpoints/activities.rb', line 175

def activity_laps(id_or_options, options = {})
  id, options = parse_args(id_or_options, options)
  get("activities/#{id}/laps", options).map do |row|
    Strava::Models::Lap.new(row)
  end
end

#activity_photos(id_or_options, options = {}) {|Strava::Models::DetailedPhoto| ... } ⇒ Array<Strava::Models::DetailedPhoto>

List photos for an activity.

Returns the photos on the given activity. This is an undocumented Strava API endpoint. By default, retrieves full-size photos (5000px).

Examples:

Get all photos

photos = client.activity_photos(1234567890)
photos.each { |photo| puts photo.urls }

Get specific size

photos = client.activity_photos(1234567890, size: 1920)

Parameters:

  • id_or_options (Integer, Hash)

    Activity ID or options hash with :id key

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

    Pagination options

Options Hash (options):

  • :size (Integer)

    Photo size in pixels (default: 5000 for full size)

  • :page (Integer)

    Page number

  • :per_page (Integer)

    Number of items per page (default: 30)

Yields:

Returns:



128
129
130
131
132
# File 'lib/strava/api/endpoints/activities.rb', line 128

def activity_photos(id_or_options, options = {}, &block)
  id, options = parse_args(id_or_options, options)
  options[:size] = 5000 unless options[:size] # to retrieve full size photos
  paginate "activities/#{id}/photos", options, Strava::Models::DetailedPhoto, &block
end

#activity_zones(id_or_options, options = {}) ⇒ Array<Strava::Models::ActivityZone>

Get zones for an activity.

Returns the zones of a given activity. Summit feature required. Zones include heart rate and power zones with distribution buckets.

Examples:

Get activity zones

zones = client.activity_zones(1234567890)
zones.each do |zone|
  puts "Type: #{zone.type}"
  zone.distribution_buckets.each { |bucket| puts "#{bucket.min}-#{bucket.max}: #{bucket.time}s" }
end

Parameters:

  • id_or_options (Integer, Hash)

    Activity ID or options hash with :id key

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

    Additional options

Returns:

See Also:



241
242
243
244
245
246
# File 'lib/strava/api/endpoints/activities.rb', line 241

def activity_zones(id_or_options, options = {})
  id, options = parse_args(id_or_options, options)
  get("activities/#{id}/zones", options).map do |row|
    Strava::Models::ActivityZone.new(row)
  end
end

#athlete_activities(options = {}) {|Strava::Models::SummaryActivity| ... } ⇒ Array<Strava::Models::SummaryActivity>

List activities for the authenticated athlete.

Returns the currently logged-in athlete's activities. Supports pagination and time-based filtering. Activities are returned in descending order by start date.

Examples:

Get recent activities

activities = client.athlete_activities(per_page: 10)

Get activities with time filter

activities = client.athlete_activities(
  after: Time.now - 7.days,
  per_page: 50
)

Paginate through all activities

client.athlete_activities(per_page: 100) do |activity|
  puts "#{activity.name}: #{activity.distance_s}"
end

Parameters:

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

    Pagination and filtering options

Options Hash (options):

  • :before (Time, Integer)

    Epoch timestamp or Time object for filtering activities before this time

  • :after (Time, Integer)

    Epoch timestamp or Time object for filtering activities after this time

  • :page (Integer)

    Page number

  • :per_page (Integer)

    Number of items per page (default: 30)

  • :limit (Integer)

    Maximum number of items to return

Yields:

Returns:

See Also:



214
215
216
217
218
219
# File 'lib/strava/api/endpoints/activities.rb', line 214

def athlete_activities(options = {}, &block)
  options = options.dup if options.key?(:after) || options.key?(:before)
  options[:after] = options[:after].to_i if options[:after]
  options[:before] = options[:before].to_i if options[:before]
  paginate 'athlete/activities', options, Strava::Models::SummaryActivity, &block
end

#create_activity(options = {}) ⇒ Strava::Models::DetailedActivity

Create a manual activity.

Creates a new manual activity for an athlete. Requires write access.

Examples:

activity = client.create_activity(
  name: 'Morning Run',
  sport_type: 'Run',
  start_date_local: Time.now,
  elapsed_time: 3600,
  distance: 10000,
  description: 'Easy recovery run'
)

Parameters:

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

    Activity attributes

Options Hash (options):

  • :name (String)

    Activity name (required)

  • :sport_type (String)

    Activity type (required, e.g., 'Run', 'Ride', 'Swim')

  • :start_date_local (Time, String)

    Local start date and time (required)

  • :elapsed_time (Integer)

    Activity duration in seconds (required)

  • :description (String)

    Activity description

  • :distance (Float)

    Distance in meters

  • :trainer (Boolean)

    Whether activity was on a trainer

  • :commute (Boolean)

    Whether activity was a commute

Returns:

See Also:



45
46
47
# File 'lib/strava/api/endpoints/activities.rb', line 45

def create_activity(options = {})
  Strava::Models::DetailedActivity.new(post('activities', options))
end

#update_activity(id_or_options, options = {}) ⇒ Strava::Models::DetailedActivity

Update an activity.

Updates the given activity that is owned by the authenticated athlete. Requires write access.

Examples:

Update activity name and description

activity = client.update_activity(
  id: 1234567890,
  name: 'Updated Activity Name',
  description: 'New description'
)

Mark as commute

activity = client.update_activity(1234567890, commute: true)

Parameters:

  • id_or_options (Integer, Hash)

    Activity ID or options hash with :id key

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

    Activity attributes to update

Options Hash (options):

  • :commute (Boolean)

    Whether this activity is a commute

  • :trainer (Boolean)

    Whether this activity was recorded on a training machine

  • :description (String)

    Activity description

  • :name (String)

    Activity name

  • :sport_type (String)

    Activity type (e.g., 'Run', 'Ride')

  • :gear_id (String)

    Equipment ID (specify "none" to clear gear)

Returns:

See Also:



277
278
279
280
# File 'lib/strava/api/endpoints/activities.rb', line 277

def update_activity(id_or_options, options = {})
  id, options = parse_args(id_or_options, options)
  Strava::Models::DetailedActivity.new(put("activities/#{id}", options))
end