Class: Multicity::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/multicity/multicity_agent.rb

Constant Summary collapse

@@options =
{
  :url_list => 'https://kunden.multicity-carsharing.de/kundenbuchung/hal2ajax_process.php?zoom=10&lng1=&lat1=&lng2=&lat2=&stadtCache=&mapstation_id=&mapstadt_id=&verwaltungfirma=&centerLng=13.382322739257802&centerLat=52.50734843957503&searchmode=buchanfrage&with_staedte=false&buchungsanfrage=N&lat=52.50734843957503&lng=13.382322739257802&instant_access=J&open_end=J&objectname=multicitymarker&ignore_virtual_stations=J&before=null&after=null&ajxmod=hal2map&callee=getMarker',
  :url_area => 'https://www.multicity-carsharing.de/wp-content/plugins/multicity_map/geschaeftsbereich_neu.kml',
  :url_load_stations => 'https://www.multicity-carsharing.de/rwe/json.php'
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAgent

Returns a new instance of Agent.



15
16
17
18
# File 'lib/multicity/multicity_agent.rb', line 15

def initialize
  @agent = Mechanize.new
  @agent.user_agent = @@user_agent ||= "Multicity Rubygem"
end

Class Method Details

.price(minutes) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/multicity/multicity_agent.rb', line 54

def self.price minutes
  price = minutes.to_f * 0.28
  price = 39.0 if price > 39.0 && minutes < 1440
   
  if minutes > 1440
    price = 39.0 * (minutes/1440)
    price += 0.28 * (minutes%1440)
  end
  
  {:price =>  price.round(2), :unit => 'EUR' }
end

.user_agent=(val) ⇒ Object

Set the user agent



11
12
13
# File 'lib/multicity/multicity_agent.rb', line 11

def self.user_agent=val
  @@user_agent = val
end

Instance Method Details

#get_carsObject

Gets all cars in Berlin Returns an array of Multicity:Car



22
23
24
25
26
27
28
29
# File 'lib/multicity/multicity_agent.rb', line 22

def get_cars  
  data = @agent.get(@@options[:url_list], {}, nil, headers).body
  marker = data.scan(/"marker":\[(.*?)\],"/).first.join
  marker = marker.to_s.gsub('&nbsp;', ' ')
  marker = JSON.parse("[#{marker}]")
  marker.map! { |item| Car.new(item) }
  marker.select { |item| item.valid? }
end

#get_loading_spotsObject



47
48
49
50
51
52
# File 'lib/multicity/multicity_agent.rb', line 47

def get_loading_spots
  data = @agent.get(@@options[:url_load_stations], {}, nil, headers).body
  data = encode data
  marker = JSON.parse(data)
  marker["marker"].map! {|item| Spot.new(item)}
end

#get_operation_areaObject

Gets the Operation Area for Berlin Returns an Array of coordinate-Arrays that creates a Polygon Example: [ [13.405852,52.464874], [13.425508,52.466625], … ]



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/multicity/multicity_agent.rb', line 34

def get_operation_area
  data = @agent.get(@@options[:url_area], {}, nil, headers).body
  data = XmlSimple.xml_in(data)
  coordinates = data["Document"].first["Placemark"].first["Polygon"].first["outerBoundaryIs"].first["LinearRing"].first["coordinates"].first
  coords = []
  coordinates.strip.split("\n").each do |coordinate|
    coordinate = coordinate.split ","
    coords << [ coordinate[1].to_f, coordinate[0].to_f ]
  end
  
  coords
end