Working with Hub

Hub is a cli for github.

I mostly use it for creating pull-requests or to navigate to the github page for the current repository.

It can be used for more, including calling github api’s

Here is an example of fetching a file from a repo (which would work based upon the credentials attached to hub).

hub api -X GET /repos/chriseyre2000/sample-readme/contents/README.md -H "Accept: application/vnd.github.VERSION.raw"

This fetches the content of the file.

Intend to use this to analyse the package.json of a range of projects.

These will be passed into https://github.com/chriseyre2000/package_compare so that I can load a neo4j database. This will allow me to analyse the package dependencies.

Microservices can result in a lot of packages ….

This is a script that can be used to load a node package.json from github into a local neo4j database:

#/bin/bash
owner=$1
project=$2

hub api -X GET /repos/$owner/$project/contents/package.json -H "Accept: application/vnd.github.VERSION.raw" > $project.json
package_compare $project.json localhost neo4j magicbeans

Metaprogramming Elixir – Part One

This is a work through an discussion of Metaprogramming Elixir.

The entry point of metaprogramming in Elixir is the quite macro.

quote do: 1 + 2
{:+, [context: Elixir, import: Kernel], [1, 2]}

This allows the transformation of a code block into the abstract syntax tree (AST). This can then be manipulated like any other data structure and return to the generated code. The practical upshot of this is that half of the Elixir language is actually macros. You can if you need to write your own macros to perform complex operations simply.

There are recommendations to limit the use of macros to things that you can’t solve with normal functions. It’s possible with macros to create a powerful equivalent of inheritance, but must be used sparingly to avoid magical code.

Most of this work takes place at compile time rather than at runtime.

You use require to import macros.

General warning: in languages that have implicit returns adding logging at the end of a function masks the result. Capture the result, log then return result.

unquote is a technique where a macro can read variables from the hosts context. This is how you pass parameters into a macro.

Simple Promises In Javascript

This is how to wrap a callback api in promises:

// Sample function with callback
function clientFactory() {
  return {
    request: (name, callback) => {
      callback(name)
    }
  }
}

let client = clientFactory()

client.request('a', console.log)

// Wrapping a callback into a promise

function returnCallbackResult(input) {
  let promise = new Promise(
    resolver => {
      let client = clientFactory()
      client.request(input, (a) => {
        resolver(a)
      })
  })
  return promise
}

returnCallbackResult('b').then( console.log )

// How to wrap a promise

function chainingPromise(input) {
  let promise = new Promise(
    resolver => {
      input.then( (a) => resolver(`wrapping ${a}`))
    }
  )
  return promise
}

chainingPromise( returnCallbackResult( 'c' ) ).then(console.log)