class FathomAnalytics::Api
Constants
- LIMIT
Attributes
email[R]
password[R]
url[R]
Public Class Methods
new(url:, email:, password:)
click to toggle source
# File lib/fathom_analytics/api.rb, line 7 def initialize(url:, email:, password:) @url = url @email = email @password = password @auth_token = nil end
Public Instance Methods
add_site(name:)
click to toggle source
# File lib/fathom_analytics/api.rb, line 14 def add_site(name:) post_request(path: "/api/sites", params: { name: name }) end
authenticate()
click to toggle source
# File lib/fathom_analytics/api.rb, line 60 def authenticate params = { "Email": email, "Password": password } response = post_request(path: "/api/session", params: params, authenticated: false) do |raw_response| set_auth_token(raw_response) end response end
page_agg_page_views_stats(id:, from:, to:, limit: LIMIT, offset: 0)
click to toggle source
# File lib/fathom_analytics/api.rb, line 45 def page_agg_page_views_stats(id:, from:, to:, limit: LIMIT, offset: 0) path = "/api/sites/#{id}/stats/pages/agg/pageviews" get_request(path: path, before: to, after: from, limit: limit, offset: offset) end
page_agg_stats(id:, from:, to:, limit: LIMIT, offset: 0)
click to toggle source
# File lib/fathom_analytics/api.rb, line 40 def page_agg_stats(id:, from:, to:, limit: LIMIT, offset: 0) path = "/api/sites/#{id}/stats/pages/agg" get_request(path: path, before: to, after: from, limit: limit, offset: offset) end
referrer_agg_page_views_stats(id:, from:, to:, limit: LIMIT, offset: 0)
click to toggle source
# File lib/fathom_analytics/api.rb, line 55 def referrer_agg_page_views_stats(id:, from:, to:, limit: LIMIT, offset: 0) path = "/api/sites/#{id}/stats/referrers/agg/pageviews" get_request(path: path, before: to, after: from, limit: limit, offset: offset) end
referrer_agg_stats(id:, from:, to:, limit: LIMIT, offset: 0)
click to toggle source
# File lib/fathom_analytics/api.rb, line 50 def referrer_agg_stats(id:, from:, to:, limit: LIMIT, offset: 0) path = "/api/sites/#{id}/stats/referrers/agg" get_request(path: path, before: to, after: from, limit: limit, offset: offset) end
remove_site(id:)
click to toggle source
# File lib/fathom_analytics/api.rb, line 18 def remove_site(id:) delete_request(path: "/api/sites/#{id}") end
site_agg_stats(id:, from:, to:, limit: LIMIT, offset: 0)
click to toggle source
# File lib/fathom_analytics/api.rb, line 35 def site_agg_stats(id:, from:, to:, limit: LIMIT, offset: 0) path = "/api/sites/#{id}/stats/site/agg" get_request(path: path, before: to, after: from, limit: limit, offset: offset) end
site_realtime_stats(id:)
click to toggle source
# File lib/fathom_analytics/api.rb, line 26 def site_realtime_stats(id:) get_request(path: "/api/sites/#{id}/stats/site/realtime") end
site_stats(id:, from:, to:, limit: LIMIT, offset: 0)
click to toggle source
# File lib/fathom_analytics/api.rb, line 30 def site_stats(id:, from:, to:, limit: LIMIT, offset: 0) path = "/api/sites/#{id}/stats/site" get_request(path: path, before: to, after: from, limit: limit, offset: offset) end
sites()
click to toggle source
# File lib/fathom_analytics/api.rb, line 22 def sites get_request(path: "/api/sites") end
Private Instance Methods
api_response(response_body)
click to toggle source
# File lib/fathom_analytics/api.rb, line 130 def api_response(response_body) parsed = JSON.parse(response_body) parsed["Data"] || {} rescue JSON::ParserError {} end
connection()
click to toggle source
# File lib/fathom_analytics/api.rb, line 157 def connection @connection ||= FathomAnalytics::Connection.new(base_url: url) end
delete_request(path:, authenticated: true) { |response| ... }
click to toggle source
# File lib/fathom_analytics/api.rb, line 113 def delete_request(path:, authenticated: true) ensure_auth_token if authenticated response = connection.delete( path: path, auth_token: @auth_token ) if response.status == 200 yield response if block_given? api_response(response.body) else raise FathomAnalytics::Error.new(response.status) end end
ensure_auth_token()
click to toggle source
# File lib/fathom_analytics/api.rb, line 137 def ensure_auth_token return unless @auth_token.nil? authenticate end
get_request(path:, before: nil, after: nil, limit: nil, offset: nil, authenticated: true)
click to toggle source
# File lib/fathom_analytics/api.rb, line 73 def get_request(path:, before: nil, after: nil, limit: nil, offset: nil, authenticated: true) ensure_auth_token if authenticated params = {} params['before'] = before if before params['after'] = after if after params['limit'] = limit if limit params['offset'] = offset if offset response = connection.get( path: path, auth_token: @auth_token, params: params ) if response.status == 200 api_response(response.body) else raise FathomAnalytics::Error.new(response.status) end end
parse_auth_header(header)
click to toggle source
# File lib/fathom_analytics/api.rb, line 150 def parse_auth_header(header) return nil if header.nil? matches = header.match(/auth=([a-zA-Z0-9\-_]+);/) matches[1] if matches end
post_request(path:, params:, authenticated: true) { |response| ... }
click to toggle source
# File lib/fathom_analytics/api.rb, line 95 def post_request(path:, params:, authenticated: true, &block) ensure_auth_token if authenticated response = connection.post( path: path, params: params, auth_token: @auth_token ) if response.status == 200 yield response if block_given? api_response(response.body) else raise FathomAnalytics::Error.new(response.status) end end
set_auth_token(response)
click to toggle source
# File lib/fathom_analytics/api.rb, line 143 def set_auth_token(response) set_cookie_header = response.headers['set-cookie'] @auth_token = parse_auth_header(set_cookie_header) raise FathomAnalytics::Error.new("Failed to retrieve auth key") unless @auth_token end