Class: CamarasValenciaEs::SurveillancePost
- Inherits:
-
Object
- Object
- CamarasValenciaEs::SurveillancePost
- Includes:
- Util::LatLonConverter, Util::V8Unescaper, HTTParty
- Defined in:
- lib/camaras_valencia_es/surveillance_post.rb
Instance Attribute Summary collapse
-
#camera_count ⇒ Object
Returns the value of attribute camera_count.
-
#icon_type ⇒ Object
Returns the value of attribute icon_type.
-
#id ⇒ Object
Returns the value of attribute id.
-
#neighbourhood ⇒ Object
Returns the value of attribute neighbourhood.
-
#street_name ⇒ Object
Returns the value of attribute street_name.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Class Method Summary collapse
- .all(opts = {}) ⇒ Object
-
.parse(json) ⇒ Object
attribute :id, type: String, match: ‘idgrupo’ attribute :x, type: defuck_coords, match: ‘latitud’ attribute :y, type: defuck_coords, match: ‘longitud’ attribute :icon_type, type: String, match: ‘tipoicono’.
Instance Method Summary collapse
- #cameras ⇒ Object
- #cameras_to_attributes ⇒ Object
-
#initialize(id, x, y, icon_type) ⇒ SurveillancePost
constructor
A new instance of SurveillancePost.
- #load_attributes ⇒ Object
- #to_attributes ⇒ Object
Methods included from Util::V8Unescaper
Constructor Details
#initialize(id, x, y, icon_type) ⇒ SurveillancePost
Returns a new instance of SurveillancePost.
14 15 16 17 |
# File 'lib/camaras_valencia_es/surveillance_post.rb', line 14 def initialize(id, x, y, icon_type) @id, @x, @y, @icon_type = id, x, y, icon_type convert_to_lat_long end |
Instance Attribute Details
#camera_count ⇒ Object
Returns the value of attribute camera_count.
12 13 14 |
# File 'lib/camaras_valencia_es/surveillance_post.rb', line 12 def camera_count @camera_count end |
#icon_type ⇒ Object
Returns the value of attribute icon_type.
12 13 14 |
# File 'lib/camaras_valencia_es/surveillance_post.rb', line 12 def icon_type @icon_type end |
#id ⇒ Object
Returns the value of attribute id.
12 13 14 |
# File 'lib/camaras_valencia_es/surveillance_post.rb', line 12 def id @id end |
#neighbourhood ⇒ Object
Returns the value of attribute neighbourhood.
12 13 14 |
# File 'lib/camaras_valencia_es/surveillance_post.rb', line 12 def neighbourhood @neighbourhood end |
#street_name ⇒ Object
Returns the value of attribute street_name.
12 13 14 |
# File 'lib/camaras_valencia_es/surveillance_post.rb', line 12 def street_name @street_name end |
#x ⇒ Object
Returns the value of attribute x.
12 13 14 |
# File 'lib/camaras_valencia_es/surveillance_post.rb', line 12 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
12 13 14 |
# File 'lib/camaras_valencia_es/surveillance_post.rb', line 12 def y @y end |
Class Method Details
.all(opts = {}) ⇒ Object
46 47 48 49 |
# File 'lib/camaras_valencia_es/surveillance_post.rb', line 46 def self.all(opts={}) response_body = SurveillancePost.get('/infogrupos.asp', query: SurveillancePost.query_params(opts.delete(:bbox))).body self.parse(response_body) end |
.parse(json) ⇒ Object
attribute :id, type: String, match: ‘idgrupo’ attribute :x, type: defuck_coords, match: ‘latitud’ attribute :y, type: defuck_coords, match: ‘longitud’ attribute :icon_type, type: String, match: ‘tipoicono’
parser WhyDidntTheyUseProperJSON
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/camaras_valencia_es/surveillance_post.rb', line 26 def self.parse(json) defuck_coords = ->(v) { CGI.unescape(v).gsub(/,/,".").to_f } serializable = JSON.parse(json) # This JSON structure is fucking weird just to implement # a simple array of objects. An Array of Hashe would have been more than enough. # What they call latitude, and longitude is in fact UTM X/Y. attrs = serializable['datos'] attrs['idgrupo'].collect.with_index { |idgrupo, i| x, y = defuck_coords.call(attrs['longitud'][i]), defuck_coords.call(attrs['latitud'][i]) self.new(attrs['idgrupo'][i], x, y, attrs['tipoicono'][i]) } end |
Instance Method Details
#cameras ⇒ Object
63 64 65 |
# File 'lib/camaras_valencia_es/surveillance_post.rb', line 63 def cameras @cameras ||= SurveillanceCamera.all(self.id) end |
#cameras_to_attributes ⇒ Object
80 81 82 |
# File 'lib/camaras_valencia_es/surveillance_post.rb', line 80 def cameras_to_attributes self.cameras.map(&:to_attributes) end |
#load_attributes ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/camaras_valencia_es/surveillance_post.rb', line 51 def load_attributes response_body = SurveillancePost.get('/infogrupo.asp', query: {idgrupo: id}).body body_parts = response_body.split(/,/) self.camera_count = body_parts.pop.to_i body_parts = body_parts.first.split(/-/) self.neighbourhood = unescape(body_parts.pop) self.street_name = unescape(body_parts.pop) response_body end |
#to_attributes ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/camaras_valencia_es/surveillance_post.rb', line 67 def to_attributes { remote_id: self.id, location: [self.latlon.y, self.latlon.x], address: { street_name: (self.street_name || "").strip, neighbourhood_name: (self.neighbourhood || "").strip }, surveillance_cameras: self.cameras_to_attributes } end |