camaras_valencia_es 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
7
+ environment_id="ruby-1.9.3-p0@camaras_valencia_es"
8
+
9
+ #
10
+ # First we attempt to load the desired environment directly from the environment
11
+ # file. This is very fast and efficicent compared to running through the entire
12
+ # CLI and selector. If you want feedback on which environment was used then
13
+ # insert the word 'use' after --create as this triggers verbose mode.
14
+ #
15
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
16
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]] ; then
17
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
18
+ else
19
+ # If the environment file has not yet been created, use the RVM CLI to select.
20
+ rvm --create "$environment_id"
21
+ fi
22
+
23
+ #
24
+ # If you use an RVM gemset file to install a list of gems (*.gems), you can have
25
+ # it be automatically loaded. Uncomment the following and adjust the filename if
26
+ # necessary.
27
+ #
28
+ # filename=".gems"
29
+ # if [[ -s "$filename" ]] ; then
30
+ # rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
31
+ # fi
32
+
33
+ #
34
+ # If you use bundler and would like to run bundle each time you enter the
35
+ # directory, you can uncomment the following code.
36
+ #
37
+ # # Ensure that Bundler is installed. Install it if it is not.
38
+ # if ! command -v bundle >/dev/null; then
39
+ # printf "The rubygem 'bundler' is not installed. Installing it now.\n"
40
+ # gem install bundler
41
+ # fi
42
+ #
43
+ # # Bundle while reducing excess noise.
44
+ # printf "Bundling your gems. This may take a few minutes on a fresh clone.\n"
45
+ # bundle | grep -v '^Using ' | grep -v ' is complete' | sed '/^$/d'
46
+ #
47
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in camaras_valencia_es.gemspec
4
+ gemspec
@@ -0,0 +1,47 @@
1
+ h1. camaras_valencia_es: Valencia's Traffic CCTV API Client
2
+
3
+ This is just a Ruby client to "http://camaras.valencia.es":http://camaras.valencia.es which allows to access the public data related to the CCTV monitoring Valencia's streets.
4
+ This library aims to make easier to gather and work with public data released by the Spanish Regional Goverment.
5
+
6
+ h2. Installation
7
+
8
+ <pre lang="bash">
9
+ $ gem install camaras_valencia_es
10
+ </pre>
11
+
12
+ Or if you are using bundler:
13
+
14
+ <pre lang="ruby">
15
+ gem 'camaras_valencia_es'
16
+ </pre>
17
+
18
+ h2. Usage
19
+
20
+ Docs are a work in progress.
21
+
22
+ <pre lang="ruby">
23
+ posts = CamarasValenciaEs::SurveillancePosts.all
24
+ posts.each do |post|
25
+ puts post.id
26
+ puts post.x # UTM coordinates properly parsed to Float
27
+ puts post.y
28
+
29
+ post.cameras.each do |camera|
30
+ puts camera.id
31
+ puts camera.x
32
+ puts camera.y
33
+ puts camera.source
34
+ puts camera.target
35
+ end
36
+ end
37
+ </pre>
38
+
39
+ h2. Software License
40
+
41
+ Copyright (c) 2012 Vicente Reig Rincón de Arellano
42
+
43
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
44
+
45
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
46
+
47
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "camaras_valencia_es/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "camaras_valencia_es"
7
+ s.version = CamarasValenciaEs::VERSION
8
+ s.authors = ["Vicente Reig Rincón de Arellano"]
9
+ s.email = ["vicente.reig@gmail.com"]
10
+ s.homepage = "http://github.com/vicentereig/camaras_valencia_es"
11
+ s.summary = %q{A client to access http://camaras.valencia.es traffic CCTV information.}
12
+ s.description = %q{A RESTful wrapper to the RESTful-like API at http://camaras.valencia.es to access traffic CCTV information.}
13
+
14
+ s.rubyforge_project = "camaras_valencia_es"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ s.add_runtime_dependency "httparty"
24
+ s.add_runtime_dependency "active_support"
25
+ end
@@ -0,0 +1,7 @@
1
+ require 'camaras_valencia_es/version'
2
+ require 'camaras_valencia_es/surveillance_post'
3
+ require 'camaras_valencia_es/surveillance_camera'
4
+
5
+ module CamarasValenciaEs
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,42 @@
1
+ require 'httparty'
2
+
3
+ module CamarasValenciaEs
4
+ class SurveillanceCamera
5
+ attr_accessor :id, :target, :address, :x, :y, :source, :type, :icon_type
6
+
7
+ def initialize(id, x, y, target, source, address, type, icon_type)
8
+ @id, @x, @y, @target, @source, @address, @type, @icon_type = id, x, y, target, source, address, type, icon_type
9
+ end
10
+
11
+ # TODO: DRY. Check SurveillancePost.parse
12
+ def self.parse(json)
13
+ defuck_coords = ->(v) {
14
+ CGI.unescape(v).gsub(/,/,".").to_f
15
+ }
16
+
17
+ serializable = JSON.parse(json)
18
+ # This JSON structure is fucking weird just for representing
19
+ # a simple array of objects. An Array of Hashe would have been more than enough.
20
+ # What they call latitude, and longitude is in fact UTM X/Y.
21
+ attrs = serializable['datos']
22
+ attrs['idcamara'].collect.with_index { |idgrupo, i|
23
+ x, y = defuck_coords.call(attrs['latitud'][i]), defuck_coords.call(attrs['longitud'][i])
24
+ target, source, address = CGI.unescape(attrs['destino'][i]), CGI.unescape(attrs['origen'][i]), CGI.unescape(attrs['direccion'][i])
25
+ self.new(attrs['idcamara'][i], x, y, target, source, address, attrs['tipo'][i], attrs['tipoicono'][i])
26
+ }
27
+ end
28
+
29
+ include HTTParty
30
+
31
+ base_uri 'http://camaras.valencia.es/web/'
32
+ headers 'Content-type' => 'application/json'
33
+
34
+ def self.all(post_id)
35
+ self.parse SurveillanceCamera.get('/infocamaras.asp', query: { idgrupo: post_id}).body
36
+ end
37
+
38
+ def image
39
+ CGI.unescape SurveillanceCamera.get('/imagen.asp', query: { idcamara: self.id}).body
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,65 @@
1
+ require 'active_support/core_ext/object/blank'
2
+ require 'httparty'
3
+ require 'cgi'
4
+ require 'json'
5
+
6
+ module CamarasValenciaEs
7
+ class SurveillancePost
8
+ attr_accessor :id, :x, :y, :icon_type, :street_name, :camera_count, :neighbourhood
9
+
10
+ def initialize(id, x, y, icon_type)
11
+ @id, @x, @y, @icon_type = id, x, y, icon_type
12
+ end
13
+
14
+ #attribute :id, type: String, match: 'idgrupo'
15
+ #attribute :x, type: defuck_coords, match: 'latitud'
16
+ #attribute :y, type: defuck_coords, match: 'longitud'
17
+ #attribute :icon_type, type: String, match: 'tipoicono'
18
+ #
19
+ #parser WhyDidntTheyUseProperJSONParser
20
+
21
+ def self.parse(json)
22
+ defuck_coords = ->(v) {
23
+ CGI.unescape(v).gsub(/,/,".").to_f
24
+ }
25
+
26
+ serializable = JSON.parse(json)
27
+ # This JSON structure is fucking weird just for representing
28
+ # a simple array of objects. An Array of Hashe would have been more than enough.
29
+ # What they call latitude, and longitude is in fact UTM X/Y.
30
+ attrs = serializable['datos']
31
+ attrs['idgrupo'].collect.with_index { |idgrupo, i|
32
+ x, y = defuck_coords.call(attrs['latitud'][i]), defuck_coords.call(attrs['longitud'][i])
33
+ self.new(attrs['idgrupo'][i], x, y, attrs['tipoicono'][i])
34
+ }
35
+ end
36
+
37
+ include HTTParty
38
+ base_uri 'http://camaras.valencia.es/web/'
39
+ headers 'Content-type' => 'application/json'
40
+
41
+ def self.all(opts={})
42
+ response_body = SurveillancePost.get('/infogrupos.asp', query: SurveillancePost.query_params(opts.delete(:bbox))).body
43
+ self.parse(response_body)
44
+ end
45
+
46
+ def find_street_name_and_neighbourhood_and_camera_count
47
+ response_body = SurveillancePost.get('/infogrupo.asp', query: {idgrupo: id}).body
48
+ response_body = response_body.split(/,/)
49
+ self.camera_count = response_body.pop.to_i
50
+ response_body = response_body.first.split(/-/)
51
+ self.neighbourhood = CGI.unescape response_body.pop || ""
52
+ self.street_name = CGI.unescape response_body.pop || ""
53
+ end
54
+
55
+ def cameras
56
+ @cameras ||= SurveillanceCamera.all(self.id)
57
+ end
58
+
59
+ protected
60
+ def self.query_params(bbox)
61
+ return {} unless bbox.present?
62
+ {lat1: bbox[0], long1: bbox[1], lat2: bbox[2], long2: bbox[3]}
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ module CamarasValenciaEs
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: camaras_valencia_es
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Vicente Reig Rincón de Arellano
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: &70328969763020 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70328969763020
25
+ - !ruby/object:Gem::Dependency
26
+ name: active_support
27
+ requirement: &70328969762460 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70328969762460
36
+ description: A RESTful wrapper to the RESTful-like API at http://camaras.valencia.es
37
+ to access traffic CCTV information.
38
+ email:
39
+ - vicente.reig@gmail.com
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - .gitignore
45
+ - .rvmrc
46
+ - Gemfile
47
+ - README.textile
48
+ - Rakefile
49
+ - camaras_valencia_es.gemspec
50
+ - lib/camaras_valencia_es.rb
51
+ - lib/camaras_valencia_es/surveillance_camera.rb
52
+ - lib/camaras_valencia_es/surveillance_post.rb
53
+ - lib/camaras_valencia_es/version.rb
54
+ homepage: http://github.com/vicentereig/camaras_valencia_es
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project: camaras_valencia_es
74
+ rubygems_version: 1.8.16
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: A client to access http://camaras.valencia.es traffic CCTV information.
78
+ test_files: []