There is a Google Ruby client for doing this. They also have documentation.
- Follow the instructions for creating a service account.
- Add the service account to your Google Analytics account user list.
- Create a Project for your new API project if you wish. I usually do and name it something related to the project.
- Create a service account.
- Enable the Google Analytics Reporting API
- In GSuite Domain Security Settings go to API Controls and check "Trust internal, domain-owned apps."
Scope - Analytics Reporting v4
Actually follow the example here.