Class: Ubr::CLI
- Inherits:
-
Object
- Object
- Ubr::CLI
- Defined in:
- lib/ubr/cli.rb
Class Method Summary collapse
- .authorize!(client_id, code, redirect_uri, secret = nil) ⇒ Object
- .login!(client_id) ⇒ Object
- .parse_options! ⇒ Object
Class Method Details
.authorize!(client_id, code, redirect_uri, secret = nil) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/ubr/cli.rb', line 66 def self.(client_id, code, redirect_uri, secret=nil) unless secret print "Give me your secret: " secret = $stdin.gets.strip end result = API.(client_id: client_id, code: code, secret: secret, redirect_uri: redirect_uri) token = result[:access_token] if token filename = API.token_path File.write(filename, token) File.chmod(0600, filename) puts puts "Hurray! Your are authorized!" puts "The token is saved to #{filename}" else end end |
.login!(client_id) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ubr/cli.rb', line 55 def self.login!(client_id) uri = 'https://login.uber.com/oauth/authorize?' + [ 'response_type=code', 'client_id=' + client_id, 'state=' + client_id, 'scope=request', ].join('&') puts "Opening login URL: #{uri}" Launchy.open(uri) end |
.parse_options! ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/ubr/cli.rb', line 9 def self. = {} argv = ARGV.dup OptionParser.new do |opts| opts. = 'Usage: ubr <pickup> <dropoff> [options]' opts.separator '' opts.separator 'ubr: Request an Uber from the command line.' opts.separator '' opts.separator 'Options:' opts.on('-h', '--help', 'Displays this help') do puts opts exit end opts.on('-p', '--product NAME', 'Automatically selects a product') do |product| [:product] = product end end.parse!(argv) case argv[0] when 'login' client_id = argv[1] or raise "Client ID needed" login!(client_id) exit when 'authorize' client_id = argv[1] or raise "Client ID needed" code = argv[2] or raise "Code needed" redirect_uri = argv[3] or raise "Redirect URI needed" (client_id, code, redirect_uri) exit end [:pickup] = Coordinate.parse(argv.shift) or raise "Pickup location needed!" [:dropoff] = Coordinate.parse(argv.shift) or raise "Dropoff location needed!" raise "Unrecognized args: #{argv.inspect}" unless argv.empty? end |