scb 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,105 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative '../db'
4
+
5
+ module SCB
6
+ class DB
7
+ class Table
8
+ attr_reader :name
9
+ attr_accessor :api
10
+
11
+ def initialize(name, api = nil)
12
+ @name = name
13
+ @api = api || SCB.api
14
+ end
15
+
16
+ def query(simple_query, filter = "item")
17
+ json_query parse_simple_query(simple_query, filter)
18
+ end
19
+
20
+ def json_query(search_query = {})
21
+ body = post_query(search_query, "json")
22
+ api.load_json(body) unless body.nil?
23
+ end
24
+
25
+ def png_query(simple_query, filter = "item")
26
+ post_query parse_simple_query(simple_query, filter), "png"
27
+ end
28
+
29
+ def post_query(search_query, format = "json")
30
+ api_post(name, {
31
+ query: search_query,
32
+ response: {
33
+ format: format
34
+ }
35
+ })
36
+ rescue SCB::HTTP::Exception
37
+ raise SCB::DB::InvalidQuery, "Invalid query"
38
+ end
39
+
40
+ def title
41
+ data['title']
42
+ end
43
+
44
+ def uri
45
+ api_uri
46
+ end
47
+
48
+ def variables(klass = Variable)
49
+ @variables ||= data['variables'].map do |v|
50
+ klass.new(v)
51
+ end
52
+ end
53
+
54
+ def codes
55
+ variables.map(&:code)
56
+ end
57
+
58
+ def write_png_query(filename, simple_query, filter = "item")
59
+ write_file filename, png_query(simple_query, filter)
60
+ end
61
+
62
+ def write_png_query_raw(filename, search_query)
63
+ write_file filename, post_query(search_query, "png")
64
+ end
65
+
66
+ def inspect
67
+ name
68
+ end
69
+
70
+ def data
71
+ @data ||= api.get_and_parse(name) || []
72
+ end
73
+
74
+ private
75
+
76
+ def api_uri
77
+ api.uri(name)
78
+ end
79
+
80
+ def api_post(name, search_query)
81
+ api.post(name, search_query)
82
+ end
83
+
84
+ def write_file(filename, data)
85
+ unless File.exist?(filename)
86
+ File.open(filename, 'wb') do |f|
87
+ f.write data
88
+ end
89
+ end
90
+ end
91
+
92
+ def parse_simple_query(simple_query, filter)
93
+ simple_query.map do |q|
94
+ {
95
+ code: q[0].to_s,
96
+ selection: {
97
+ filter: filter,
98
+ values: Array(q[1]).map(&:to_s)
99
+ }
100
+ }
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+
3
+ module SCB
4
+ class DB
5
+ class Variable
6
+ attr_reader :data
7
+
8
+ def initialize(data)
9
+ @data = data
10
+ end
11
+
12
+ def code
13
+ data["code"]
14
+ end
15
+
16
+ def text
17
+ data["text"]
18
+ end
19
+
20
+ def values
21
+ data["values"]
22
+ end
23
+
24
+ def values_hash
25
+ @values_hash ||= Hash[values.zip(value_texts)]
26
+ end
27
+
28
+ def value_texts
29
+ data["valueTexts"]
30
+ end
31
+
32
+ def elimination?
33
+ !!data["elimination"]
34
+ end
35
+
36
+ def time?
37
+ !!data["time"]
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'net/http'
4
+
5
+ require_relative "version"
6
+
7
+ module SCB
8
+ class HTTP
9
+ HEADERS = {
10
+ 'User-Agent' => "RubyGem: scb (#{SCB::VERSION})"
11
+ }
12
+
13
+ class << self
14
+ def get(uri)
15
+ perform uri, Net::HTTP::Get.new(uri.request_uri, HEADERS)
16
+ end
17
+
18
+ def post(uri, body)
19
+ request = Net::HTTP::Post.new(uri.request_uri, HEADERS).tap do |r|
20
+ r.body = body
21
+ end
22
+
23
+ perform(uri, request)
24
+ end
25
+
26
+ private
27
+
28
+ def perform(uri, request)
29
+ Net::HTTP.start(uri.host, uri.port) do |http|
30
+ http.read_timeout = 60
31
+ response = http.request(request)
32
+ http.finish
33
+
34
+ if response.kind_of? Net::HTTPSuccess
35
+ response
36
+ else
37
+ raise Exception, response
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ class Exception < RuntimeError
44
+ attr_accessor :response
45
+ attr_reader :message
46
+
47
+ def initialize(response = nil)
48
+ @response = response
49
+ @message = response.message if response
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+
3
+ module SCB
4
+ VERSION = "0.0.1"
5
+ end
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'scb/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = "scb"
10
+ spec.version = SCB::VERSION
11
+ spec.authors = ["Peter Hellberg"]
12
+ spec.email = ["peter@c7.se"]
13
+ spec.summary = "A small API client for the SCB API."
14
+ spec.homepage = "https://github.com/peterhellberg/scb"
15
+ spec.license = "MIT"
16
+
17
+ spec.required_ruby_version = '>= 1.9.3'
18
+
19
+ spec.files = `git ls-files`.split($/)
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.test_files = spec.files.grep(%r{^spec/})
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "minitest", "~> 4.7"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "pry"
28
+ end
@@ -0,0 +1,6 @@
1
+ [
2
+ {
3
+ "dbid": "ssd",
4
+ "text": "Statistikdatabasen"
5
+ }
6
+ ]
@@ -0,0 +1,107 @@
1
+ [
2
+ {
3
+ "id": "AM",
4
+ "type": "l",
5
+ "text": "Arbetsmarknad"
6
+ },
7
+ {
8
+ "id": "BO",
9
+ "type": "l",
10
+ "text": "Boende, byggande och bebyggelse"
11
+ },
12
+ {
13
+ "id": "FM",
14
+ "type": "l",
15
+ "text": "Finansmarknad"
16
+ },
17
+ {
18
+ "id": "HE",
19
+ "type": "l",
20
+ "text": "Hushållens ekonomi"
21
+ },
22
+ {
23
+ "id": "JO",
24
+ "type": "l",
25
+ "text": "Jord- och skogsbruk, fiske"
26
+ },
27
+ {
28
+ "id": "LE",
29
+ "type": "l",
30
+ "text": "Levnadsförhållanden"
31
+ },
32
+ {
33
+ "id": "MI",
34
+ "type": "l",
35
+ "text": "Miljö"
36
+ },
37
+ {
38
+ "id": "NV",
39
+ "type": "l",
40
+ "text": "Näringsverksamhet"
41
+ },
42
+ {
43
+ "id": "PR",
44
+ "type": "l",
45
+ "text": "Priser och konsumtion"
46
+ },
47
+ {
48
+ "id": "TK",
49
+ "type": "l",
50
+ "text": "Transporter och kommunikationer"
51
+ },
52
+ {
53
+ "id": "AA",
54
+ "type": "l",
55
+ "text": "Ämnesövergripande statistik"
56
+ },
57
+ {
58
+ "id": "BE",
59
+ "type": "l",
60
+ "text": "Befolkning"
61
+ },
62
+ {
63
+ "id": "EN",
64
+ "type": "l",
65
+ "text": "Energi"
66
+ },
67
+ {
68
+ "id": "HA",
69
+ "type": "l",
70
+ "text": "Handel med varor och tjänster"
71
+ },
72
+ {
73
+ "id": "HS",
74
+ "type": "l",
75
+ "text": "Hälso- och sjukvård"
76
+ },
77
+ {
78
+ "id": "KU",
79
+ "type": "l",
80
+ "text": "Kultur och fritid"
81
+ },
82
+ {
83
+ "id": "ME",
84
+ "type": "l",
85
+ "text": "Demokrati"
86
+ },
87
+ {
88
+ "id": "NR",
89
+ "type": "l",
90
+ "text": "Nationalräkenskaper"
91
+ },
92
+ {
93
+ "id": "OE",
94
+ "type": "l",
95
+ "text": "Offentlig ekonomi"
96
+ },
97
+ {
98
+ "id": "SO",
99
+ "type": "l",
100
+ "text": "Socialtjänst"
101
+ },
102
+ {
103
+ "id": "UF",
104
+ "type": "l",
105
+ "text": "Utbildning och forskning"
106
+ }
107
+ ]
@@ -0,0 +1,57 @@
1
+ [
2
+ {
3
+ "id": "BO0102",
4
+ "type": "l",
5
+ "text": "Ombyggnad och rivning av flerbostadshus"
6
+ },
7
+ {
8
+ "id": "BO0201",
9
+ "type": "l",
10
+ "text": "Priser för nyproducerade bostäder"
11
+ },
12
+ {
13
+ "id": "BO0303",
14
+ "type": "l",
15
+ "text": "Outhyrda bostadslägenheter i flerbostadshus"
16
+ },
17
+ {
18
+ "id": "BO0406",
19
+ "type": "l",
20
+ "text": "Hyror i bostadslägenheter"
21
+ },
22
+ {
23
+ "id": "BO0601",
24
+ "type": "l",
25
+ "text": "Fastighetstaxeringar"
26
+ },
27
+ {
28
+ "id": "BO0101",
29
+ "type": "l",
30
+ "text": "Nybyggnad av bostäder"
31
+ },
32
+ {
33
+ "id": "BO0104",
34
+ "type": "l",
35
+ "text": "Bostadsbestånd (kalkylerat)"
36
+ },
37
+ {
38
+ "id": "BO0301",
39
+ "type": "l",
40
+ "text": "Intäkts- och kostnadsundersökning för flerbostadshus (IKU)"
41
+ },
42
+ {
43
+ "id": "BO0404",
44
+ "type": "l",
45
+ "text": "Avgifter/hyror för nybyggda lägenheter"
46
+ },
47
+ {
48
+ "id": "BO0501",
49
+ "type": "l",
50
+ "text": "Fastighetspriser och lagfarter"
51
+ },
52
+ {
53
+ "id": "BO0701",
54
+ "type": "l",
55
+ "text": "Bygglovsstatistik för bostäder och lokaler"
56
+ }
57
+ ]
@@ -0,0 +1,47 @@
1
+ [
2
+ {
3
+ "id": "LagenhetOmbAkBpAtAr",
4
+ "type": "t",
5
+ "text": "Ombyggda lägenheter i flerbostadshus med statligt stöd i riket efter åtgärd, byggnadsperiod och ägarkategori. År 1989 - 2008"
6
+ },
7
+ {
8
+ "id": "LagenhetOmbAtgardAr",
9
+ "type": "t",
10
+ "text": "Ombyggnad av flerbostadshus, tillskott av lägenheter, i riket efter åtgärd, ägarkategori, byggnadsperiod, lägenhetstyp och med/utan statligt stöd. År 1989 - 2012"
11
+ },
12
+ {
13
+ "id": "LagenhetOmbStodtypAr",
14
+ "type": "t",
15
+ "text": "Ombyggda lägenheter i flerbostadshus i riket efter ägarkategori, byggnadsperiod, lägenhetstyp och med/utan statligt stöd. År 1989 - 2008"
16
+ },
17
+ {
18
+ "id": "LagenhetRivAkBpLtRAr",
19
+ "type": "t",
20
+ "text": "Rivning av lägenheter i flerbostadshus efter region, ägarkategori, byggnadsperiod, lägenhetstyp och rivningsorsak. År 1989 - 2012"
21
+ },
22
+ {
23
+ "id": "LagenhetOmbAkBpAr",
24
+ "type": "t",
25
+ "text": "Ombyggd bostadsarea i flerbostadshus efter region, ägarkategori och byggnadsperiod. År 1989 - 2012"
26
+ },
27
+ {
28
+ "id": "LagenhetOmbAkBpLtAr",
29
+ "type": "t",
30
+ "text": "Ombyggda lägenheter i flerbostadshus efter region, ägarkategori, byggnadsperiod och lägenhetstyp. År 1989 - 2012"
31
+ },
32
+ {
33
+ "id": "LagenhetOmbKv",
34
+ "type": "t",
35
+ "text": "Lägenheter i ombyggda flerbostadshus efter region. Preliminära uppgifter. Kvartal 1989K1 - 2012K4"
36
+ },
37
+ {
38
+ "id": "LagenhetOmbUpplatAr",
39
+ "type": "t",
40
+ "text": "Ombyggda lägenheter i flerbostadshus med statligt stöd efter region och upplåtelseform. År 1989 - 2008"
41
+ },
42
+ {
43
+ "id": "LagenhetRivKvalAr",
44
+ "type": "t",
45
+ "text": "Rivning av lägenheter i flerbostadshus efter region och kvalitetsgrupp. År 1989 - 2011"
46
+ }
47
+ ]