Permalink
Browse files

Added support for pagination, closes #5.

  • Loading branch information...
dblock committed Nov 23, 2018
1 parent d829354 commit 40f2a0fd25d9ed0c19204c56f4308a186f1ee5f9
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-11-23 14:45:08 -0500 using RuboCop version 0.60.0.
# on 2018-11-23 15:20:19 -0500 using RuboCop version 0.60.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
@@ -25,7 +25,7 @@ Metrics/ClassLength:
Metrics/CyclomaticComplexity:
Max: 18

# Offense count: 6
# Offense count: 7
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/MethodLength:
Max: 19
@@ -41,7 +41,7 @@ Naming/MemoizedInstanceVariableName:
Exclude:
- 'lib/strava/logger.rb'

# Offense count: 19
# Offense count: 20
Style/Documentation:
Enabled: false

@@ -51,8 +51,8 @@ Style/Documentation:
Style/FormatStringToken:
EnforcedStyle: unannotated

# Offense count: 25
# Offense count: 30
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 112
Max: 116
@@ -3,6 +3,7 @@
* Added support for converting and displaying measurements of activity distance, elevation, speed and pace - [@dblock](https://github.com/dblock).
* Added `Activity#type_emoji` and `Activity#strava_url` - [@dblock](https://github.com/dblock).
* Added `Athlete#name` and `Athlete#strava_url` - [@dblock](https://github.com/dblock).
* [#5](https://github.com/dblock/strava-ruby-client/issues/5): Added support for pagination - [@dblock](https://github.com/dblock).
* Your contribution here.

### 0.1.0 (2018/11/23)
@@ -6,7 +6,7 @@ Strava Ruby Client

A newer Ruby client for the [Strava API v3](https://developers.strava.com).

Unlike [strava-api-v3](https://github.com/jaredholdcroft/strava-api-v3) provides a first class interface to Strava models and more consistent error handling.
Unlike [strava-api-v3](https://github.com/jaredholdcroft/strava-api-v3) provides complete OAuth refresh token flow support, a richer first class interface to Strava models, natively supports pagination and implements more consistent error handling.

# Table of Contents

@@ -15,6 +15,7 @@ Unlike [strava-api-v3](https://github.com/jaredholdcroft/strava-api-v3) provides
- [API](#api)
- [Athlete](#athlete)
- [Athlete Activities](#athlete-activities)
- [Pagination](#pagination)
- [OAuth](#oauth)
- [Configuration](#configuration)
- [Web Client Options](#web-client-options)
@@ -80,6 +81,16 @@ activity.total_elevation_gain_s # => '475.4ft'

See [Strava::Models::Activity](lib/strava/models/activity.rb) for all available properties.

#### Pagination

Some Strava APIs, including [athlete_activities](#athlete-activities) support pagination when supplying an optional `page` and `per_page` parameter. By default the client retrieves one page of data, which Strava currently defaults to 30 items. You can paginate through more data by supplying a block and an optional `per_page` parameter. The underlying implementation makes page-sized calls and increments the `page` argument.

```ruby
client.athlete_activities(per_page: 30) do |activity|
activity # => Strava::Models::Activity
end
```

### OAuth

Obtain a redirect URL.
@@ -27,4 +27,5 @@
require_relative 'strava/oauth/client'

require_relative 'strava/api/config'
require_relative 'strava/api/cursor'
require_relative 'strava/api/client'
@@ -18,10 +18,8 @@ def athlete
Strava::Models::Athlete.new(get('athlete'))
end

def athlete_activities(options = {})
get('athlete/activities', options).map do |activity|
Strava::Models::Activity.new(activity)
end
def athlete_activities(options = {}, &block)
paginate 'athlete/activities', options, Strava::Models::Activity, &block
end

class << self
@@ -33,6 +31,22 @@ def config
Config
end
end

private

def paginate(path, options, model, &_block)
if block_given?
Cursor.new(self, path, options).each do |page|
page.each do |row|
yield model.new(row)
end
end
else
get(path, options).map do |row|
model.new(row)
end
end
end
end
end
end
@@ -0,0 +1,29 @@
module Strava
module Api
class Cursor
include Enumerable

attr_reader :client
attr_reader :path
attr_reader :params

def initialize(client, path, params = {})
@client = client
@path = path
@params = params
end

def each
next_page = 1
loop do
query = params.merge(page: next_page)
response = client.get(path, query)
yield response
break unless response.any?

next_page += 1
end
end
end
end
end
Oops, something went wrong.

0 comments on commit 40f2a0f

Please sign in to comment.