MongoDB and Sinatra

I have been working a project to create a REST web service for testing purposes. Since I work for a company that uses Ruby and MongoDB that’s what I decided to use. Turns out the hard part wasn’t making Sinatra talk to the Mongo database, but was figuring out how to take the results and convert to a JSON format.

The ruby code is pretty simple, you have 5 required gems to install and 6 lines of code

!#/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
require 'haml'
require 'mongo'
require 'json'
DB = Mongo::Connection.new.db("local")
coll = DB.collection('skip')

get '/api/user/:val.json' do
   content_type "application/json"
    docs = coll.find({"id" => Integer(params[:val]), "type" => "user"}, :fields => {:_id => 0}).to_a
    test = Hash[*docs]
   test.to_json
end

I have looking up the user data by there ID which is passed in the URL and captured by :val This is passed as a search parameter to the MongoDB find along with the “type” which in this case is user. I don’t want the id created by MongoDB in the requests so I set the field to false in the return by setting :fields => {:_id => 0}. What you get back from MongoDB is BSON which look a lot like JSON in the MongoDB console, but it’s not! If you don’t take the results and convert them to a array using .to_a then you will get the database cursor value. There are couple more steps to take to massage the data into something for people to use. The array has to be turned into a ruby hash Hash[*whatever] (looks a lot like a C pointer). Then the hash can be turned into a JSON string using .to_json

You know you have been in Denmark too long when…

found here. 

 

You think there is no such thing as bad weather, only bad clothing.

You think its normal to pick up a girl in a pub, walk her to her bike and ride with her back home.

You understand why not every type of meat can be put together on top of bread.

You think its impolite to sit next to someone in a bus if there is a bench where you can sit on your own.

You go to the supermarket and buy three good beers and 10 not too good ones.

You can open a beer bottle with almost anything.

You honestly believe that the distance between Copenhagen and Aalborg is long.

You can tell the difference between a Grøn Tuborg and a Carlsberg beer

The first thing you do on entering a bank/post office/pharmacy etc. is to look for the queue number machine.

You accept that you will have to queue to take a queue number.

When a stranger on the street smiles at you, you assume that:
a. he is drunk;
b. he is insane;
c. he is American;
d. he is all of the above.

Silence is fun.

It no longer seems excessive to spend 800 kr. on alcohol in a single night.

You know that “religious holiday” means “let’s get pissed”.

You use “Mmmm” as conversation filler.

The word “yes” is an intake of breath.

You have only two facial expressions, smiling or blank

Traditional dinners may not necessarily mean a cooked meal.

You forget how to open canned beer.

Can’t remember when to say “please” and “excuse me”.

You will leave a pub if you can’t find a seat.

Your wardrobe no longer has suits but blue shirts and mustard colored sports jackets and lots of denim.

You don’t mind paying the same for a 200-metre bus ride as you do for going 10 kms.

You don’t look twice at businessmen in dark suits wearing white sport socks.

You start to believe that if it weren’t for Denmark’s efforts, the world would probably collapse pretty soon.

You find yourself more interested in the alcohol content than the name of the wine.

It feels natural to wear sport clothes and a backpack everywhere.

You know the meaning of life has something to do with the word “hyggelig”.

You are very surprised when you receive compliments about ANYTHING – including your appearance/clothing! In fact when you do, you find it suspicious and start thinking they might have ulterior motives.

You’ve completely forgotten what a “date” is – no one ever comes to pick you up and unexpected gifts are VERY unexpected.

You don’t think it strange that no one ever comes by to visit without being invited and you never show up at any one’s place unannounced either.

You wouldn’t dream of coming even 10 minutes early to a party. (Once around the block is always an alternative)

You find yourself lighting candles when you have guests – even if it is brightly sunny outside and 20 degrees.

You offer people strange-tasting brown alcoholic liquids with their coffee in the MORNING!

Don’t we love Denmark??!!

Why pre-crime will never work

This article points out the the error rate for pre-crime detection would be so high that it would be useless.

First, predictive software of this kind is undermined by a simple statistical problem known as the false-positive paradox. Any system designed to spot terrorists before they commit an act of terrorism is, necessarily, looking for a needle in a haystack. As the adage would suggest, it turns out that this is an incredibly difficult thing to do. Here is why: let’s assume for a moment that 1 in 1,000,000 people is a terrorist about to commit a crime. Terrorists are actually probably much much more rare, or we would have a whole lot more acts of terrorism, given the daily throughput of the global transportation system. Now lets imagine the FAST algorithm correctly classifies 99.99 percent of observations — an incredibly high rate of accuracy for any big data-based predictive model. Even with this unbelievable level of accuracy, the system would still falsely accuse 99 people of being terrorists for every one terrorist it finds. Given that none of these people would haveactually committed a terrorist act yet distinguishing the innocent false positives from the guilty might be a non-trivial, and invasive task.

Of course FAST has nowhere near a 99.99 percent accuracy rate. I imagine much of the work being done here is classified, but a writeup in Nature reported that the first round of field tests had a 70 percent accuracy rate. From the available material it is difficult to determine exactly what this number means. There are a couple of ways to interpret this, since both the write-up and theDHSdocumentation (all pdfs) are unclear. This might mean that the current iteration of FAST correctly classifies 70 percent of people it observes — which would produce false positives at an abysmal rate, given the rarity of terrorists in the population. The other way of interpreting this reported result is that FAST will call a terrorist a terrorist 70 percent of the time. This second option tells us nothing about the rate of false positives, but it would likely be quite high. In either case, it is likely that the false-positive paradox would be in full force for FAST, ensuring that any real terrorists identified are lost in a sea of falsely accused innocents.

 

Kurt Vonnegut’s response to the burning of Slaughterhouse-Five

well worth the read  a couple good quotes

 

If you were to bother to read my books, to behave as educated persons would, you would learn that they are not sexy, and do not argue in favor of wildness of any kind. They beg that people be kinder and more responsible than they often are. It is true that some of the characters speak coarsely. That is because people speak coarsely in real life. Especially soldiers and hardworking men speak coarsely, and even our most sheltered children know that. And we all know, too, that those words really don’t damage children much. They didn’t damage us when we were young. It was evil deeds and lying that hurt us.

I read in the newspaper that your community is mystified by the outcry from all over the country about what you have done. Well, you have discovered that Drake is a part of American civilization, and your fellow Americans can’t stand it that you have behaved in such an uncivilized way. Perhaps you will learn from this that books are sacred to free men for very good reasons, and that wars have been fought against nations which hate books and burn them. If you are an American, you must allow all ideas to circulate freely in your community, not merely your own.

If you and your board are now determined to show that you in fact have wisdom and maturity when you exercise your powers over the eduction of your young, then you should acknowledge that it was a rotten lesson you taught young people in a free society when you denounced and then burned books–books you hadn’t even read. You should also resolve to expose your children to all sorts of opinions and information, in order that they will be better equipped to make decisions and to survive.

Again: you have insulted me, and I am a good citizen, and I am very real.