Planet Ruby Gem of the Week

Thursday, April 23, 2015

Ruby Gem of the Week

Week #17 - datapak gem - work with tabular data packages (.csv files w/ datapackage.json) using SQLite (w/ ActiveRecord) 00:00 • over a year ago

Let’s say you want to share your data with the world or use the data that others share with the world. How to get started? A pragmatic way is to use tabular data packages.

What’s a tabular data package?

Tabular Data Package is a simple structure for publishing and sharing tabular data with the following key features:

  • Data is stored in CSV (comma separated values) files
  • Metadata about the dataset both general (e.g. title, author) and the specific data files (e.g. schema) is stored in a single JSON file named datapackage.json which follows the Data Package format

(Source: Tabular Data Packages, Open Knowledge Foundation)

Here’s a minimal example of a tabular data package holding two

Thursday, April 16, 2015

Ruby Gem of the Week

Week #16 - tilt gem - let's build (yet another) micro web framework in less than 33 lines of code 00:00 • over a year ago

Do you think the Ruby on Rails web framework is a massive monster - more than 10,000+ lines of code just for ActionPack - not counting ActiveModel, ActiveRecord, ActiveJob, ActiveResource and what not? Or do you think that even the “classic” Sinatra web machinery clocking in at more than 1,000+ lines of code is more macro than micro?

Why not write a “real” micro web framework inspired by Konstantin Haase’s Almost Sinatra hack that ships as a 6(!) lines of code pastie:

almost_sinatra.rb:

%w.rack tilt date INT TERM..map{|l|trap(l){$r.stop}rescue require l};$u=Date;$z=($u.new.year + 145).abs;puts "== Almost Sinatra/No Version has taken the stage on #$z for development with backup f

Thursday, April 9, 2015

Ruby Gem of the Week

Week #15 - beerdb gem - serving a Guinness Irish Stout or a Bamberg Aecht Schlenkerla Rauchbier Märzen as JSON w/ Ruby 00:00 • over a year ago

What’s the beerdb gem?

The beerdb gem offers a ready-to-use database schema (in SQL) and models such as - surprise, surprise - Beer, Brewery, Brand and friends (using the ActiveRecord object-relational mapper machinery). Example:

Let’s try the brewery model:

by = Brewery.find_by( key: 'guinness' )

by.title
=> 'St. James's Gate Brewery / Guinness Brewery'

by.country.key
=> 'ie'

by.country.title
=> 'Ireland'

by.city.title
=> 'Dublin'

by.beers.first
=> 'Guinness', 4.2

...

Or let’s try the beer model:

b = Beer.find_by( key: 'guinness' )

b.title
=> 'Guinness'

b.abv    # that is, alcohol by volume (abv)
=> 4.2

b.tags
=> 'irish_dry_stout', 'dr

Thursday, April 2, 2015

Ruby Gem of the Week

Week #14 - html-proofer gem - auto-proofread (check and validate) your hypertext (HTML) pages 00:00 • over a year ago

Let’s say you have a project site and want to check that all your hypertext (HTML) pages open and close all tags according to the specs, that all internal (e.g. href="#datasets") and external (e.g. href="openbeer.github.io") links are working (no 404’s not found errors etc.), that all images include an alternative text (alt="Database Schema") attribute and so on.

What’s the html-proofer gem?

Let’s thank Garen Torikian and friends who have bundled up all these checks and more in a ready-to-use gem, that is, html-proofer.

Let’s try it on the Open Mundi (world.db) project site:

_site
|-- i
|   |-- sqlitestudio.png
|   |-- worlddb-models-place.png
|   `-- worlddb-models.png
|-- build.html
|-- index.html
`-- style.css


Thursday, March 26, 2015

Ruby Gem of the Week

Week #13 - props gem - yet another config (INI) reader in Ruby 00:00 • over a year ago

Let’s say you’re looking for an easy configuration file format for your tool or service. Why not use the Windows-inspired INI format as an (easier and simpler) alternative to the popular JSON, YAML and TOML formats? Example:

ruby.conf:

title = Planet Ruby

[rubylang]
  title = Ruby Lang News
  link  = www.ruby-lang.org/en/news
  feed  = www.ruby-lang.org/en/feeds/news.rss

[rubyonrails]
  title = Ruby on Rails News
  link  = weblog.rubyonrails.org
  feed  = weblog.rubyonrails.org/feed/atom.xml

[viennarb]
  title = Vienna.rb News
  link  = vienna-rb.at
  feed  = vienna-rb.at/atom.xml

Now all that’s missing is a gem that reads in the Windows-inspired INI format and returns a plain “old” Ruby hash.

What’s the props gem?

Using

Thursday, March 19, 2015

Ruby Gem of the Week

Week #12 - logutils gem - yet another (lightweight, simple) logging library in Ruby 00:00 • over a year ago

Sooner or later you will debug your code and add some print statements to help you along. Example:

puts "[debug] value: #{value}"
puts "[debug] value.class.name: #{value.class.name}

A “better” way is to use a logger that lets you turn off and on your debug messages as needed and why not a add some more message classes for errors or warnings, for example?

The “classic” message class hierarchy reads:

  • [OFF]
  • FATAL
  • ERROR
  • WARN
  • INFO
  • DEBUG
  • [ALL]
What’s the logutils gem?

Using the logutils gem - yet another logging library - you can print all messages and more. Start by getting a logger e.g.:

logger = LogUtils::Logger.new

An

Thursday, March 12, 2015

Ruby Gem of the Week

Week #11 - worldlite gem - lightweight public domain country data (all data included as good ol' ruby code) 00:00 • over a year ago

Ever wondered how many countries are in the world? 222? 204? 196? 193? It all depends - of course - what’s your definition of country is. The United Nations lists 193 countries (as members), the World Football Association (that is, FIFA) lists 209 countries (as members) plus 13 more countries (as members of continental federations only), or the Olympics Committee lists 204 countries or, the ISO 3166 country code standard lists 196 countries, for example, and so on. Now lets say you need a country list for all FIFA members for the World Cup Qualifiers 2018 in Russia in Ruby?

What’s the worldlite gem?

Let’s welcome the worldlite gem that includes “lightweight” public domain country data - that is, all data gets included as good ol’ ruby code - no database, no dependencies, no copy

Thursday, March 5, 2015

Ruby Gem of the Week

Week #10 - annotate gem - annotate your ActiveRecord models with comments about your table structure 00:00 • over a year ago

Magic. ActiveRecord models can be as simple as:

class Beer < ActiveRecord::Base
end

or

class Brewery < ActiveRecord::Base
end

Some may find that’s a little too much magic. Where’s the code? What attribute can you use?

By default ActiveRecord models require no information on the database tables wrapped (it all works - thanks to convention over configuration, that is, the class Beer (singular noun), for example, gets mapped to the table beers (plural noun) and Brewery to breweries and so on.

Best of both worlds. Less code is great and it’s easy to update the model - just update the table - there are no out-of-date setter and getters duplicated in t

Thursday, February 26, 2015

Ruby Gem of the Week

Week #9 - state_machine(s) gem - model processes and work flows with finite state machines (FSM) and automata theory 00:00 • over a year ago

What’s a state machine? What’s a finite state machine (FSM)?

A state machine or finite state machine (FSM) lets you model a traffic light, a shopping cart checkout procedure, a conference proposal process, a credit card transaction and many more “real world” tasks using 1) states, 2) events, and 3) transitions. Or to quote the Wikipedia:

A finite-state machine (FSM) or finite-state automaton (plural: automata), or simply a state machine, is a mathematical model of computation used to design both computer programs and sequential logic circuits. It is conceived as an abstract machine that can be in one of a finite number of states. The machine is in only one state at a time; the state it is in at any given time is called the current state. It can change from one sta

Thursday, February 19, 2015

Ruby Gem of the Week

Week #8 - rails-erd gem - generate entity-relationship diagrams (ERD) for your activerecord models 00:00 • over a year ago

Let’s say you have defined your database schema (tables) with ActiveRecord in Ruby. Example:

create_table :breweries do |t|
  t.string  :key,    null: false
  t.string  :title,  null: false
  t.string  :address
  t.string  :web
end

create_table :beers do |t|
  t.references :brewery
  t.string  :key,     null: false
  t.string  :title,   null: false
  t.text    :comments
end

And your models with classes in Ruby and assocations with class macros such as belongs_to, has_many, and so on:

class Beer < ActiveRecord::Base
  belongs_to :brewery
end

class Brewery < ActiveRecord::Base
  has_many   :beers
end

How can you auto-generate an entity-relationship diagram? For example:

Thursday, February 12, 2015

Ruby Gem of the Week

Week #7 - gli gem - git-like interfaces for awesome command-line tools 00:00 • over a year ago

What’s OptionParser?

Ruby ships with a built-in class, that is, OptionParser that lets you define and parse options for your command line tool. Let say you’re building a command line tool for the open beer database and as options you want to offer a switch to turn on debug messages e.g. -v or --verbose and another switch to change the database name from the default beer.db to lets say pivo.db using -n pivo.db or --dbname=pivo.db.

A minimal version with the built-in OptionParser looks like:

require 'optparse'

config = { name: 'beer.db' }

parser = OptionParser.new do |opts|
  opts.banner = "Usage: beerdb [OPTS]"

  opts.on("-v", "--verbose",

Thursday, February 5, 2015

Ruby Gem of the Week

Week #6 - schemadoc gem - auto-generate your database schema docs for tables, columns, etc. 00:00 • over a year ago

What’s schemadoc?

The schemadoc gem includes a ready-to-use command line tool named - surprise, surprise - schemadoc that lets you auto-generate your database schema documentation for tables, columns, and more.

Overview. The schemadoc tool connects to your database (e.g. SQLite, PostgreSQL, etc.) and writes out the schema info in database.json

{
  "schemas": [
    {
      "name": "football",
      "tables": [
        {
          "name": "alltime_standing_entries",
          "columns": [
            {
              "name": "id",
              "type": "integer",
              "default": null,
              "null": false
            },
            {
              "name": "alltime_standing_id",
              "type": "integer",
              "default": nu

Thursday, January 29, 2015

Ruby Gem of the Week

Week #5 - feedparser gem - web feed parser and normalizers (for RSS 2.0, Atom, n friends) 00:00 • over a year ago

What’s a Web Feed?

A web feed (or news feed) is a simple document/data format that 1) lets you publish a list of status updates, blog postings, articles, pictures, cartoons, recordings, etc. and that 2) lets others subscribe to your updates.

Example:

<feed>
  <title>beer.db - Open Beer, Brewery n Brewpubs Data News and Updates</title>
  <link href="openbeer.github.io/" />
  <updated>2015-01-04T09:25:14+00:00</updated>
  <entry>
    <title>Beer-A-Day - Open Data Eternal Page-A-Day Calender</title>
    <link href="openbeer.github.io/2014/12/12/beer-a-day.html" />
    <updated>2014-12-12T00:00:00+00:00</updated>
    <summary>As an example of using the beer.db...</summary>
  </ent

Thursday, January 22, 2015

Ruby Gem of the Week

Week #4 - kramdown gem - turn easy-to-read and easy-to-write wiki-style plain text in markdown into hypertext 00:00 • over a year ago

What’s Markdown?

Markdown is an easy-to-write and easy-to-read wiki-style markup language that lets you author web pages in plain text. Example:

## What's `football.db`?

A free and open public domain football database & schema
for use in any (programming) language (e.g. uses plain datasets). Example:

    ### Teams
    
    barcelona, Barcelona|FC Barcelona|Fútbol Club Barcelona, BAR
    madrid,    Real Madrid|Real Madrid CF,                   RMD
    ...

becomes

<h2>What's <code>football.db</code>?</h2>

<p>A free and open public domain football database &amp; schema
for use in any (programming) language (e.g. uses plain datasets). Example:</p>

<pre><code>
### Teams

barcelona, B

Thursday, January 15, 2015

Ruby Gem of the Week

Week #3 - slideshow gem - a free web alternative to PowerPoint and Keynote in Ruby 00:00 • over a year ago

What’s Slide Show (S9)?

A Ruby gem that lets you create slide shows and author slides in plain text using a wiki-style markup language that’s easy-to-write and easy-to-read. The Slide Show (S9) project also collects and welcomes themes and ships “out-of-the-gem” with built-in support for “loss-free” gradient vector graphics themes.

Getting Started in 1-2-3 Easy Steps
  • Step 1: Author your slides in plain text using a wiki-style markup language
  • Step 2: Build your slide show using the slideshow gem
  • Step 3: Open up your slide show (web page) in your browser and hit the space bar to flip through your slides
  • That’s it. Showtime!
Step 1: Author your slides in plain text using a wiki-style markup language

Slide Show l

Thursday, January 8, 2015

Ruby Gem of the Week

Week #2 - hoe gem - build, package and publish gems with hoe rake tasks 00:00 • over a year ago

What’s Hoe?

Hoe is a gem that bundles ready-to-use rake tasks to help you build, package and publish your own gems. Thanks to Ryan Davis and friends (from Seattle.rb) for polishing the gem all those years - more than 100+ releases - leading to today’s version 3.13.0.

Let’s create a bare bones gem (hellohoe) and publish it on RubyGems.org.

Set up your gem by adding a build script, readme, change log and manifest

To use Hoe together with your own code use the following structure:

/hellohoe
  + README.txt        - Description in plain text 
  + History.txt       - Version change log in plain text
  + Manifest.txt      - List of all files to include in plain text
  + Rakefile          - Build script (requires your name and email)  
  + /lib
  

Thursday, January 1, 2015

Ruby Gem of the Week

Week #1 - factbook gem - turn the world factbook into open structured data e.g JSON 00:00 • over a year ago

What’s the World Factbook?

The World Factbook [1][2] published by the Central Intelligence Agency (CIA) offers free country profiles in the public domain (that is, no copyright(s), no rights reserved).

  • [1] The World Factbook
  • [2] Wikipedia Article: The World Factbook
What’s the factbook gem?

The factbook gem ships with scripts for the world factbook that let you turn web pages into open structured data e.g JSON and friends. Example:

To get the country profile page for Austria as a hash (that is, structured data e.g. nested key/values) use:

page = Factbook::Page.new( 'at' )   # at is the country code for Austria
pp page.data                        # pretty print hash

To save the hash to disk as JSON use

pluto.models/1.4.0, feed.parser/1.0.0, feed.filter/1.1.1 - Ruby/2.0.0 (2014-11-13/x86_64-linux) on Rails/4.2.0 (production)