Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Routific#set_project( data ) #48

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
132 changes: 132 additions & 0 deletions .byebug_history
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
exit
params
exit
params
exit
Object.const_get(class_name)
send(class_name.to_sym)
class_name.to_sym
class_name
c
params
exit
params.fetch("drivers")
params.fetch("drivers"0
exit
result
c
exit
value.class
value
exit
params[attr]
params
params[attr.to_s]
attr
exit
self.send(attr)
attr
exit
self.send(attr)
value
attr
exit
collections.include?(attr)
attr
c
n
params
self.collections
self.object_values
ATTRS
exit
params
RoutificApi::Project.new(id: params.fetch(:id),name: params.fetch(:name),date: params.fetch(:date))
9: )
8: date: params.fetch(:date)
7: name: params.fetch(:name),
6: id: params.fetch(:id),
RoutificApi::Project.new(
exit
params
exit
result
e
exit
data.to_json
data
response
exit
job_data
exit
job_data
exit
job.fetch
job
n
data
result['job_id']
n
c
response
exit
headers(token)
method.downcase.to_sym
nil.to_json
url(endpoint)
RestClient::Request.execute(method: method.downcase.to_sym,url: url(endpoint),data: data.to_json,headers: headers(token))
data.to_json
data
response
token
endpoint
method
exit
Util.send_request("POST", "vrp", Routific.token, data)
exit
data
exit
data
exit
JSON.parse({'blh': 'ljl'})
JSON.parse({'blh'=> 'ljl'})
JSON.parse(result)
result
exit
self
attr
self.send(attr).class
attr.class
attr
exit
subject
exit
subject
exit
@date
@name
@id
@stops
@stop
exit
@stops
stops
stopes
stores
exit
@name
name
self name
set_attrs(params)
params
attrs
exit
params
attrs
exit
self.send(attr).map(&:as_json)
self.send(attr).each(&:as_json)
self.send(attr)
exit
send(attr)
attr
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ routific-*.gem

# Ignore gem locked versions
Gemfile.lock

# Ignore byebug history
.byebug_history
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,7 @@ It returns a job object with the following attibutes:
- `created_at`, `finished_at`: creation and finish times
- `input`: the data used for the request
- `route`: a route object

#### `routific.set_project( data )`

Read the [platform documentation](https://routific-platform.readme.io/reference) for a full list of attributes.
5 changes: 5 additions & 0 deletions lib/errors/missing_attribute_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class MissingAttributeError < StandardError
def initialize(msg = "Attribute not set in subclass")
super
end
end
5 changes: 5 additions & 0 deletions lib/errors/not_array_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class NotArrayError < StandardError
def initialize(msg = "Not an array")
super
end
end
20 changes: 20 additions & 0 deletions lib/routific.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
require_relative './errors/not_array_error'
require_relative './errors/missing_attribute_error'
require_relative './routific/jsonable'
require_relative './routific/attributable'
require_relative './routific/location'
require_relative './routific/visit'
require_relative './routific/break'
Expand All @@ -6,6 +10,17 @@
require_relative './routific/way_point'
require_relative './routific/options'
require_relative './routific/job'
require_relative './routific/project'
require_relative './routific/driver'
require_relative './routific/stop'
require_relative './routific/setting'
require_relative './routific/project_location'
require_relative './routific/factory_helper'
require_relative './routific/project_factory'
require_relative './routific/driver_factory'
require_relative './routific/stop_factory'
require_relative './routific/setting_factory'
require_relative './routific/project_location_factory'

require_relative './util'

Expand Down Expand Up @@ -65,6 +80,11 @@ def get_route_async
RoutificApi::Job.new(result["job_id"], data)
end

def set_project(data)
result = Util.send_request("POST", "project", Routific.token, data, true)
RoutificApi::ProjectFactory.new(result).call
end

class << self
# Sets the default access token to use
def set_token(token)
Expand Down
58 changes: 58 additions & 0 deletions lib/routific/attributable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module RoutificApi
class Attributable
include RoutificApi::Jsonable

ATTRS = %i(values object_values collections)
def initialize(params)
ensure_attrs_set
set_attrs(params)
end

private

def attrs
values + object_values + collections
end

def ensure_attrs_set
ATTRS.each do |attr|
raise MissingAttributeError unless self.respond_to?(attr)
end
end

def set_attrs(params)
attrs.each do |attr|
set_attr(attr, params[attr.to_s])
end
end

def set_attr(attr, value)
define_setter(attr, value)
define_reader(attr)
set_variable(attr, value)
ensure_collection_is_array(attr, value)
end

def ensure_collection_is_array(attr, value)
if collections.include?(attr) && value.class != Array
raise NotArrayError
end
end

def define_setter(attr, value)
self.class.send(:define_method, "#{ attr }=".to_sym) do |value|
instance_variable_set("@" + attr.to_s, value)
end
end

def define_reader(attr)
self.class.send(:define_method, attr.to_sym) do
instance_variable_get("@" + attr.to_s)
end
end

def set_variable(attr, value)
self.send("#{ attr }=".to_sym, value)
end
end
end
15 changes: 15 additions & 0 deletions lib/routific/coords.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module RoutificApi
class Coords < Attributable
def values
[]
end

def object_values
%(lat lng)
end

def collections
[]
end
end
end
15 changes: 15 additions & 0 deletions lib/routific/driver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module RoutificApi
class Driver < Attributable
def values
%i(name shift_start shift_end phone_number speed capacity)
end

def object_values
%i(start_location end_location break)
end

def collections
%i(types)
end
end
end
25 changes: 25 additions & 0 deletions lib/routific/driver_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module RoutificApi
class DriverFactory
include FactoryHelper

attr_reader :params
def initialize(params)
@params = params
end

def call
Driver.new(
"name" => name,
"start_location" => start_location,
"end_location" => end_location,
"shift_start" => shift_start,
"shift_end" => shift_end,
"phone_number" => phone_number,
"speed" => speed,
"capacity" => capacity,
"types" => types,
"break" => shift_break
)
end
end
end
Loading