class PlausibleApi::Client

Constants

BASE_URL

Public Class Methods

new(site_id, token) click to toggle source
# File lib/plausible_api/client.rb, line 19
def initialize(site_id, token)
  @site_id = site_id.to_s
  @token   = token.to_s
end

Public Instance Methods

aggregate(options = {}) click to toggle source
# File lib/plausible_api/client.rb, line 24
def aggregate(options = {})
  call PlausibleApi::Stats::Aggregate.new(options)
end
breakdown(options = {}) click to toggle source
# File lib/plausible_api/client.rb, line 32
def breakdown(options = {})
  call PlausibleApi::Stats::Breakdown.new(options)
end
realtime_visitors() click to toggle source
# File lib/plausible_api/client.rb, line 36
def realtime_visitors
  call PlausibleApi::Stats::Realtime::Visitors.new
end
timeseries(options = {}) click to toggle source
# File lib/plausible_api/client.rb, line 28
def timeseries(options = {})
  call PlausibleApi::Stats::Timeseries.new(options)
end
valid?() click to toggle source
# File lib/plausible_api/client.rb, line 40
def valid?
  begin
    realtime_visitors
    return true
  rescue
    return false
  end
end

Private Instance Methods

call(api) click to toggle source
# File lib/plausible_api/client.rb, line 50
def call(api)      
  raise StandardError.new api.errors unless api.valid?
  
  url = "#{BASE_URL}#{api.request_url.gsub('$SITE_ID', @site_id)}"
  uri = URI.parse(url)

  req = Net::HTTP::Get.new(uri.request_uri)
  req.add_field('Authorization', "Bearer #{@token}")

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true  

  response = http.request(req)

  if response.code == "200"
    api.parse_response response.body
  else
    raise StandardError.new response.body
  end
end