Skip to content

Instantly share code, notes, and snippets.

@yowchun93
Last active April 16, 2020 06:17
Show Gist options
  • Save yowchun93/ecbfc5887a87d10ad40ef71e3e4cc565 to your computer and use it in GitHub Desktop.
Save yowchun93/ecbfc5887a87d10ad40ef71e3e4cc565 to your computer and use it in GitHub Desktop.
Order creation strategy
## Might be unpopular , but i dont think we need this class because Ruby != Java LOL
## We should trust the object to implement create_order
class CourierAPI::OrderCreationStrategy::Base
attr_reader :order, :error_message
def create_order(parcel)
raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
end
end
class CourierApi::OrderCreationStrategy::Pickupp
def create_order(parcel)
scheduler_options = {pickup_datetime: parcel.pickup_datetime}
request_body = PickuppAPI::RequestBody.new(parcel, **scheduler_options).call
@order = PickuppAPI::Order.new(request_body)
@order.save
rescue PickuppAPI::ResourceInvalid, PickuppAPI::BadRequest, PickuppAPI::ServerError,
ActiveResource::UnauthorizedAccess, ActiveResource::ResourceNotFound, ActiveResource::TimeoutError => e
@error_message = e.message
false
end
end
class CourierApi::OrderCreationStrategy::Raf
def create_order(parcel)
scheduler_options = {pickup_datetime: parcel.pickup_datetime}
request_body = RafAPI::RequestBody.new(parcel, **scheduler_options).call
@order = RafAPI::Order.new(request_body)
@order.save
rescue RafAPI::ResourceInvalid, PickuppAPI::BadRequest, PickuppAPI::ServerError,
ActiveResource::UnauthorizedAccess, ActiveResource::ResourceNotFound, ActiveResource::TimeoutError => e
@error_message = e.message
false
end
end
class CreateRemoveOrderService
def initialize
@parcel = parcel
@order_creation_strategy = set_order_creation_strategy
end
def call
order = order_creation_strategy.create_order(parcel)
## should the saving, label creation , and updating happen in the strategy or here ?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment