Class: Spritpreisrechner::Station

Inherits:
Object
  • Object
show all
Defined in:
lib/spritpreisrechner/station.rb

Constant Summary collapse

FUEL_TYPES =
%w[DIE SUP GAS].freeze
REGION_TYPES =
%w[BL PB].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(station) ⇒ Station

Returns a new instance of Station.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/spritpreisrechner/station.rb', line 9

def initialize(station)
  @id = station[:id]
  @name = station[:name]
  @location = Location.new(station[:location])
  @contact = Contact.new(station[:contact])
  @opening_hours = []
  @offer_information = OfferInformation.new(station[:offerInformation])
  @payment_methods = PaymentMethod.new(station[:paymentMethods])
  @payment_arrangements = PaymentArrengement.new(station[:paymentArrangements])
  @other_service_offers = station[:otherServiceOffers]&.split(', ')
  @position = station[:position].to_i
  @open = station[:open]
  @distance = station[:distance].to_f
  @prices = []

  station[:openingHours].each do |opening_hour|
    @opening_hours << OpeningHour.new(opening_hour)
  end

  station[:prices].each do |price|
    @prices << Price.new(price)
  end
end

Instance Attribute Details

#contactObject (readonly)

Returns the value of attribute contact.



4
5
6
# File 'lib/spritpreisrechner/station.rb', line 4

def contact
  @contact
end

#distanceObject (readonly)

Returns the value of attribute distance.



3
4
5
# File 'lib/spritpreisrechner/station.rb', line 3

def distance
  @distance
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/spritpreisrechner/station.rb', line 3

def id
  @id
end

#locationObject (readonly)

Returns the value of attribute location.



4
5
6
# File 'lib/spritpreisrechner/station.rb', line 4

def location
  @location
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/spritpreisrechner/station.rb', line 3

def name
  @name
end

#offer_informationObject (readonly)

Returns the value of attribute offer_information.



4
5
6
# File 'lib/spritpreisrechner/station.rb', line 4

def offer_information
  @offer_information
end

#openObject (readonly)

Returns the value of attribute open.



3
4
5
# File 'lib/spritpreisrechner/station.rb', line 3

def open
  @open
end

#opening_hoursObject (readonly)

Returns the value of attribute opening_hours.



4
5
6
# File 'lib/spritpreisrechner/station.rb', line 4

def opening_hours
  @opening_hours
end

#other_service_offersObject (readonly)

Returns the value of attribute other_service_offers.



3
4
5
# File 'lib/spritpreisrechner/station.rb', line 3

def other_service_offers
  @other_service_offers
end

#payment_arrangementsObject (readonly)

Returns the value of attribute payment_arrangements.



4
5
6
# File 'lib/spritpreisrechner/station.rb', line 4

def payment_arrangements
  @payment_arrangements
end

#payment_methodsObject (readonly)

Returns the value of attribute payment_methods.



4
5
6
# File 'lib/spritpreisrechner/station.rb', line 4

def payment_methods
  @payment_methods
end

#positionObject (readonly)

Returns the value of attribute position.



3
4
5
# File 'lib/spritpreisrechner/station.rb', line 3

def position
  @position
end

#pricesObject (readonly)

Returns the value of attribute prices.



4
5
6
# File 'lib/spritpreisrechner/station.rb', line 4

def prices
  @prices
end

Class Method Details

.by_address(lat: nil, lng: nil, fuel_type: 'DIE', closed: 'true') ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/spritpreisrechner/station.rb', line 34

def self.by_address(lat: nil, lng: nil, fuel_type: 'DIE', closed: 'true')
  errors = []
  errors << 'you need to submit a valid latitude coordinate' if lat.nil?
  errors << 'you need to submit a valid longitude coordinate' if lng.nil?
  errors << "you need to submit a valid Fuel Type. The options are #{FUEL_TYPES}" if fuel_type.nil? || !FUEL_TYPES.include?(fuel_type)
  return Response.new(errors: errors) if errors.any?

  response = conn.get('search/gas-stations/by-address', latitude: lat, longitude: lng, fuelType: fuel_type, includeClosed: closed)
  attributes = JSON.parse(response.body, symbolize_names: true)
  response = Response.new(attributes)
  response.stations = []

  if response.success?
    attributes.each do |station|
      response.stations << Station.new(station)
    end
  end

  response
end

.by_region(code: nil, region_type: nil, fuel_type: 'DIE', closed: 'true') ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/spritpreisrechner/station.rb', line 55

def self.by_region(code: nil, region_type: nil, fuel_type: 'DIE', closed: 'true')
  errors = []
  errors << 'you need to submit a valid region code' if code.nil?
  errors << "you need to submit a valid region type. The options are #{REGION_TYPES}" if region_type.nil? || !REGION_TYPES.include?(region_type)
  errors << "you need to submit a valid Fuel Type. The options are #{FUEL_TYPES}" if fuel_type.nil? || !FUEL_TYPES.include?(fuel_type)
  return Response.new(errors: errors) if errors.any?

  response = conn.get('search/gas-stations/by-region', code: code, type: region_type, fuelType: fuel_type, includeClosed: closed)
  attributes = JSON.parse(response.body, symbolize_names: true)
  response = Response.new(attributes)
  response.stations = []

  if response.success?
    attributes.each do |station|
      response.stations << Station.new(station)
    end
  end

  response
end

.connObject



76
77
78
# File 'lib/spritpreisrechner/station.rb', line 76

def self.conn
  Spritpreisrechner.conn
end