Sha256: 915a82154c0a148d95b57465d417387ca1edbda8d6e82feb2395a3817b34f570

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

module ParkingTicket
  module Client
    module PayByPhone
      class Adapter
        class << self
          def valid_credentials?(username, password)
            Request.valid_credentials?(username, password)
          end
        end

        def initialize(configuration)
          @configuration = configuration
        end

        def valid_credentials?(_username, _password)
          request.valid_credentials?(_username, _password)
        end

        def covered?
          !!current_ticket
        end

        def current_ticket
          ticket = request.tickets(account_id).find do |ticket|
            ticket.dig('vehicle', 'licensePlate') == @configuration.license_plate
          end

          return unless ticket

          {
            starts_on: DateTime.parse(ticket['startTime']),
            ends_on: DateTime.parse(ticket['expireTime']),
            license_plate: ticket.dig('vehicle', 'licensePlate'),
            cost: ticket.dig('segments', 0, 'cost'),
            client: 'PayByPhone',
            client_ticket_id: ticket['parkingSessionId']
          }
        end

        def renew
          return if covered?

          quote = request.new_quote(rate_option_id, account_id)
          request.new_ticket(account_id, quote['parkingStartTime'], quote['quoteId'], payment_method_id)
        end

        private

        def request
          @request ||= Request.new(@configuration)
        end

        def account_id
          @account_id ||= request.accounts.dig(0, 'id')
        end

        def payment_method_id
          request.payment_methods['items'].find do |payment_method|
            payment_method['maskedCardNumber'] == @configuration.card_number
          end['id']
        end

        def rate_option_id
          request.rate_options(account_id).find do |rate_option|
            rate_option['type'] == 'RES' && rate_option['licensePlate'] == @configuration.license_plate
          end['rateOptionId']
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
parking_ticket-1.0.32 lib/client/pay_by_phone/adapter.rb