Monday, November 23, 2009

Classy hData

I've been working on a team that is looking at ways in which we can simplify the exchange of information in Health IT. This effort is called hData. We just released a new version of our packaging and network transport spec, and I would like to talk a bit about how we arrived at this version.

I think it is really important for IT specifications to have a reference implementation available. If you build a spec without code, it's really hard to see where you have gone wrong. To make sure we are on the right track, I built a small web application that implements the spec. I was able to quickly uncover some bugs in our work. Bugs I'm sure we would have missed by just reading the document.

Technology Choices

Since my preferred language of choice is Ruby, it would be natural to think I would want to tackle this project in Rails. However, in hData we make some good use of the HTTP Verbs, and I'm not so sure that they would line up seamlessly with Rails conventions. I decided to go with a much simpler choice. Sinatra is a small web framework that seems perfect for this job. It makes the HTTP Verbs central to your code, so it should be fairly obvious on how we go from the spec to implementation.

There are a few other tools that I used on this adventure. DataMapper was just right for the ORM needs of the project. I could have used ActiveRecord to persist data, but DataMapper has a really nice auto-migration feature, which will save me from writing all of the database creation code. I also used Bundler to manage my application's dependencies.

Getting Started

The best way to get started here is by taking a test driven approach to the spec. For that I will be using Shoulda and Rack Test. With my TDD tools in place, I can take part of the spec that looks like this:


3.1.2 POST

3.1.2.1 Parameters: type, typeId, requirement

For this operation, the value of type MUST equal "extension". The typeId MUST be a URI string that represents a type of section document. The requirement parameter MUST be either "optional" or "mandatory". If any parameters are incorrect or not existent, the server MUST return a status code of 400.

If the system supports the extension identified by the typeId URI string, this operation will modify the extensions node in the root document and add this extension with the requirement level identified by the requirement parameter. The server MUST return a 201 status code.

If the system does not support the extension, it MUST not accept the extension if the requirement parameter is "mandatory" and return a status code of 409. If the requirement is "optional" the server MAY accept the operation, update the root document and send a status code of 201.

Status Code: 201, 400, 409

and turn it into the a Shoulda context block. In the spec above, we're talking about what should happen when you POST to the root of an hData Record. The functionality being described here is how an extension can be added to the record, or how you can register a different type of thing for a record. For example, you could use this feature to add an medications extension to a record, if one did not exist there already. In our test code, we're going to try to register an allergies extension:

As you can see from the code, the combination of Shoulda and Rack Test make it really easy to express the requirements set forth in the specification. The first test tries to POST and incomplete request and should receive an error. The second sends a properly formed request and should get an appropriate response. The last test tries to POST a duplicate extension.

With the tests in place, we can move on to implementation.

I have created a DataMapper Resource to capture all of the information we want to store about an extension. I will also use the validation framework of DataMapper to make sure that all of the requirements for an extension are met. I end up with the resulting code:


With my model in place, I can implement the code to handle the web request:


The code above is pretty typical for Sinatra. The post block handles POST's to the root URL. There I call a method to check and make sure that the type parameter is set. If it isn't I halt the processing and let the user know that the request is malformed with a 400 code. If the type is set to extension, then we drop into the handle_extension method. Inside of the method, I build an Extension object and check it using the DataMapper validation framework.

There is a little bit of funkiness at the end of the handle_extension method where I need to check the type of error. This is due to the fact that I need to return different status codes depending on the error. Unfortunately, with the DataMapper validations, I didn't see any way to return anything with the errors other than a text message, so this seemed like the best way of doing things.

The handle_section at the end of the post block handles another part of the spec. Don't worry, I didn't write it until I had the tests done first.

Lather, Rinse, Repeat

Implementing the rest of the hData Packaging and Transport spec followed the same process. Take the spec and write a matching unit test. Implement the spec and refine the code until the test passed.

In doing this, I found a couple of bugs in our spec. We hadn't provided parameter names for POSTing section documents. Our description of how to add metadata to documents was ambiguous at best. The nice part was that I was able to discover these things before even digging into the implementation.

What still needs to be done

While the Sinatra app that I wrote is a pretty good implementation of the hData Packaging and Transport spec, it still has some gaps. It doesn't support POSTing metadata on documents, it only creates and serves it's own. It also doesn't support nested sections, but that shouldn't be too hard to add.

Wrap Up

You can find the code at eedrummer/classy-hdata on github. Even if you aren't interested in hData, this application should serve as an example Sinatra/DataMapper application. If you dig into the code and the hData spec, I think you'll see that hData is really easy to implement, especially in a classy framework like Sinatra.

26 comments:

一年 said...

分享的朋友,在精不在多,分享的幸福就在下一秒........................................

said...

看看blog調整心情,又要來繼續工作,大家加油.........................

黑色星期五 said...

一個人想法的大小,決定他成就的大小。..................................................

緣分 said...

每個人生命中都可能遇到貴人,這些貴人不一定真的尊貴,他可能是陌生人,也可能是你的敵人。..................................................

馨裕 said...

性自拍貼圖臺灣自拍網臺灣自拍貼圖網做愛自拍照免費線上自拍影片免費線上貼片區免費線上貼圖免費線上愛影片免費線上影片a片免費線上影片網免費線上影片論壇免費觀看卡通色情片免費觀看台灣片交流網吻技完全免費成人網宏耶宏椰的家宏爺宏爺網站宏爺影城我78我愛78免費我愛78論談我愛87論壇我愛論壇找妹做愛即時通辣妹利特利的遊戲免費觀看成人卡通影片色咪咪影片網一夜情聊天室情色小說情色貼圖

dawnc said...

Are you currently aquatics getting some style of costs that will can’t hold out until eventually payday loan for you newport news your next pay day progress comes? have you ever ever been hooked having associate unforeseen automobile resolve? With regard to the smallest amount bit unforeseen unexpected emergency you’re under-going, a quick day loan can assist you get the money you will need easily! generally, the whole employment generally wants merely fifteen min's or maybe manner less.

marko said...

In this manner these arrangements give you cash without presenting any collateral.3 month time is given to pay these totals. Candidate will have money 600 credit more than 12 months support for doing any work with less premium charges. www.approvedcashadvance.com/locations/jackson-MS/

marko said...

These are only a couple of the circumstances were an advance in light of your payday can offer assistance. In any case, there are an extensive variety of different circumstances, for example, a separated auto or a family crisis where these sort of credits can come in truly convenient. Check Cashing Corona

marko said...

An open gas top permits gas to dissipate. Take the time to fix the top as well. At long last, dependably verify you are not conveying additional freight. A few individuals consider their auto trunks as their storeroom. cash advance

marko said...

A secured credit is much simpler to get than an unsecured advance. You offer the bank the guarantee as installment for the advance in the event that you are not able to complete. The secured moneylender will hold you to that guarantee. It is similar to "prepaying" for your credit. Despite the fact that it is a less demanding approach to get support, you can't overlook the risks of an auto title advance. check cashing

musa ibrahim said...

Gone are the canicule range abettor bicycles were of clear rates, in the present condition, flooding incautious mechanical party are hardened the bike which has accomplished the flying as can be costs. It would be on the completed veritable to say that the costs of abettor bicycles see adjusted at the related of the costs of vehicles in the business. https://www.usacheckcashingstore.com/san-diego

Paul Smith said...

I wasn't going to stop by, but something "inside of me" didn't me let leave this online portal. To tell the truth, this post is helpful and the whole research paper writing process doesn't seem to be difficult!

ariyan arifin said...

I anchored the fax numbers to my payday acclaim affiliations and beatific them see on who to acquaintance apropos the agreement of my advances. On the off adventitious that you do this accomplish a point to affiliate your annual cardinal and encased antecedent trusts cardinal so they can abundantly acquisition your record. Auto Title Loans Chicago

Shan Jonson said...

Consistency - If you accept a debt claim plan, any array of debt claim plan, again one of the key things you are activity to charge is bendability of income. The aftermost affair that you charge is for your assets to be disrupted. Your plan is based on your accepted assets levels.
payday loans chicago

jhon said...

Did the borrower apprehend that it is oftentimes cheaper to acquirement the accolade absolute than it is to accumulate charging and acquire points? Let's not balloon the bulk of absorption that will be paid over the time it takes to pay the debt off, if it anytime gets paid off.

check cashing

musa ibrahim said...

Right ashamed a borrower hypotheses the animate affiliation, the acclamation is able in the deathwatch of authentic some key weighs at the present time anniversary as a borrower's buyer, and whether he has a animate appellation to the auto. auto title loans chicago

Shan Jonson said...

The accepted time aeon of this affectionate of adjustment is two weeks or three weeks. There are some lending food in the United States which accuse with a minimum of 15 percent and a best allegation of 30 percent. This usually translates to an APR of a minimum of 390 percent and best of 790 percent. This is the case for a appellation of two weeks. Usually, to ensure that the money will appear aback to the lender, they will crave the borrower to address a antedate analysis addressed to the lender with all the details.
cash advance loans in fresno ca

Mission Vission said...

Chill whether you are persuading with acquired abhorrent acclaim accoutrement like defaults, affronted obligations, leaving, aback pieces, missing bits, due bits, CCJs, IVA, nonappearance of parts, fragment late, defalcation what not. Check Cashing

Justin said...

The most reasonable approach to purchase another auto or truck is to exploit maker offered low financing costs, which can once in a while keep running as low as zero percent. Those rates, while liberal, aren't offered regularly and are for the most part accessible just to purchasers with the most elevated financial assessments. payday loans

Justin said...

Now and again, you may need to pay even some measure of additional expenses for these. Try not to want to take from the private loan specialists alongside that of Government. Payday Loans

marko said...

You ought to dependably get the cash to the banks or, the money related organizations inside time every month. Attempt to spare the cash from the continuous month, so you can pay understudy advance first in the forthcoming month. Cash Advance Chicago

Paulo said...

By taking out an understudy credit and solidification, the greater part of your remarkable understudy obligations will be paid off by one bigger advance. Credit combination can enormously disentangle the reimbursement of your school obligations by permitting you to pay only one bill every month rather than numerous. Payday Loans Chicago

Paulo said...

Il testosterone è la sostanza nel corpo che ti dà energia e quando i livelli sono esauriti, i livelli di energia cadere e desiderio sessuale precipita. Ci sono molte erbe che possono aumentare i livelli e Ginseng e Horny Goat Weed sono ben noti per non capacità di aumentare i livelli rapidamente, ma le migliori erbe di tutti sono Tonkgat Ali e Tribulus. problemi di erezione a 20 anni

Paulo said...

Non dobbiamo battere intorno al cespuglio: la lettera di copertura gioca un ruolo cruciale in termini di vincita che colloquio di lavoro si aspira a. Questo breve pezzo di testo che si introduce nel contesto di un ruolo che si crede si sono qualificati per. lavoro a londra

Paulo said...

Similar path works with home repair credits as this is frequently called as a monetary item. As the name infers, home repair credits are money related access to mortgage holders who wish to do overhauls or changes in their current living spaces. payday loans san-diego

Paulo said...

Get some information about the loaning organizations and where to get the most reduced financing costs. This sort of learning could outfit you with enough know how with regards to applying for your very own advance. car title loans chicago