Skip to content

Instantly share code, notes, and snippets.

@indrekj
Created November 27, 2012 13:48
Show Gist options
  • Select an option

  • Save indrekj/4154301 to your computer and use it in GitHub Desktop.

Select an option

Save indrekj/4154301 to your computer and use it in GitHub Desktop.

Interactor

module Trip
  module Controls
    class AttendsTripOffer
      def initialize(request)
        Validator.validate!(request)

        @user = FacebookUserFetcher.get(request.access_token)
        @trip_offer = Repositories::TripOffer.find_by_id(request.trip_offer_id)
      end

      def call
        Permissions::TripOfferAttendingPolicy.enforce!(@trip_offer, @user)

        @trip_offer.add_passenger(@user)

        Repositories::TripOffer.save(@trip_offer)

        Response.new(:trip_offer => @trip_offer)
      end

      class Request
        include Virtus
        include Aequitas

        attribute :access_token, String
        attribute :trip_offer_id, Integer

        validates_presence_of :access_token

        validates_presence_of :trip_offer_id
        validates_numericalness_of :trip_offer_id
      end

      class Response
        include Virtus

        attribute :trip_offer, TripOffer
      end
    end
  end
end

Usage (sinatra)

    include Trip::Controls

    post "/trip_offers/:trip_offer_id/attend" do
      begin
        req = AttendsTripOffer::Request.new
        req.access_token = access_token
        req.trip_offer_id = params[:trip_offer_id]

        res = AttendsTripOffer.new(req).call
        trip_offer = res.trip_offer

        Serializers::TripOffer.new(trip_offer).to_json
      rescue => exception
        handle_error(exception)
      end
    end

Please critique, all input appreciated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment