Dealing with NIL in JSON::Any in Crystal Lang

One of the challenges when developing with Crystal is that it is not as forgiving about data types as, say, Python or Ruby. nil will keep you occupied. The following error message: cast from Nil to String failed, at /usr/share/crystal/src/json/any.cr:220:5:220 can be solved by: # cast to string, THEN check if it is nil if…

Weiterlesen

How to use catch all routes with Kemal

For a Vue.js single page application – at least on a SUB URL of our page – we want a catch all on our server, which will always render the same template / HTML output to the browser. Kemal is internally based on Radix for routing: https://github.com/luislavena/radix Radix has a Catch All / Glob character:…

Weiterlesen

mongo_orm Exception: missing bson key: publish_acl (Exception)

When you extend your field definitions in the mongo_orm document classes, specifically with embedded documents, you might run into the following runtime (!) error: Exception: missing bson key: <name> (Exception) This is not an error in your code – your code is raising, because the database structure is not as it expects it to be!…

Weiterlesen

mongo_orm custom name for collection

mongo_orm will automatically determine the name for your collection by using the Module and Class name, and appending an “s”. In cases where you want to name your collection, you can simply do this: class PapiTest < Mongo::ORM::Document     collection_name “Buster”     field test : String end get “/” do     pt = PapiTest.new     pt.test = “I want…

Weiterlesen

Exception: Unexpected quote at 1:2 (CSV::MalformedCSVError) Crystal

While trying to parse an Amazon pay file, I stumbled across this particular problem: Exception: Unexpected quote at 1:2 (CSV::MalformedCSVError) The reason: Amazon Pay encodes it’s files in UTF-8 with BOM. The BOM is an optional (in UTF-8 files) marker. You can see it with the tool xxd for instance xxd apolish_amazon_2018DecMonthlyTransaction.csv | less The…

Weiterlesen