Class: TokenHawk::DomainQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/token_hawk/domain_query.rb

Instance Method Summary collapse

Constructor Details

#initialize(domain:) ⇒ DomainQuery

Returns a new instance of DomainQuery.

Raises:

  • (ActiveRecord::RecordNotFound)


5
6
7
8
9
# File 'lib/token_hawk/domain_query.rb', line 5

def initialize(domain:)
  @domain = domain.to_s
  @scope  = CallRecord.for_domain(@domain)
  raise ActiveRecord::RecordNotFound, "No records for domain: #{@domain}" if @scope.empty?
end

Instance Method Details

#call_count ⇒ Object



15
16
17
# File 'lib/token_hawk/domain_query.rb', line 15

def call_count
  @scope.count
end

#cost_per_call ⇒ Object



19
20
21
# File 'lib/token_hawk/domain_query.rb', line 19

def cost_per_call
  (total_cost.to_f / call_count).round(2)
end

#recent_calls ⇒ Object



31
32
33
# File 'lib/token_hawk/domain_query.rb', line 31

def recent_calls
  @scope.order(created_at: :desc).limit(10)
end

#top_tags ⇒ Object



23
24
25
26
27
28
29
# File 'lib/token_hawk/domain_query.rb', line 23

def top_tags
  @scope.to_a
        .flat_map { |r| r.tags.to_a }
        .tally
        .sort_by { |_, count| -count }
        .map(&:first)
end

#total_cost ⇒ Object



11
12
13
# File 'lib/token_hawk/domain_query.rb', line 11

def total_cost
  @scope.sum(:total_cost_cents)
end