Skip to content

Commit

Permalink
Merge pull request #9 from acroca/use-v1
Browse files Browse the repository at this point in the history
Refactored gem to use V1
  • Loading branch information
asoesilo committed Jun 26, 2015
2 parents abe3316 + 17f4c08 commit fa2b0e7
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 185 deletions.
21 changes: 16 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
routific (0.0.3)
routific (1.0.0)
json (~> 1.8)
rest-client (~> 1.7)

Expand All @@ -16,24 +16,29 @@ GEM
columnize (0.9.0)
debugger-linecache (1.2.0)
diff-lcs (1.2.5)
domain_name (0.5.24)
unf (>= 0.0.5, < 1.0.0)
dotenv (0.11.1)
dotenv-deployment (~> 0.0.2)
dotenv-deployment (0.0.2)
faker (1.4.2)
i18n (~> 0.5)
http-cookie (1.0.2)
domain_name (~> 0.5)
i18n (0.6.11)
json (1.8.2)
json (1.8.3)
method_source (0.8.2)
mime-types (2.4.3)
netrc (0.10.2)
mime-types (2.6.1)
netrc (0.10.3)
pry (0.10.1)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
pry-byebug (2.0.0)
byebug (~> 3.4)
pry (~> 0.10)
rest-client (1.7.2)
rest-client (1.8.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 3.0)
netrc (~> 0.7)
rspec (3.0.0)
Expand All @@ -49,6 +54,9 @@ GEM
rspec-support (~> 3.0.0)
rspec-support (3.0.2)
slop (3.6.0)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.1)

PLATFORMS
ruby
Expand All @@ -60,3 +68,6 @@ DEPENDENCIES
pry-byebug (~> 2.0)
routific!
rspec (~> 3.0)

BUNDLED WITH
1.10.3
95 changes: 48 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,16 @@ routific = Routific.new(--API_KEY--)

### Instance methods

`routific.setLocation( id, params )`
`routific.setVisit( id, [params] )`

Sets a location with the specified ID and parameters
Sets a visit for the specified location using the specified parameters

Required arguments in params:

- lat: Latitude of this location
- lng: Longitude of this location

Optional arguments in params:

- name: Name of this location

`routific.setVisit( id, [params] )`

Sets a visit for the specified location using the specified parameters
- location: Object representing the location of the visit.
+ lat: Latitude of this location
+ lng: Longitude of this location
+ name: (optional) Name of the location

Optional arguments in params:

Expand All @@ -53,11 +47,18 @@ Sets a vehicle with the specified ID and parameters

Required arguments in params:

- start_location: ID of start location for this vehicle
- start_location: Object representing the start location for this vehicle.
+ lat: Latitude of this location
+ lng: Longitude of this location
+ name: (optional) Name of the location

Optional arguments in params:

- end_location: ID of end location for this vehicle
- end_location: Object representing the end location for this vehicle.
+ lat: Latitude of this location
+ lng: Longitude of this location
+ name: (optional) Name of the location

- shift_start: this vehicle's start shift time (e.g. '08:00'). Default value is 00:00, if not specified.
- shift_end: this vehicle's end shift time (e.g. '17:00'). Default value is 23:59, if not specified.
- capacity: the capacity that this vehicle can load
Expand Down Expand Up @@ -94,27 +95,28 @@ require 'routific'

routific = Routific.new(--API_KEY--)

routific.setLocation("order_1", {
"name" => "6800 Cambie",
"lat" => 49.227107,
"lng" => -123.1163085,
})

routific.setLocation("depot", {
"name" => "800 Kingsway",
"lat" => 49.2553636,
"lng" => -123.0873365,
})

routific.setVisit("order_1", {
"start" => "9:00",
"end" => "12:00",
"duration" => 10,
"location" => {
"name" => "6800 Cambie",
"lat" => 49.227107,
"lng" => -123.1163085,
}
})

routific.setVehicle("vehicle_1", {
"start_location" => "depot",
"end_location" => "depot",
"start_location" => {
"name" => "800 Kingsway",
"lat" => 49.2553636,
"lng" => -123.0873365,
},
"end_location" => {
"name" => "800 Kingsway",
"lat" => 49.2553636,
"lng" => -123.0873365,
},
"shift_start" => "8:00",
"shift_end" => "12:00",
})
Expand All @@ -129,38 +131,37 @@ require 'routific'

Routific.setToken(--API_KEY--)

network = {
"order_1" => {
"name" => "6800 Cambie",
"lat" => 49.227107,
"lng" => -123.1163085
},
"depot" => {
"name" => "800 Kingsway",
"lat" => 49.2553636,
"lng" => -123.0873365
}
}

visits = {
"order_1" => {
"start" => "9:00",
"end" => "12:00",
"duration" => 10
"duration" => 10,
"location" => {
"name" => "6800 Cambie",
"lat" => 49.227107,
"lng" => -123.1163085
}
}
}

fleet = {
"vehicle_1" => {
"start-location" => "depot",
"end-location" => "depot",
"shift-start" => "8:00",
"shift-end" => "12:00"
"start_location" => {
"name" => "800 Kingsway",
"lat" => 49.2553636,
"lng" => -123.0873365
},
"end_location" => {
"name" => "800 Kingsway",
"lat" => 49.2553636,
"lng" => -123.0873365
},
"shift_start" => "8:00",
"shift_end" => "12:00"
}
}

data = {
network: network,
visits: visits,
fleet: fleet
}
Expand Down
22 changes: 7 additions & 15 deletions lib/routific.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,33 @@

# Main class of this gem
class Routific
attr_reader :token, :network, :visits, :fleet
attr_reader :token, :visits, :fleet

# Constructor
# token: Access token for Routific API
def initialize(token)
@token = token
@network = {}
@visits = {}
@fleet = {}
end

# Sets a location with the specified ID and parameters
# id: location ID
# params: parameters for this location
def setLocation(id, params)
network[id] = Location.new(params)
end

# Sets a visit for the specified location using the specified parameters
# id: ID of location to visit
# params: parameters for this visit
def setVisit(id, params={})
visits[id] = Visit.new(params)
visits[id] = Visit.new(id, params)
end

# Sets a vehicle with the specified ID and parameters
# id: vehicle ID
# params: parameters for this vehicle
def setVehicle(id, params)
fleet[id] = Vehicle.new(params)
fleet[id] = Vehicle.new(id, params)
end

# Returns the route using the previously provided network, visits and fleet information
# Returns the route using the previously provided visits and fleet information
def getRoute
data = {
network: network,
visits: visits,
fleet: fleet
}
Expand All @@ -62,7 +53,7 @@ def token
@@token
end

# Returns the route using the specified access token, network, visits and fleet information
# Returns the route using the specified access token, visits and fleet information
# If no access token is provided, the default access token previously set is used
# If the default access token either is nil or has not been set, an ArgumentError is raised
def getRoute(data, token = @@token)
Expand All @@ -75,7 +66,7 @@ def getRoute(data, token = @@token)

begin
# Sends HTTP request to Routific API server
response = RestClient.post('https://routific.com/api/vrp',
response = RestClient.post('https://api.routific.com/v1/vrp',
data.to_json,
'Authorization' => prefixed_token,
content_type: :json,
Expand All @@ -88,6 +79,7 @@ def getRoute(data, token = @@token)
# Parse the JSON representation into a Route object
Route.parse(jsonResponse)
rescue => e
puts e
errorResponse = JSON.parse e.response.body
puts "Received HTTP #{e.message}: #{errorResponse["error"]}"
nil
Expand Down
8 changes: 7 additions & 1 deletion lib/routific/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ def initialize(params)
@name = params["name"]
end

def ==(another_location)
self.name == another_location.name &&
self.lat == another_location.lat &&
self.lng == another_location.lng
end

def to_json(options = nil)
as_json(options).to_json
end
Expand All @@ -43,4 +49,4 @@ def validate(params)
raise ArgumentError, "'lat' and 'lng' parameters must be provided"
end
end
end
end
5 changes: 2 additions & 3 deletions lib/routific/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ def parse(routeJson)
way_points.each do |waypoint_info|
# Get all information for this way point
location_id = waypoint_info["location_id"]
location_name = waypoint_info["location_name"]
arrival_time = waypoint_info["arrival_time"]
finish_time = waypoint_info["finish_time"]
way_point = WayPoint.new(location_id, location_name, arrival_time, finish_time)
way_point = WayPoint.new(location_id, arrival_time, finish_time)
route.addWayPoint(vehicle_name, way_point)
end
end
Expand All @@ -46,4 +45,4 @@ def parse(routeJson)
route
end
end
end
end
33 changes: 18 additions & 15 deletions lib/routific/vehicle.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
# This class represents a vehicle in the fleet
class Vehicle
attr_accessor :start_location, :end_location, :shift_start, :shift_end, :capacity
attr_accessor :id, :start_location, :end_location, :shift_start, :shift_end, :capacity

# Constructor
#
# Required arguments in params:
# start_location: ID of start location for this vehicle
# start_location: start location for this vehicle. Instance of Location
#
# Optional arguments in params:
# end_location: ID of end location for this vehicle
# end_location: end location for this vehicle. Instance of Location
# shift_start: this vehicle's start shift time (e.g. '08:00'). Default value is 00:00, if not specified.
# shift_end: this vehicle's end shift time (e.g. '17:00'). Default value is 23:59, if not specified.
# capacity: the capacity that this vehicle can load
def initialize(params)
# Validates the provided parameters
def initialize(id, params)
validate(params)

@start_location = params["start_location"]
@end_location = params["end_location"]
@id = id
@start_location = Location.new(params["start_location"])
if params["end_location"]
@end_location = Location.new(params["end_location"])
end
@shift_start = params["shift_start"]
@shift_end = params["shift_end"]
@capacity = params["capacity"]
Expand All @@ -31,22 +33,23 @@ def to_json(options=nil)
# def to_json(options = nil)
def as_json(options = nil)
jsonData = {}
jsonData["start-location"] = self.start_location
jsonData["end-location"] = self.end_location if self.end_location
jsonData["shift-start"] = self.shift_start if self.shift_start
jsonData["shift-end"] = self.shift_end if self.shift_end
jsonData["start_location"] = self.start_location.as_json
jsonData["end_location"] = self.end_location.as_json if self.end_location
jsonData["shift_start"] = self.shift_start if self.shift_start
jsonData["shift_end"] = self.shift_end if self.shift_end
jsonData["capacity"] = self.capacity if self.capacity

jsonData
end


private
# Validates the parameters being provided
# Raises an ArgumentError if any of the required parameters is not provided.
# Required parameter is: start_location
# Required parameters are: location
def validate(params)
if(params["start_location"].nil?)
raise ArgumentError, "'start_location' parameter must be provided"
if params["start_location"].nil?
raise ArgumentError, "'start-location' parameter must be provided"
end
end
end
end
Loading

0 comments on commit fa2b0e7

Please sign in to comment.