Sha256: 81237a896d5766c62fd537c3fde5e40d08ee5e95cc73d54c1371485340dd95da

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

module ParkingTicket
  module Clients
    class Adapter
      class << self
        def parent_module
          const_get(name.split('::')[0..-2].join('::'))
        end

        def valid_credentials?(username, password)
          parent_module::Client.auth(username, password).status == 200
        end
      end
      class Error < StandardError
      end

      def initialize(username, password)
        @username = username
        @password = password
      end

      def vehicles
        raise_invalid_credentials! unless valid_credentials?
        fetch_and_map_vehicles
      end

      def rate_options(zipcode, license_plate)
        raise_invalid_credentials! unless valid_credentials?
        fetch_and_map_rate_options(zipcode, license_plate)
      end

      def running_ticket(license_plate, zipcode)
        raise_invalid_credentials! unless valid_credentials?
        fetch_and_map_running_ticket(license_plate, zipcode)
      end

      def payment_methods
        raise_invalid_credentials! unless valid_credentials?
        fetch_and_map_payment_methods
      end

      def new_ticket(license_plate, zipcode, rate_option_id, quantity, time_unit, payment_method_id)
        raise_invalid_credentials! unless valid_credentials?
        return if running_ticket(license_plate, zipcode)

        request_new_ticket(license_plate, zipcode, rate_option_id, quantity, time_unit, payment_method_id)
      end

      private

      def client
        @client ||= self.class.parent_module::Client.new(@username, @password)
      end

      def valid_credentials?
        @valid_credentials ||= self.class.valid_credentials?(@username, @password)
      end

      def raise_invalid_credentials!
        raise Error, 'Adapter credentials are not valid'
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
parking_ticket-1.0.41 lib/clients/adapter.rb