Project

scb

0.0
No commit activity in last 3 years
No release in over 3 years
A small API client for the SCB API.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
~> 4.7
>= 0
>= 0
 Project Readme

SCB

A small API client for the SCB API.

You probably want to read the API_beskrivning.pdf (in Swedish)

Build Status

Installation

Add this line to your application's Gemfile:

gem 'scb'

And then execute:

$ bundle

Or install it yourself as:

$ gem install scb

Usage

It is probably a good idea to take a look in specs

Configuration of the API client is done automatically, but it is also possible to manually compose the objects:

# Manual composition
config = SCB::API.config.new
api    = SCB::API.new(config)
db     = SCB::DB.new(api)
table  = db.table('ProdbrEl')

# Short form
table = SCB.table('ProdbrEl')

# Liquid renewable energy in 2009
table.query Bransle: 925, Tid: 2009

Configuration

Nothing needs to be configured out of the box, but you can change the api_host, api_name, api_version, language, database, http_client and json_parser settings like this:

# English config
config = SCB::API::Config.new do |c|
  c.language = 'en'
end

# You can also pass a block straight to SCB.api
SCB.api { |c| c.api_host = 'scb.test' } 

Database

The database is divided into levels/sub-levels and tables.

The default language is 'sv' but there is also some data unde 'en'

db = SCB.db

# The names of All the top levels
db.levels.map(&:name)

# The English title of LonArb07Privat
SCB.db.en.table('LonArb07Privat').title

Level

The database levels can contain sub-levels or tables. You are probably more interested in the actual tables and their data.

# All the levels below BO
SCB.level('BO').levels

# All the tables below HE0110A
SCB.level('HE0110A').tables

Table

This is probably where you want to spend most of your time.

You can ask the table for possible variables in order to construct your query. I’ve implemented a simplified query format along the lines: Key: value where value is automatically turned into an Array if you only want a single value.

Note: Each value is also turned into a string.

# Database table containing data on electricity production
table = SCB.table('EltillfM')

# URI to the API endpoint for the table
table.uri

# The variable codes for the table
table.variables.map(&:code)

# Query: Wind production in January and February of 2013
table.query Prodslag: "Vind", Tid: ["2013M01", "2013M02"]

# Usage of the Internet
table = SCB.table('LE0108T05')

table.query Tid: 2012, Dem: "25-34"

# You can also get the result as a png
filename = '/tmp/internet_usage_2011.png'
query    = { AnvOmr: 80, Tid: 2011, ContentsCode: "LE0108A2" }

table.write_png_query(filename, query)

Note: write_png_query will not overwrite existing files.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request