Recently with friends we are building side project called “Midway” its a simple battleship server. The whole point of the game is to create a client and defeat other people maps! winner takes it all! The person with lowest number of hits/won-map wins! This is again a not to self type of entry. So i created some placeholder libs to building clients and while working through Erlang placeholder i made some notes on stuff.
Making request
So first of all i used builtin inets for making requests. So first thing to do is to start inets!
1
|
|
Ok i generalized using httpc to really simple thing
1 2 3 4 5 |
|
So if you will a call you will get something like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
One more thing i added was extracting body of the response
1
|
|
So after calling it on response we can get body
1 2 3 4 |
|
Parsing Json
So next thing to do was to parse json :) for this pupose use two libs mochijson2 or jiffy.
Mochijson2
So this one is easy to find and use. All you need to do is grab file from mochiweb project and compile it :) it has two method encode and decode.
1 2 |
|
One thing worth mentioning is that it expects input in a bit structured format.
So typycaly it will use “struct” symbol for json “{}” objects.
1
|
|
So array in json will be list but object will have to be in tuple with struct. It is awkward at first but you can get used to it. but there is easier thing to use…
Jiffy
This! There is a good example on main page.
1 2 3 4 5 6 7 |
|
This shows how nice it is to use :D no ‘struct’ pure love!
You can get it here https://github.com/davisp/jiffy
Summary
This is just note to self to not be checking up docs looking for this stuff again :).