Class: Strava::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/strava/client.rb

Constant Summary collapse

BASE_URL =

can be overridden for individual requests

'https://www.strava.com/api/v3/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Client

Returns a new instance of Client.



9
10
11
# File 'lib/strava/client.rb', line 9

def initialize(token)
  @token = token
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



4
5
6
# File 'lib/strava/client.rb', line 4

def token
  @token
end

#usageUsage (readonly)

Returns Information on API quota usage.

Returns:

  • (Usage)

    Information on API quota usage



6
7
8
# File 'lib/strava/client.rb', line 6

def usage
  @usage
end

Instance Method Details

#check_for_error(response) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/strava/client.rb', line 46

def check_for_error(response)
  @usage = Usage.new(response.headers['X-Ratelimit-Limit'], response.headers['X-Ratelimit-Usage'])
  case response.code
  when 401, 403
    raise Strava::AccessError.new(response.to_h)
  end
end

#delete(path, **params) ⇒ Object



25
26
27
# File 'lib/strava/client.rb', line 25

def delete(path, **params)
  make_request(:delete, path, **params)
end

#get(path, **params) ⇒ Object



13
14
15
# File 'lib/strava/client.rb', line 13

def get(path, **params)
  make_request(:get, path, **params)
end

#handle_params(params) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/strava/client.rb', line 37

def handle_params(params)
  if @token
    params.merge!(access_token: @token)
  else
    params.merge!(client_id: Strava.client_id, client_secret: Strava.secret)
  end
  params.reverse_each { |k, v| params.delete(k) if v.nil? }
end

#list_races(year = Time.now.year) ⇒ Object

non athlete calls



56
57
58
# File 'lib/strava/client.rb', line 56

def list_races(year = Time.now.year)
  RunningRace.list_races(self, year)
end

#make_request(verb, path, **params) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/strava/client.rb', line 29

def make_request(verb, path, **params)
  puts (params[:host] || BASE_URL) + path
  handle_params(params)
  res = HTTParty.send(verb, (params.delete(:host) || BASE_URL) + path, query: params)
  check_for_error(res)
  res
end

#post(path, **params) ⇒ Object



17
18
19
# File 'lib/strava/client.rb', line 17

def post(path, **params)
  make_request(:post, path, **params)
end

#put(path, **params) ⇒ Object



21
22
23
# File 'lib/strava/client.rb', line 21

def put(path, **params)
  make_request(:put, path, **params)
end

#segment_explorer(bounds = '37.821362,-122.505373,37.842038,-122.465977') ⇒ Object



60
61
62
# File 'lib/strava/client.rb', line 60

def segment_explorer(bounds = '37.821362,-122.505373,37.842038,-122.465977')
  Segment.explorer(self, bounds)
end