├── .github └── workflows │ └── main.yml ├── .gitignore ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── Gemfile ├── Gemfile.lock ├── LICENSE.txt ├── README.md ├── Rakefile ├── bin ├── console └── setup ├── fathom_api.gemspec ├── lib ├── fathom.rb ├── fathom │ ├── client.rb │ ├── object.rb │ ├── objects │ │ ├── account.rb │ │ ├── event.rb │ │ ├── list.rb │ │ └── site.rb │ ├── resource.rb │ ├── resources │ │ ├── account.rb │ │ ├── aggregations.rb │ │ ├── current_visitors.rb │ │ ├── events.rb │ │ └── sites.rb │ └── version.rb └── fathom_api.rb └── test ├── fathom_api_test.rb └── test_helper.rb /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | on: 3 | pull_request: 4 | branches: 5 | - "*" 6 | push: 7 | branches: 8 | - main 9 | 10 | jobs: 11 | tests: 12 | runs-on: ubuntu-latest 13 | strategy: 14 | matrix: 15 | ruby: ["2.7", "3.0"] 16 | 17 | steps: 18 | - uses: actions/checkout@main 19 | 20 | - name: Set up Ruby 21 | uses: ruby/setup-ruby@v1 22 | with: 23 | ruby-version: ${{ matrix.ruby }} 24 | bundler-cache: true 25 | 26 | - name: StandardRb check 27 | run: bundle exec standardrb 28 | 29 | - name: Run tests 30 | run: | 31 | bundle exec rake test 32 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /_yardoc/ 4 | /coverage/ 5 | /doc/ 6 | /pkg/ 7 | /spec/reports/ 8 | /tmp/ 9 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). 6 | 7 | ## [Unreleased] 8 | 9 | ## [0.2.0] - 2022-05-26 10 | 11 | ### Changed 12 | 13 | - Update the Aggregations endpoint to automatically call `.to_json` when using Filters if needed. Thanks @joemasilotti! 14 | - Bump version to 0.2.0 15 | 16 | ## [0.1.2] - 2021-08-23 17 | 18 | ### Added 19 | 20 | - Previous stable release of the ruby gem for the Fathom API. 21 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community. 8 | 9 | ## Our Standards 10 | 11 | Examples of behavior that contributes to a positive environment for our community include: 12 | 13 | * Demonstrating empathy and kindness toward other people 14 | * Being respectful of differing opinions, viewpoints, and experiences 15 | * Giving and gracefully accepting constructive feedback 16 | * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience 17 | * Focusing on what is best not just for us as individuals, but for the overall community 18 | 19 | Examples of unacceptable behavior include: 20 | 21 | * The use of sexualized language or imagery, and sexual attention or 22 | advances of any kind 23 | * Trolling, insulting or derogatory comments, and personal or political attacks 24 | * Public or private harassment 25 | * Publishing others' private information, such as a physical or email 26 | address, without their explicit permission 27 | * Other conduct which could reasonably be considered inappropriate in a 28 | professional setting 29 | 30 | ## Enforcement Responsibilities 31 | 32 | Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful. 33 | 34 | Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate. 35 | 36 | ## Scope 37 | 38 | This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. 39 | 40 | ## Enforcement 41 | 42 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at afomera@hey.com. All complaints will be reviewed and investigated promptly and fairly. 43 | 44 | All community leaders are obligated to respect the privacy and security of the reporter of any incident. 45 | 46 | ## Enforcement Guidelines 47 | 48 | Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct: 49 | 50 | ### 1. Correction 51 | 52 | **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community. 53 | 54 | **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested. 55 | 56 | ### 2. Warning 57 | 58 | **Community Impact**: A violation through a single incident or series of actions. 59 | 60 | **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban. 61 | 62 | ### 3. Temporary Ban 63 | 64 | **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior. 65 | 66 | **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban. 67 | 68 | ### 4. Permanent Ban 69 | 70 | **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. 71 | 72 | **Consequence**: A permanent ban from any sort of public interaction within the community. 73 | 74 | ## Attribution 75 | 76 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0, 77 | available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. 78 | 79 | Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity). 80 | 81 | [homepage]: https://www.contributor-covenant.org 82 | 83 | For answers to common questions about this code of conduct, see the FAQ at 84 | https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations. 85 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | source "https://rubygems.org" 4 | 5 | # Specify your gem's dependencies in fathom_api.gemspec 6 | gemspec 7 | 8 | gem "rake", "~> 13.0" 9 | 10 | gem "minitest", "~> 5.0" 11 | 12 | gem "standard" 13 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | fathom_api (0.2.0) 5 | faraday (~> 1.7) 6 | faraday_middleware (~> 1.1) 7 | 8 | GEM 9 | remote: https://rubygems.org/ 10 | specs: 11 | ast (2.4.2) 12 | faraday (1.10.0) 13 | faraday-em_http (~> 1.0) 14 | faraday-em_synchrony (~> 1.0) 15 | faraday-excon (~> 1.1) 16 | faraday-httpclient (~> 1.0) 17 | faraday-multipart (~> 1.0) 18 | faraday-net_http (~> 1.0) 19 | faraday-net_http_persistent (~> 1.0) 20 | faraday-patron (~> 1.0) 21 | faraday-rack (~> 1.0) 22 | faraday-retry (~> 1.0) 23 | ruby2_keywords (>= 0.0.4) 24 | faraday-em_http (1.0.0) 25 | faraday-em_synchrony (1.0.0) 26 | faraday-excon (1.1.0) 27 | faraday-httpclient (1.0.1) 28 | faraday-multipart (1.0.3) 29 | multipart-post (>= 1.2, < 3) 30 | faraday-net_http (1.0.1) 31 | faraday-net_http_persistent (1.2.0) 32 | faraday-patron (1.0.0) 33 | faraday-rack (1.0.0) 34 | faraday-retry (1.0.3) 35 | faraday_middleware (1.2.0) 36 | faraday (~> 1.0) 37 | minitest (5.14.4) 38 | multipart-post (2.1.1) 39 | parallel (1.20.1) 40 | parser (3.0.2.0) 41 | ast (~> 2.4.1) 42 | rainbow (3.0.0) 43 | rake (13.0.6) 44 | regexp_parser (2.1.1) 45 | rexml (3.2.5) 46 | rubocop (1.18.4) 47 | parallel (~> 1.10) 48 | parser (>= 3.0.0.0) 49 | rainbow (>= 2.2.2, < 4.0) 50 | regexp_parser (>= 1.8, < 3.0) 51 | rexml 52 | rubocop-ast (>= 1.8.0, < 2.0) 53 | ruby-progressbar (~> 1.7) 54 | unicode-display_width (>= 1.4.0, < 3.0) 55 | rubocop-ast (1.10.0) 56 | parser (>= 3.0.1.1) 57 | rubocop-performance (1.11.4) 58 | rubocop (>= 1.7.0, < 2.0) 59 | rubocop-ast (>= 0.4.0) 60 | ruby-progressbar (1.11.0) 61 | ruby2_keywords (0.0.5) 62 | standard (1.1.7) 63 | rubocop (= 1.18.4) 64 | rubocop-performance (= 1.11.4) 65 | unicode-display_width (2.0.0) 66 | 67 | PLATFORMS 68 | x86_64-darwin-20 69 | x86_64-darwin-21 70 | x86_64-linux 71 | 72 | DEPENDENCIES 73 | fathom_api! 74 | minitest (~> 5.0) 75 | rake (~> 13.0) 76 | standard 77 | 78 | BUNDLED WITH 79 | 2.2.33 80 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2021 Andrea Fomera 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Fathom API 2 | 3 | ### 🛠 An easy to use client with the Fathom API 4 | 5 | You'll need a usefathom.com account to use the API, if you don't have one [click here to sign up](https://usefathom.com/ref/CVNWHD). 6 | 7 | As of August 17th, 2021 the API was still early-access, so some endpoints may be different than in production. Feel free to submit a PR or issue. Contributions are welcome! 8 | 9 | [![Build Status](https://github.com/afomera/fathom_api/workflows/Tests/badge.svg)](https://github.com/afomera/fathom_api/actions) [![Gem Version](https://badge.fury.io/rb/fathom_api.svg)](https://badge.fury.io/rb/fathom_api) 10 | 11 | ## Installation 12 | 13 | Add `fathom_api` to your application's Gemfile: 14 | 15 | ```bash 16 | bundle add fathom_api 17 | 18 | # OR in the Gemfile 19 | gem "fathom_api" 20 | ``` 21 | 22 | ## Usage 23 | 24 | Create a client to get started, passing an `api_key` you generate in your Fathom API settings. 25 | 26 | ```ruby 27 | client = Fathom::Client.new(api_key: ENV['FATHOM_API_KEY']) 28 | ``` 29 | 30 | ### Fetch account details 31 | 32 | ```ruby 33 | # Get account details 34 | client.account.info 35 | # => Fathom::Account 36 | ``` 37 | 38 | Responses are wrapped in an object that dynamically allows you to call the attributes as if they are an OpenStruct... IE 39 | 40 | ```ruby 41 | response = client.account.info 42 | # => #> 43 | 44 | response.name 45 | # => "Your account name" 46 | response.email 47 | # => "you@starfleet.org" 48 | ``` 49 | 50 | ### Pagination 51 | 52 | When an API's response comes back with a object of "list", we automatically wrap that to attempt to provide some helper methods to ease your implementations. 53 | 54 | ```ruby 55 | list = client.sites.list(limit: 1) 56 | # => Fathom::List 57 | 58 | # We provide two helper methods to allow you to get the first or last id from the data response 59 | list.first_id 60 | list.last_id 61 | 62 | # check if the list has more after it with 63 | list.has_more? 64 | # => true 65 | 66 | # use the next page cursor in your next response 67 | list2 = client.sites.list(limit: 1, starting_after: list.last_id) 68 | ``` 69 | 70 | ### Sites 71 | 72 | ```ruby 73 | client.sites.list 74 | # Optionally, pass params in to filter / limit responses 75 | # Limit can be between 1 and 100 76 | client.sites.list(limit: 1, starting_after: "SITE_ID") 77 | # => Fathom::List 78 | 79 | client.sites.retrieve(site_id: site_id) 80 | client.sites.create({}) # Include `name` and other params 81 | client.sites.update(site_id: site_id, {}) # Params being updated 82 | client.sites.delete(site_id: site_id) 83 | client.sites.wipe(site_id: site_id) 84 | # All return 85 | # => Fathom::Site 86 | ``` 87 | 88 | ### Events 89 | 90 | ```ruby 91 | client.events.list(site_id: site_id) 92 | # => Fathom::List 93 | # Optionally, pass params in to filter / limit responses 94 | # Limit can be between 1 and 100 95 | client.events.list(site_id: site_id, limit: 10, starting_after: "EVENT_ID") 96 | # => Fathom::List 97 | 98 | client.events.retrieve(site_id: site_id, event_id: event_id) 99 | client.events.create(site_id: site_id, {}) # Attributes for event 100 | client.events.update(site_id: site_id, {}) # Attributes being updated 101 | client.events.delete(site_id: site_id, event_id: event_id) 102 | client.events.wipe(site_id: site_id, event_id: event_id) 103 | # All return 104 | # Fathom::Event 105 | ``` 106 | 107 | ### Current Visitors 108 | 109 | ```ruby 110 | client.current_visitors(site_id: site_id, {}) # Can add detailed: true for a more detailed report 111 | ``` 112 | 113 | ### Aggregation 114 | 115 | ```ruby 116 | client.aggregations.list(entity_id: entity_id, entity: entity, aggregates: aggregates, **params) 117 | ``` 118 | 119 | You can find all the available parameters in the [official Fathom docs](https://usefathom.com/api#introduction) 120 | 121 | ## 🙏 Contributing 122 | 123 | This project uses Standard for formatting Ruby code. Please make sure to run standardrb before submitting pull requests. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/afomera/fathom_api/blob/main/CODE_OF_CONDUCT.md). 124 | 125 | ## Code of Conduct 126 | 127 | Everyone interacting in the FathomApi project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/afomera/fathom_api/blob/main/CODE_OF_CONDUCT.md). 128 | 129 | ## 📝 License 130 | 131 | The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). 132 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "bundler/gem_tasks" 4 | require "rake/testtask" 5 | 6 | Rake::TestTask.new(:test) do |t| 7 | t.libs << "test" 8 | t.libs << "lib" 9 | t.test_files = FileList["test/**/*_test.rb"] 10 | end 11 | 12 | task default: :test 13 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # frozen_string_literal: true 3 | 4 | require "bundler/setup" 5 | require "fathom_api" 6 | 7 | # You can add fixtures and/or initialization code here to make experimenting 8 | # with your gem easier. You can also use a different console, if you like. 9 | 10 | # (If you use this, don't forget to add pry to your Gemfile!) 11 | # require "pry" 12 | # Pry.start 13 | 14 | client = Fathom::Client.new(api_key: ENV['FATHOM_API_KEY']) 15 | 16 | 17 | puts "`client` is now set with the api_key equal to ENV['FATHOM_API_KEY']" 18 | require "irb" 19 | IRB.start(__FILE__) 20 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | set -vx 5 | 6 | bundle install 7 | 8 | # Do any other automated setup that you need to do here 9 | -------------------------------------------------------------------------------- /fathom_api.gemspec: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require_relative "lib/fathom/version" 4 | 5 | Gem::Specification.new do |spec| 6 | spec.name = "fathom_api" 7 | spec.version = Fathom::VERSION 8 | spec.authors = ["Andrea Fomera"] 9 | spec.email = ["afomera@hey.com"] 10 | 11 | spec.summary = "A gem for using the Fathom API" 12 | spec.homepage = "https://github.com/afomera/fathom_api" 13 | spec.license = "MIT" 14 | spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0") 15 | 16 | spec.metadata["homepage_uri"] = spec.homepage 17 | spec.metadata["source_code_uri"] = "https://github.com/afomera/fathom_api" 18 | # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here." 19 | 20 | # Specify which files should be added to the gem when it is released. 21 | # The `git ls-files -z` loads the files in the RubyGem that have been added into git. 22 | spec.files = Dir.chdir(File.expand_path(__dir__)) do 23 | `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) } 24 | end 25 | spec.bindir = "exe" 26 | spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } 27 | spec.require_paths = ["lib"] 28 | 29 | spec.add_dependency "faraday", "~> 1.7" 30 | spec.add_dependency "faraday_middleware", "~> 1.1" 31 | end 32 | -------------------------------------------------------------------------------- /lib/fathom.rb: -------------------------------------------------------------------------------- 1 | require "faraday" 2 | require "faraday_middleware" 3 | require "fathom/version" 4 | 5 | module Fathom 6 | autoload :Client, "fathom/client" 7 | autoload :Resource, "fathom/resource" 8 | autoload :Object, "fathom/object" 9 | 10 | # Resources (like high level API endpoints) 11 | autoload :AccountResource, "fathom/resources/account" 12 | autoload :AggregationsResource, "fathom/resources/aggregations" 13 | autoload :CurrentVisitorsResource, "fathom/resources/current_visitors" 14 | autoload :EventsResource, "fathom/resources/events" 15 | autoload :SitesResource, "fathom/resources/sites" 16 | 17 | # Objects we wrap the responses in 18 | autoload :Account, "fathom/objects/account" 19 | autoload :Event, "fathom/objects/event" 20 | autoload :List, "fathom/objects/list" 21 | autoload :Site, "fathom/objects/site" 22 | 23 | class Error < StandardError; end 24 | 25 | def self.build_object(data) 26 | return data unless data.is_a?(Hash) 27 | type = data.fetch("object", "object") 28 | class_name = type.split("_").map(&:capitalize).join 29 | Fathom.const_get(class_name).new(data) 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /lib/fathom/client.rb: -------------------------------------------------------------------------------- 1 | module Fathom 2 | class Client 3 | BASE_URL = "https://api.usefathom.com/v1" 4 | 5 | attr_reader :api_key, :adapter 6 | 7 | def initialize(api_key:, adapter: Faraday.default_adapter) 8 | @api_key = api_key 9 | @adapter = adapter 10 | end 11 | 12 | def account 13 | AccountResource.new(self) 14 | end 15 | 16 | def aggregations 17 | AggregationsResource.new(self) 18 | end 19 | 20 | def current_visitors 21 | CurrentVisitorsResource.new(self) 22 | end 23 | 24 | def events 25 | EventsResource.new(self) 26 | end 27 | 28 | def sites 29 | SitesResource.new(self) 30 | end 31 | 32 | def connection 33 | @connection ||= Faraday.new do |conn| 34 | conn.url_prefix = BASE_URL 35 | conn.request :json 36 | conn.response :json, content_type: "application/json" 37 | conn.adapter adapter 38 | end 39 | end 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /lib/fathom/object.rb: -------------------------------------------------------------------------------- 1 | require "ostruct" 2 | 3 | module Fathom 4 | class Object 5 | def initialize(attributes) 6 | @attributes = OpenStruct.new(attributes) 7 | end 8 | 9 | def method_missing(method, *args, &block) 10 | attribute = @attributes.send(method, *args, &block) 11 | attribute.is_a?(Hash) ? Object.new(attribute) : attribute 12 | end 13 | 14 | def respond_to_missing?(method, include_private = false) 15 | true 16 | end 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /lib/fathom/objects/account.rb: -------------------------------------------------------------------------------- 1 | module Fathom 2 | class Account < Object 3 | end 4 | end 5 | -------------------------------------------------------------------------------- /lib/fathom/objects/event.rb: -------------------------------------------------------------------------------- 1 | module Fathom 2 | class Event < Object 3 | end 4 | end 5 | -------------------------------------------------------------------------------- /lib/fathom/objects/list.rb: -------------------------------------------------------------------------------- 1 | module Fathom 2 | class List < Object 3 | def initialize(attributes) 4 | super 5 | @attributes["data"].map! do |entry| 6 | Fathom.build_object(entry) 7 | end 8 | end 9 | 10 | def has_more? 11 | @attributes["has_more"] 12 | end 13 | 14 | def last_id 15 | @attributes["data"].last.id 16 | end 17 | 18 | def first_id 19 | @attributes["data"].first.id 20 | end 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /lib/fathom/objects/site.rb: -------------------------------------------------------------------------------- 1 | module Fathom 2 | class Site < Object 3 | end 4 | end 5 | -------------------------------------------------------------------------------- /lib/fathom/resource.rb: -------------------------------------------------------------------------------- 1 | module Fathom 2 | class Resource 3 | attr_reader :client 4 | 5 | def initialize(client) 6 | @client = client 7 | end 8 | 9 | private 10 | 11 | def get_request(url, params: {}, headers: {}) 12 | handle_response client.connection.get(url, params, default_headers.merge(headers)) 13 | end 14 | 15 | def post_request(url, body:, headers: {}) 16 | handle_response client.connection.post(url, body, default_headers.merge(headers)) 17 | end 18 | 19 | def patch_request(url, body:, headers: {}) 20 | handle_response client.connection.patch(url, body, default_headers.merge(headers)) 21 | end 22 | 23 | def put_request(url, body:, headers: {}) 24 | handle_response client.connection.put(url, body, default_headers.merge(headers)) 25 | end 26 | 27 | def delete_request(url, params: {}, headers: {}) 28 | handle_response client.connection.delete(url, params, default_headers.merge(headers)) 29 | end 30 | 31 | def default_headers 32 | {Authorization: "Bearer #{client.api_key}"} 33 | end 34 | 35 | def handle_response(response) 36 | case response.status 37 | when 400 38 | raise Error, "Your request was malformed. #{response.body}" 39 | when 401 40 | raise Error, "You did not supply valid authentication credentials. #{response.body}" 41 | when 403 42 | raise Error, "You are not allowed to perform that action. #{response.body}" 43 | when 404 44 | raise Error, "No results were found for your request. #{response.body}" 45 | when 429 46 | raise Error, "Your request exceeded the API rate limit. #{response.body}" 47 | when 500 48 | raise Error, "We were unable to perform the request due to server-side problems. #{response.body}" 49 | end 50 | 51 | Fathom.build_object(response.body) 52 | end 53 | end 54 | end 55 | -------------------------------------------------------------------------------- /lib/fathom/resources/account.rb: -------------------------------------------------------------------------------- 1 | module Fathom 2 | class AccountResource < Resource 3 | def info 4 | get_request("account") 5 | end 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /lib/fathom/resources/aggregations.rb: -------------------------------------------------------------------------------- 1 | module Fathom 2 | class AggregationsResource < Resource 3 | # TODO: Ensure this works properly 4 | def list(entity_id:, entity:, aggregates:, **params) 5 | filters = params.delete(:filters) || {} 6 | filters = filters.to_json if filters.is_a?(Hash) 7 | 8 | params.merge!( 9 | entity_id: entity_id, 10 | entity: entity, 11 | aggregates: aggregates, 12 | filters: filters 13 | ) 14 | 15 | get_request("aggregations", params: params) 16 | end 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /lib/fathom/resources/current_visitors.rb: -------------------------------------------------------------------------------- 1 | module Fathom 2 | class CurrentVisitorsResource < Resource 3 | def list(site_id:, **params) 4 | get_request("current_visitors", params: params.merge(site_id: site_id)) 5 | end 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /lib/fathom/resources/events.rb: -------------------------------------------------------------------------------- 1 | module Fathom 2 | class EventsResource < Resource 3 | def list(site_id:, **params) 4 | get_request("sites/#{site_id}/events", params: params) 5 | end 6 | 7 | def retrieve(site_id:, event_id:) 8 | get_request("sites/#{site_id}/events/#{event_id}") 9 | end 10 | 11 | def create(site_id:, **attributes) 12 | post_request("sites/#{site_id}/events", body: attributes) 13 | end 14 | 15 | def update(site_id:, event_id:, **attributes) 16 | post_request("sites/#{site_id}/events/#{event_id}", body: attributes) 17 | end 18 | 19 | def delete(site_id:, event_id:) 20 | delete_request("sites/#{site_id}/events/#{event_id}") 21 | end 22 | 23 | def wipe(site_id:, event_id:) 24 | delete_request("sites/#{site_id}/events/#{event_id}/data") 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /lib/fathom/resources/sites.rb: -------------------------------------------------------------------------------- 1 | module Fathom 2 | class SitesResource < Resource 3 | def list(**params) 4 | get_request("sites", params: params) 5 | end 6 | 7 | def retrieve(site_id:) 8 | get_request("sites/#{site_id}") 9 | end 10 | 11 | def create(**attributes) 12 | post_request("sites", body: attributes) 13 | end 14 | 15 | def update(site_id:, **attributes) 16 | post_request("sites/#{site_id}", body: attributes) 17 | end 18 | 19 | def delete(site_id:) 20 | delete_request("sites/#{site_id}") 21 | end 22 | 23 | def wipe(site_id:) 24 | delete_request("sites/#{site_id}/data") 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /lib/fathom/version.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module Fathom 4 | VERSION = "0.2.0" 5 | end 6 | -------------------------------------------------------------------------------- /lib/fathom_api.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | # Empty, but used to require and load in the Fathom namespace automatically 4 | # So we have saner class names rather than FathomApi::Client 5 | require "fathom" 6 | -------------------------------------------------------------------------------- /test/fathom_api_test.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "test_helper" 4 | 5 | class FathomTest < Minitest::Test 6 | def test_that_it_has_a_version_number 7 | refute_nil ::Fathom::VERSION 8 | end 9 | 10 | def test_it_does_something_useful 11 | assert true 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | $LOAD_PATH.unshift File.expand_path("../lib", __dir__) 4 | require "fathom_api" 5 | 6 | require "minitest/autorun" 7 | --------------------------------------------------------------------------------