Class: Drivenow::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/drivenow/drivenow_agent.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Agent

Creates a new instance of the Drivenow::Agent. options:

:city => The City you want to get all for. e.g. :berlin
:uri => The URI can be changed in case you want to test locally or anything else

– TODO: Option for the Xpath for cars-array inside the JSON



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/drivenow/drivenow_agent.rb', line 15

def initialize(options = {})
	cities = Agent.cities
	unless options[:city].nil?
		url_params = "cit=#{cities[options[:city]]}"
	end
	options = { :uri => "https://www.drive-now.com/php/metropolis/json.vehicle_filter?#{url_params}" }.merge(options)

	page = open(options[:uri]).read
	cars = JSON(page)["rec"]["vehicles"]["vehicles"]
	cities = cities.invert
	cars.each { |item| item["cit"] = cities[item["cit"]] }
	cars.map! { |item| Car.new(item) }
	@cars = cars
end

Instance Attribute Details

#carsObject (readonly)

Returns the value of attribute cars.



7
8
9
# File 'lib/drivenow/drivenow_agent.rb', line 7

def cars
  @cars
end

Class Method Details

.citiesObject

Gets all cities that are available at the moment, currently

* Berlin
* Düsseldorf
* München

Can easily be overridden if there are more cities available in the future



39
40
41
42
43
44
45
46
# File 'lib/drivenow/drivenow_agent.rb', line 39

def self.cities
	{
		:duesseldorf => "1293",
		:berlin => "6099", 
		:muenchen => "4604",
		:koeln => "1774"
	}
end

Instance Method Details

#inspectObject



30
31
32
# File 'lib/drivenow/drivenow_agent.rb', line 30

def inspect
	@cars.inspect
end