Sha256: 86b569a6e78405cd040b0ca972dc9dfed4a0d334e5441c4228990b6d2ec2bfa3

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

require 'byebug'
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.40 lib/clients/adapter.rb