class MiniStrava::Client

Constants

BaseUrl

Public Class Methods

new(access_token) click to toggle source
# File lib/mini_strava/client.rb, line 13
def initialize access_token
  raise BlankAccessTokenError.new "access_token can't be blank" if access_token.to_s.strip == ''
  @access_token = access_token
end

Public Instance Methods

activities(options={}) click to toggle source

valid options: before, after, page, per_page

# File lib/mini_strava/client.rb, line 24
def activities options={}
  response = get '/activities', options
  parse_response response, Models::Activity
end
activity(id, options={}) click to toggle source
# File lib/mini_strava/client.rb, line 29
def activity id, options={}
  response = get "/activities/#{id}"
  parse_response response, Models::Activity
end
athlete() click to toggle source
# File lib/mini_strava/client.rb, line 18
def athlete
  response = get '/athlete'
  parse_response response, Models::Athlete
end
segment(id) click to toggle source
# File lib/mini_strava/client.rb, line 34
def segment id
  response = get "/segments/#{id}"
  parse_response response, Models::Segment
end

Private Instance Methods

build_uri(path, params={}) click to toggle source
# File lib/mini_strava/client.rb, line 71
def build_uri path, params={}
  params = params.merge access_token: @access_token
  uri = URI(File.join(BaseUrl, path))
  uri.query = URI.encode_www_form params
  uri
end
get(path, params={}) click to toggle source
# File lib/mini_strava/client.rb, line 41
def get path, params={}
  uri = build_uri path, params
  request = Net::HTTP::Get.new uri
  perform_request request, uri
end
parse_response(response, type) click to toggle source
# File lib/mini_strava/client.rb, line 62
def parse_response response, type
  attributes = JSON.parse response.body
  if attributes.is_a? Array
    attributes.collect { |a| type.build a }
  else
    type.build attributes
  end
end
perform_request(request, uri) click to toggle source
# File lib/mini_strava/client.rb, line 47
def perform_request request, uri
  response = Net::HTTP.start uri.hostname, uri.port, use_ssl: true do |http|
    http.request request
  end

  case response.code
  when '401'
    raise AuthorizationError.new
  when '404'
    raise ResourceNotFound.new
  else
    response
  end
end