Challenges working with health data
Table of content
In recent years the mobile health industry has been booming. Companies like FitBit, Moves, Nike are leading this revolution. The essential idea behind these services is that you can measure the number of activities or calorie burn, calorie intake or food consumption and quality of your sleep. Since it is becoming a huge deal, naturally a lot of startups are taking advantage of it and trying to make use of this data. Some companies are providing integration points with these services and unifying data, some are just proxies to these APIs. Challenges for these types of startups are data unification, rate limits and user’s expectation of instant feedback. While working with multiple APIs, each one of them has a unique format that needs to be parsed and stored. Another issue that you have to solve is rate limiting. For example, if you want to get step count for a month and API provides this kind of data only on daily basis, API rate limits become an issue since fetching data for a month takes 30 requests. And the user may hit the “sync” button more than once. If you want to increase rate limits, you would have to sign a contract with the data provider, which may be a tedious process. Also, consider the fact that users want to see their steps immediately after they synchronize with the provider. Sometimes providers love to change their API unannounced or somehow you may miss that fact.
What we had
We had to start out with a Ruby on Rails app that had some synchronization logic in modules that were included in ActiveRecord models. It worked, but it was almost impossible to reason about. The code was scattered all over the place with tons of duplication.
How did we solve that problem?
Well, with good architecture, of course! The first thing that we needed to do is to cover the whole thing with integration tests, to ensure we do not break anything along the way. Now, by integration tests, I mean that we simulated work with API and our data layer and business logic layer; no UI code was touched. We recorded all request/response cycles and made sure that response data is getting parsed, unified and stored.
After we were confident in the percentage of test coverage, we chose an architecture that had a superclass, that was responsible for storing data and asking questions to the system. All API connections and data parsing were handled in subclasses. It worked okay at the beginning when we had 2-3 integrations, but at some point, we discovered that testing this system required building or stubbing a lot of dependencies. Also, as you can imagine, this architecture clearly violated some object-oriented best practices. Subclasses and superclass had too many responsibilities.

When we came to the conclusion that this won’t fly, we turned our attention to best practices in this area or simply patterns that would help us to conceptualize what kind of architecture we wanted to see. Our main goal was to make it testable, composable, decoupled from main rails app and easy to reason about.
We chose to refactor the system in the following way: all external gems that worked with APIs would be wrapped with Provider classes to make sure that the object interface is consistent across all data fetching libraries. Also, we made sure that the provider returns JSON parsed into a hash.
After providers were made and unit tested, we switched our attention to data unification. We chose to call these objects parsers. A parser would take data from a specific provider and parse it into a format acceptable to us. If we had gone with unpersisted ActiveRecord model instances, that would not have given us decoupling from Rails. We chose to use a simple Virtus.model that could later be persisted into the database.
Once we made sure that we got the data we needed, we needed a mechanism to store this data. Again, looking at the patterns, we chose the repository pattern, which would take DTOs and take care of persistence of that data. Also, we needed to ask questions of the system somehow. For example, we did not want to synchronize data for 30 days if the user already had synchronized once or if they had data from two days ago, in that case we would just launch a synchronization only for two days with last sync date included, because user might have data that is not full for that day. At that point in time, it seemed natural to put this code in the repository as well. All interactions with the database happened with the help of the repository.
Now that we got everything set up, we needed a way to build these objects and make interactions between them happen. We chose the mediator pattern. This would take care of all interactions between repository, parsers and providers. By injecting these dependencies into the mediator, we were able to unit test the whole system and to keep integration test coverage.
This architecture proved to be useful to us because when Active Record was not fast enough to do the persistence part, we swapped it out for regular SQL queries and the interface of the repository did not change a bit.
Another benefit, which was unexpected, was the fact that we had to build new integrations without having the ability to test against their API. We only had documentation, and the agreement to use this API was still being signed. We were able to build the system only based on the documentation without making real requests. When the time came, it was trivial to replace missing bits with actually functional code.
The main takeaway from this article is that you should not be afraid to invest in good architecture; it will pay off further down the road. Also, consider the fact that the process of architecture is not something you do one time and forget about. It should be something that evolves as your business is changing.
Don’t write code. Solve problems.
Update (November 2026): Moves and its API shut down on 31 July 2018. Google replaced the Fitbit Web API with the Google Health API and turned off the legacy endpoints in late 2026. Virtus is discontinued; its author recommends dry-types and dry-struct instead.
