AtelierClockwork

Yet More Refinement

December 23, 2014

I’ve made a lot of progress making the iTunes result parser significantly better at handling undocumented keys while still producing attractive output. I wrote a custom function to split a string based on camelcase, and now I’m working on a system to sort keys into sections by the first work on the split.

That being said, I also took another pass at my query building system, and thanks to some clever use of protocols, I’ve made the overall code a lot cleaner.

func compact<T>(arr_in:[T?]) -> [T]{
  return arr_in.filter{ return $0? != nil }.map{ return $0!}
}

The compact function now uses filter and map to remove any empty entries then forcibly unwrap all of the remaining values. It’s cleaner to read and should behave well.

protocol ItunesQueryItem{
  var key:ItunesQueryKey {get}
  var value:String {get}
}

I had a protocol defined before, but I wasn’t making the best use of it, and didn’t see a point to showing it before, but now I’m using it more effectively, so seeings the (very simple) structure may be useful here.

private func printQueries(queries:[ItunesQueryItem]) -> String{
  if(queries.isEmpty){
    return ""
  }
  return "?" + "&".join(queries.map{ v in "\(v.key.rawValue)=\(v.value)" })
}

private var queryBody:String {
  return printQueries(compact([term, media, safeEntities, safeAttribute,
                               limit, country, isExplicit, language]))
}

This part of the code is a lot slimmer than my previous best, largely because I restructured the class to publicly expose the variables that backed the key storage rather than keeping them class internal and only allowing getters and setters.

I hid them inside the class to keep the public details “simpler,” but I realized that they were largely complicating the class internally and that I could control a lot of the potential issues by making better use of immutable state and structs.

I’m now pretty much done with the query code, and closing in on done with the response parsing code. The next sections are making the CLI app feel right, then fleshing out the Playground based documentation that I built.