Class: Ubr::App

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ App

Returns a new instance of App.



12
13
14
15
# File 'lib/ubr/app.rb', line 12

def initialize(options)
  @options = options
  @client  = API.new
end

Class Method Details

.run!(options) ⇒ Object



8
9
10
# File 'lib/ubr/app.rb', line 8

def self.run!(options)
  new(options).run!
end

Instance Method Details

#get_available_productsObject



48
49
50
# File 'lib/ubr/app.rb', line 48

def get_available_products
  @client.get('/products', @options[:pickup].to_h)[:products]
end

#get_estimate_for(product:) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/ubr/app.rb', line 78

def get_estimate_for(product:)
  @client.post('requests/estimate',
    product_id:      product[:product_id],
    start_latitude:  @options[:pickup].latitude,
    start_longitude: @options[:pickup].longitude,
    end_latitude:    @options[:dropoff].latitude,
    end_longitude:   @options[:dropoff].longitude,
  )
end


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ubr/app.rb', line 52

def print_estimate(estimate)
  if estimate[:pickup_estimate]
    puts "Pickup in #{estimate[:pickup_estimate]} minutes."
    puts "Estimation:"

    distance_estimate = estimate[:trip][:distance_estimate]
    distance_unit = estimate[:trip][:distance_unit].pluralize(distance_estimate)
    puts "- Distance: #{distance_estimate} #{distance_unit}"

    pickup_time = estimate[:pickup_estimate].minutes.from_now
    puts "- Pickup #{pickup_time.to_s :time}"

    dropoff_time = pickup_time + estimate[:trip][:duration_estimate].seconds
    puts "- Dropoff #{dropoff_time.to_s :time}"

    surge_x = if estimate[:price][:surge_multiplier] == 1.0
                'no'
              else
                "#{estimate[:price][:surge_multiplier]}"
              end
    puts "- Price: #{estimate[:price][:display]} (#{surge_x} surge)"
  else
    puts "None available."
  end
end

#request_uber!(product:, surge_confirmation_id:) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/ubr/app.rb', line 88

def request_uber!(product:, surge_confirmation_id:)
  @client.post('requests',
    product_id:      product[:product_id],
    start_latitude:  @options[:pickup].latitude,
    start_longitude: @options[:pickup].longitude,
    end_latitude:    @options[:dropoff].latitude,
    end_longitude:   @options[:dropoff].longitude,
    surge_confirmation_id: surge_confirmation_id,
  )
end

#run!Object



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
# File 'lib/ubr/app.rb', line 17

def run!

  print 'Searching for available Ubers...'
  products = get_available_products
  product = pick_or_choose(products,
    title: 'Available Ubers',
    pick: method(:product_preselected?),
    display: method(:product_description),
  )

  estimate = nil

  loop do
    print 'Getting the ETA...'
    estimate = get_estimate_for(product: product)
    puts
    print_estimate estimate
    break if confirm? "Do you want to request an #{product[:display_name]}?"
    print_separator
  end

  surge_confirmation_id = nil
  if estimate[:surge_confirmation_id] and confirm? "Do you accept the surge?"
    surge_confirmation_id = estimate[:surge_confirmation_id]
  end

  request_uber!(product: product, surge_confirmation_id: surge_confirmation_id)
  puts "Done! Please check your phone."

end