Class: Roadtrip::Trip

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/roadtrip.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start, destination, cost_per_gallon, mpg) ⇒ Trip

Returns a new instance of Trip.



21
22
23
24
25
26
# File 'lib/roadtrip.rb', line 21

def initialize(start, destination, cost_per_gallon, mpg)
  @start = start
  @destination = destination
  @cost_per_gallon = cost_per_gallon
  @mpg = mpg
end

Instance Attribute Details

#cost_per_gallonObject

Returns the value of attribute cost_per_gallon.



19
20
21
# File 'lib/roadtrip.rb', line 19

def cost_per_gallon
  @cost_per_gallon
end

#destinationObject

Returns the value of attribute destination.



19
20
21
# File 'lib/roadtrip.rb', line 19

def destination
  @destination
end

#mpgObject

Returns the value of attribute mpg.



19
20
21
# File 'lib/roadtrip.rb', line 19

def mpg
  @mpg
end

#startObject

Returns the value of attribute start.



19
20
21
# File 'lib/roadtrip.rb', line 19

def start
  @start
end

Instance Method Details

#costObject



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

def cost
  distance_in_miles * self.cost_per_gallon / mpg
end

#distanceObject



37
38
39
# File 'lib/roadtrip.rb', line 37

def distance
  trip["routes"][0]["legs"][0]["distance"]["text"]
end

#distance_in_milesObject



45
46
47
# File 'lib/roadtrip.rb', line 45

def distance_in_miles
  MILES_PER_METER * trip["routes"][0]["legs"][0]["distance"]["value"]
end

#durationObject



41
42
43
# File 'lib/roadtrip.rb', line 41

def duration
  trip["routes"][0]["legs"][0]["duration"]["text"]
end

#round_trip_costObject



53
54
55
# File 'lib/roadtrip.rb', line 53

def round_trip_cost
  self.cost * 2
end

#tripObject



28
29
30
31
32
33
34
35
# File 'lib/roadtrip.rb', line 28

def trip
  Trip.get('http://maps.google.com/maps/api/directions/json?',
           :query => {
             :origin  => self.start,
             :destination   => self.destination,
             :sensor => "false"
           })
end