Class: Ubr::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/ubr/cli.rb

Class Method Summary collapse

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.authorize!(client_id, code, redirect_uri, secret=nil)
  unless secret
    print "Give me your secret: "
    secret = $stdin.gets.strip
  end
  result = API.authorize(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.parse_options!

  options = {}
  argv = ARGV.dup

  OptionParser.new do |opts|

    opts.banner = '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|
      options[: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"
    authorize!(client_id, code, redirect_uri)
    exit
  end

  options[:pickup]  = Coordinate.parse(argv.shift) or raise "Pickup location needed!"
  options[:dropoff] = Coordinate.parse(argv.shift) or raise "Dropoff location needed!"

  raise "Unrecognized args: #{argv.inspect}" unless argv.empty?

  options

end