strava-ruby-cli 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 795187f6ed3b261eda5e43a77d27c299772de0d877af93c523dfe5b302c7c476
4
+ data.tar.gz: 54880408a8294f2eee97ab2698452c79804b3da10cf6ad09f7b8b5905e3299ca
5
+ SHA512:
6
+ metadata.gz: cdfcea1a6d0d98f0e9e4bbdc34964d33fab3beb9a5df5109180526b483a164e4beabca11d52e7f3305d6633d845460bf43320a7b870462e276ef745bb8c89df7
7
+ data.tar.gz: 7df4d3579bd3d1b8c7500bbd8b001077ea39de044bf027cb032ccfb4a339bd1852a6a1f13bfadff6e24e81a66fdd9bf88aab44a0d7b98cd0d3e5b2d679476a1c
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ### 0.1.0 (2019/1/11)
2
+
3
+ * Initial public release - [@dblock](https://github.com/dblock).
data/LICENSE.md ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Daniel Doubrovkine and Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ Strava Command-Line Client
2
+ ==========================
3
+
4
+ [![Gem Version](https://badge.fury.io/rb/strava-ruby-cli.svg)](https://badge.fury.io/rb/strava-ruby-cli)
5
+ [![Build Status](https://travis-ci.org/dblock/strava-ruby-cli.svg?branch=master)](https://travis-ci.org/dblock/strava-ruby-cli)
6
+
7
+ A command-line client for Strava [Strava API v3](https://developers.strava.com).
8
+
9
+ ## Table of Contents
10
+
11
+ - [Installation](#installation)
12
+ - [Usage](#usage)
13
+ - [Authentication](#authentication)
14
+ - [Run Commands](#run-commands)
15
+ - [Strava Console](#strava-console)
16
+ - [Contributing](#contributing)
17
+ - [Copyright and License](#copyright-and-license)
18
+
19
+ ## Installation
20
+
21
+ ```
22
+ gem install 'strava-ruby-cli'
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### Authentication
28
+
29
+ Strava commands require a Strava _Client ID_ and _Client Secret_ from [Your API Application](https://www.strava.com/settings/api). The command-line tool with prompt you for these or you can supply them with `--client_id` and `--client_secret` options. You can also adjust access scope with `--scope`.
30
+
31
+ The client will fetch an access token. This will open a new browser window, navigate to Strava, request the appropriate permissions, then handle OAuth in a local redirect. The token type, refresh token, access token and token expiration will be displayed in the browser and subsequently used.
32
+
33
+ You can note the access token and supply it via `--access_token` to avoid being prompted in the future.
34
+
35
+ ### Run Commands
36
+
37
+ Use `strava run` to execute commands against the Strava API. Everything in [strava-ruby-client](https://github.com/dblock/strava-ruby-client) is supported.
38
+
39
+ ```bash
40
+ $ strava run athlete.username
41
+
42
+ dblockdotorg
43
+ ```
44
+
45
+ Because this is Ruby, you can could fetch all currently logged-in athlete attributes in JSON.
46
+
47
+ ```bash
48
+ $ strava run athlete.to_json
49
+
50
+ {"id":26462176,"username":"dblockdotorg", ...}
51
+ ```
52
+
53
+ To retrieve activities you need the `activity:read` scope.
54
+
55
+ ```bash
56
+ $ strava --scope=activity:read run athlete_activities.first.name
57
+
58
+ TCS NYC Marathon 2018
59
+ ```
60
+
61
+ ### Strava Console
62
+
63
+ Use `strava console` to explore the Strava API interactively.
64
+
65
+ ```bash
66
+ $ strava console
67
+
68
+ Strava> athlete.name
69
+
70
+ "Daniel Doubrovkine"
71
+ ```
72
+
73
+ ## Contributing
74
+
75
+ See [CONTRIBUTING](CONTRIBUTING.md).
76
+
77
+ ## Copyright and License
78
+
79
+ Copyright (c) 2019, [Daniel Doubrovkine](https://twitter.com/dblockdotorg) and [Contributors](CHANGELOG.md).
80
+
81
+ This project is licensed under the [MIT License](LICENSE.md).
data/bin/strava ADDED
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env ruby
2
+ require 'gli'
3
+ require 'ripl'
4
+
5
+ require_relative '../lib/strava-ruby-cli'
6
+
7
+ class App
8
+ extend GLI::App
9
+
10
+ program_desc "Command-line client for Strava #{Strava::VERSION}."
11
+ version Strava::Cli::VERSION
12
+
13
+ switch %i[v verbose], desc: 'Produce verbose output', default_value: false
14
+ flag %i[i client_id], desc: 'Strava client ID', default_value: ENV['STRAVA_CLIENT_ID']
15
+ flag %i[s client_secret], desc: 'Strava client secret', default_value: ENV['STRAVA_CLIENT_SECRET']
16
+ flag %i[t access_token], desc: 'Strava access token', default_value: ENV['STRAVA_ACCESS_TOKEN']
17
+ flag %i[p scope], desc: 'Strava access scope', default_value: ENV['STRAVA_ACCESS_SCOPE'] || 'read_all'
18
+
19
+ arguments :strict
20
+ subcommand_option_handling :normal
21
+
22
+ default_command :help
23
+
24
+ pre do |global_options, _command, options, _args|
25
+ options = global_options.dup
26
+ $auth = Strava::Cli::Auth.new(options)
27
+ end
28
+
29
+ desc 'Opens a Strava console'
30
+ command :console do |c|
31
+ c.action do |_global_options, _options, _args|
32
+ Strava::Cli::Console.new($auth.access_token).start!
33
+ end
34
+ end
35
+
36
+ desc 'Runs a command.'
37
+ arg_name 'command', :multiple
38
+ command :run do |c|
39
+ c.action do |_global_options, _options, args|
40
+ Strava::Cli::Console.new($auth.access_token).run(args)
41
+ end
42
+ end
43
+ end
44
+
45
+ exit App.run(ARGV)
data/lib/cli/auth.rb ADDED
@@ -0,0 +1,120 @@
1
+ module Strava
2
+ module Cli
3
+ class Auth
4
+ def initialize(options = {})
5
+ @access_token = options[:access_token]
6
+ @client_id = options[:client_id]
7
+ @client_secret = options[:client_secret]
8
+ @scope = options[:scope]
9
+ end
10
+
11
+ def access_token
12
+ @access_token ||= get_access_token
13
+ end
14
+
15
+ def client_id
16
+ @client_id ||= get_client_id
17
+ end
18
+
19
+ def client_secret
20
+ @client_secret ||= get_client_secret
21
+ end
22
+
23
+ private
24
+
25
+ def get_access_token
26
+ require 'webrick'
27
+
28
+ server = WEBrick::HTTPServer.new(Port: 4242)
29
+
30
+ trap 'INT' do
31
+ server.shutdown
32
+ end
33
+
34
+ server.mount_proc '/' do |req, res|
35
+ begin
36
+ code = req.query['code']
37
+ response = strava_client.oauth_token(code: code)
38
+
39
+ res.body = %(
40
+ <html>
41
+ <body>
42
+ <h3>You may close this window and return to your shell.</h3>
43
+ <ul>
44
+ <li>token_type: #{response.token_type}</li>
45
+ <li>refresh_token: #{response.refresh_token}</li>
46
+ <li>access_token: #{response.access_token}</li>
47
+ <li>expires_at: #{response.expires_at}</li>
48
+ </ul>
49
+ <body>
50
+ </html>
51
+ )
52
+
53
+ @access_token = response.access_token if response
54
+ ensure
55
+ server.shutdown
56
+ end
57
+ end
58
+
59
+ redirect_url = strava_client.authorize_url(
60
+ redirect_uri: 'http://localhost:4242/',
61
+ response_type: 'code',
62
+ scope: @scope
63
+ )
64
+
65
+ system 'open', redirect_url
66
+
67
+ server.start
68
+
69
+ @access_token
70
+ end
71
+
72
+ def strava_client
73
+ @strava_client ||= Strava::OAuth::Client.new(
74
+ client_id: client_id,
75
+ client_secret: client_secret
76
+ )
77
+ end
78
+
79
+ def get_client_id
80
+ print 'Enter Strava client ID: '
81
+ get_insecure
82
+ end
83
+
84
+ def get_client_secret
85
+ print 'Enter Strava client secret: '
86
+ get_secure
87
+ end
88
+
89
+ private
90
+
91
+ def get_insecure
92
+ input = $stdin.gets
93
+ input.chomp! if input
94
+ input
95
+ rescue Interrupt => e
96
+ raise e, 'ctrl + c'
97
+ end
98
+
99
+ def get_secure
100
+ current_tty = `stty -g`
101
+ system 'stty raw -echo -icanon isig' if $CHILD_STATUS.success?
102
+ input = ''
103
+ while (char = $stdin.getbyte) && !((char == 13) || (char == 10))
104
+ if (char == 127) || (char == 8)
105
+ input[-1, 1] = '' unless input.empty?
106
+ else
107
+ $stdout.write '*'
108
+ input << char.chr
109
+ end
110
+ end
111
+ print "\r\n"
112
+ input
113
+ rescue Interrupt => e
114
+ raise e, 'ctrl + c'
115
+ ensure
116
+ system "stty #{current_tty}" unless current_tty.empty?
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,24 @@
1
+ module Strava
2
+ module Cli
3
+ class Console < SimpleDelegator
4
+ attr_reader :client
5
+
6
+ def initialize(access_token)
7
+ @client = Strava::Api::Client.new(access_token: access_token)
8
+ super @client
9
+ end
10
+
11
+ def run(args)
12
+ args.map do |arg|
13
+ rc = eval(arg)
14
+ puts rc
15
+ rc
16
+ end
17
+ end
18
+
19
+ def start!
20
+ Ripl.start(binding: binding, prompt: 'Strava> ')
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ module Strava
2
+ module Cli
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ require 'open3'
2
+ require 'English'
3
+
4
+ require 'strava-ruby-client'
5
+
6
+ require_relative 'cli/version'
7
+ require_relative 'cli/auth'
8
+ require_relative 'cli/console'
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: strava-ruby-cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Doubrovkine
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-01-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gli
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ripl
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: strava-ruby-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email: dblock@dblock.org
57
+ executables:
58
+ - strava
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - CHANGELOG.md
63
+ - LICENSE.md
64
+ - README.md
65
+ - bin/strava
66
+ - lib/cli/auth.rb
67
+ - lib/cli/console.rb
68
+ - lib/cli/version.rb
69
+ - lib/strava-ruby-cli.rb
70
+ homepage: http://github.com/dblock/strava-ruby-cli
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: 1.3.6
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.7.6
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Strava API CLI.
94
+ test_files: []