Class: Bysykkel::Rack

Inherits:
Object
  • Object
show all
Defined in:
lib/bysykkel/rack.rb

Constant Summary collapse

RACKS_URL =
'http://smartbikeportal.clearchannel.no/public/mobapp/maq.asmx/getRacks'
RACK_URL =
'http://smartbikeportal.clearchannel.no/public/mobapp/maq.asmx/getRack?id=%s'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Rack

Returns a new instance of Rack.



12
13
14
15
16
# File 'lib/bysykkel/rack.rb', line 12

def initialize(attrs = {})
  attrs.each do |k,v|
    self.__send__("#{k}=", v)
  end
end

Instance Attribute Details

#empty_locksObject

Returns the value of attribute empty_locks.



10
11
12
# File 'lib/bysykkel/rack.rb', line 10

def empty_locks
  @empty_locks
end

#idObject

Returns the value of attribute id.



10
11
12
# File 'lib/bysykkel/rack.rb', line 10

def id
  @id
end

#latObject

Returns the value of attribute lat.



10
11
12
# File 'lib/bysykkel/rack.rb', line 10

def lat
  @lat
end

#lngObject

Returns the value of attribute lng.



10
11
12
# File 'lib/bysykkel/rack.rb', line 10

def lng
  @lng
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/bysykkel/rack.rb', line 10

def name
  @name
end

#onlineObject

Returns the value of attribute online.



10
11
12
# File 'lib/bysykkel/rack.rb', line 10

def online
  @online
end

#ready_bikesObject

Returns the value of attribute ready_bikes.



10
11
12
# File 'lib/bysykkel/rack.rb', line 10

def ready_bikes
  @ready_bikes
end

Class Method Details

.allObject

Query the Bysykkel XML API @ clearchannel.no for racks.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bysykkel/rack.rb', line 19

def self.all()
  raw = open(RACKS_URL)
  doc = Nokogiri::XML.parse raw
  racks_xml = Nokogiri::XML('<stations>' + doc.children[0].children[0].text + '</stations>')
  racks = racks_xml.xpath('//station').children.inject([]) do |ary, rack|
        ary << Rack.new({
          :id => rack.text.to_i
        }) unless rack.text.to_i >= 500
        ary
  end
  
  
  hydra = Typhoeus::Hydra.new( :max_concurrency => 6, :initial_pool_size => 5 )
  racks_all = []
  racks.each do |rack|
    req = Typhoeus::Request.new( RACK_URL % rack.id )
    req.on_complete do |response|
      doc = Nokogiri::XML.parse response.body
      parsed_rack = self.parse_rack(rack.id, Nokogiri::XML(doc.children[0].children[0].text).children[0])
      racks_all << parsed_rack unless parsed_rack.nil?
    end
    hydra.queue req
  end
  hydra.run
  racks_all
end

.find(id) ⇒ Object

Query the Bysykkel XML API @ clearchannel.no for a rack.



47
48
49
50
51
52
# File 'lib/bysykkel/rack.rb', line 47

def self.find(id)
  raw = open(RACK_URL % id )
  doc = Nokogiri::XML.parse raw
  parsed_rack = self.parse_rack(id, Nokogiri::XML(doc.children[0].children[0].text).children[0])
  return parsed_rack ? [parsed_rack] : []
end