Class: ParkingTicket::Clients::PayByPhone::Adapter
- Inherits:
-
Object
- Object
- ParkingTicket::Clients::PayByPhone::Adapter
- Extended by:
- T::Sig
- Defined in:
- lib/clients/pay_by_phone/adapter.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- ACCEPTED_TIME_UNIT_MAPPER =
T.let({ 'Days' => 'days', 'Minutes' => 'minutes', 'Hours' => 'hours' }.freeze, T::Hash[String, String])
- PAYMENT_CARD_TYPE_MAPPER =
T.let({ 'MasterCard' => 'master_card', 'Visa' => 'visa' }.freeze, T::Hash[String, String])
- VEHICLE_TYPE_MAPPER =
T.let({ 'Car' => 'combustion_car', 'ElectricMotorcycle' => 'electric_motorcycle' }.freeze, T::Hash[String, String])
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(username, password) ⇒ Adapter
constructor
A new instance of Adapter.
- #new_ticket(license_plate:, zipcode:, rate_option_client_internal_id:, quantity:, time_unit:, payment_method_id:) ⇒ Object
- #payment_methods ⇒ Object
- #quote(rate_option_id, zipcode, license_plate, quantity, time_unit) ⇒ Object
- #raise_invalid_credentials! ⇒ Object
- #rate_options(zipcode, license_plate) ⇒ Object
- #running_ticket(license_plate, zipcode) ⇒ Object
- #vehicles ⇒ Object
Constructor Details
#initialize(username, password) ⇒ Adapter
Returns a new instance of Adapter.
42 43 44 45 46 47 |
# File 'lib/clients/pay_by_phone/adapter.rb', line 42 def initialize(username, password) @username = username @password = password @valid_credentials = T.let(self.class.valid_credentials?(username, password), T::Boolean) @client = T.let(self.class.client_class.new(@username, @password), ParkingTicket::Clients::PayByPhone::Client) end |
Class Method Details
.client_class ⇒ Object
13 14 15 |
# File 'lib/clients/pay_by_phone/adapter.rb', line 13 def client_class ParkingTicket::Clients::PayByPhone::Client end |
.valid_credentials?(username, password) ⇒ Boolean
18 19 20 |
# File 'lib/clients/pay_by_phone/adapter.rb', line 18 def valid_credentials?(username, password) client_class.auth(username, password).status == 200 end |
Instance Method Details
#new_ticket(license_plate:, zipcode:, rate_option_client_internal_id:, quantity:, time_unit:, payment_method_id:) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/clients/pay_by_phone/adapter.rb', line 134 def new_ticket(license_plate:, zipcode:, rate_option_client_internal_id:, quantity:, time_unit:, payment_method_id:) raise_invalid_credentials! unless @valid_credentials mapped_time_unit = ACCEPTED_TIME_UNIT_MAPPER.invert.fetch(time_unit) quote = quote(rate_option_client_internal_id, zipcode, license_plate, quantity, time_unit) @client.new_ticket( license_plate: license_plate, zipcode: zipcode, rate_option_client_internal_id: rate_option_client_internal_id, quantity: quantity, time_unit: mapped_time_unit, quote_client_internal_id: quote.client_internal_id, starts_on: quote.starts_on, payment_method_id: payment_method_id ) end |
#payment_methods ⇒ Object
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/clients/pay_by_phone/adapter.rb', line 102 def payment_methods raise_invalid_credentials! unless @valid_credentials @client.payment_methods.fetch('paymentCards').map do |payment_method| ParkingTicket::Clients::Models::PaymentMethod.new( client_internal_id: payment_method['paymentAccountId'], anonymised_card_number: payment_method['maskedCardNumber'][-4..], payment_card_type: PAYMENT_CARD_TYPE_MAPPER.fetch(payment_method['cardType']) ) end end |
#quote(rate_option_id, zipcode, license_plate, quantity, time_unit) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/clients/pay_by_phone/adapter.rb', line 117 def quote(rate_option_id, zipcode, license_plate, quantity, time_unit) raise_invalid_credentials! unless @valid_credentials mapped_time_unit = T.must(ACCEPTED_TIME_UNIT_MAPPER.key(time_unit)) fetched_quote = @client.quote(rate_option_id, zipcode, license_plate, quantity, mapped_time_unit) ParkingTicket::Clients::Models::Quote.new( client_internal_id: fetched_quote['quoteId'], starts_on: DateTime.parse(fetched_quote['parkingStartTime']), ends_on: DateTime.parse(fetched_quote['parkingExpiryTime']), cost: fetched_quote.dig('totalCost', 'amount').to_f ) end |
#raise_invalid_credentials! ⇒ Object
156 157 158 |
# File 'lib/clients/pay_by_phone/adapter.rb', line 156 def raise_invalid_credentials! raise Error, 'Adapter credentials are not valid' end |
#rate_options(zipcode, license_plate) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/clients/pay_by_phone/adapter.rb', line 65 def (zipcode, license_plate) raise_invalid_credentials! unless @valid_credentials @client.(zipcode, license_plate).map do |rate_option| mapped_time_units = rate_option['acceptedTimeUnits'].map do |accepted_time_unit| ACCEPTED_TIME_UNIT_MAPPER[accepted_time_unit] end ParkingTicket::Clients::Models::RateOption.new( client_internal_id: rate_option['rateOptionId'], name: rate_option['name'], type: rate_option['type'], accepted_time_units: mapped_time_units ) end end |
#running_ticket(license_plate, zipcode) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/clients/pay_by_phone/adapter.rb', line 83 def running_ticket(license_plate, zipcode) raise_invalid_credentials! unless @valid_credentials @client.running_tickets.filter do |ticket| ticket.dig('vehicle', 'licensePlate') == license_plate && ticket['locationId'] == zipcode end.map do |ticket| ParkingTicket::Clients::Models::Ticket.new( client_internal_id: ticket['parkingSessionId'], 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' ) end.first end |
#vehicles ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/clients/pay_by_phone/adapter.rb', line 50 def vehicles raise_invalid_credentials! unless @valid_credentials @client.vehicles.map do |vehicle| ParkingTicket::Clients::Models::Vehicle.new( client_internal_id: vehicle['vehicleId'], license_plate: vehicle['licensePlate'], vehicle_type: T.must(VEHICLE_TYPE_MAPPER[vehicle['type']]), vehicle_description: vehicle.dig('profile', 'description') ) end end |