Class: Strava::App

Inherits:
Object
  • Object
show all
Includes:
EasyAppHelper
Defined in:
lib/strava/app.rb

Constant Summary collapse

NAME =
'Strava Dashboard'
DESCRIPTION =
'Fetchs data from strava to report what you have done'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApp

Returns a new instance of App.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/strava/app.rb', line 25

def initialize
  config.config_file_base_name = 'strava'
  config.describes_application app_name: NAME,
                               app_version: Strava::VERSION,
                               app_description: DESCRIPTION

  config.add_command_line_section('Strava options') do |slop|
    slop.on :strava_access_token, 'Strava access token', argument: true, as: String
    slop.on :activity, 'Display this activity type only (Run, Ride, Swim)', argument: true
  end

  if config[:help]
    puts config.command_line_help
    exit 0
  end

  if config[:activity]
    @types = [config[:activity]]
  else
    @types = %w(Run Ride Swim)
  end
  @fields = ["Start date", "Distance", "Elapsed time", "Avg speed"]
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



24
25
26
# File 'lib/strava/app.rb', line 24

def client
  @client
end

#fieldsObject

Returns the value of attribute fields.



24
25
26
# File 'lib/strava/app.rb', line 24

def fields
  @fields
end

#typesObject

Returns the value of attribute types.



24
25
26
# File 'lib/strava/app.rb', line 24

def types
  @types
end

Instance Method Details

#configure_api_clientObject



49
50
51
# File 'lib/strava/app.rb', line 49

def configure_api_client
  @client = Strava::Api::V3::Client.new(access_token: config[:strava_access_token])
end

#runObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/strava/app.rb', line 53

def run
  configure_api_client

  activities = []

  VCR.use_cassette("activities", record: :new_episodes) do
    page = 0
    per_page = 100
    while activities.count == page*per_page
      page +=1
      activities += client.list_athlete_activities(per_page: per_page,page: page)
    end
  end

  for type in types
    rows = []
    month = -1
    activities.select { |activity| activity.type == type }.each do |activity|
      date = Time.parse(activity.start_date_local)
      current_month = date.month
      distance_km = (activity.distance/1000.0).round(3)
      elapsed_time_min = (activity.elapsed_time/60.0).round(2)
      elapsed_time_hour = (activity.elapsed_time/3600.0)
      avg_speed = (distance_km/elapsed_time_hour).round(2)
      if current_month != month && month != -1
        rows << :separator
      end
      month = current_month
      rows << [date , "#{distance_km} km", "#{elapsed_time_min} min", "#{avg_speed} km/h"]
    end

    table = Terminal::Table.new :title => type, :headings => fields, :rows => rows

    if rows.empty?
      puts "No activity found. Go out and start #{type}ing!"
    else
      puts table
    end
    puts "\n"
  end
end