Class: InternetSampler::Sampler

Inherits:
Object
  • Object
show all
Defined in:
lib/internet-sampler/sampler.rb

Instance Method Summary collapse

Constructor Details

#initialize(redis) ⇒ Sampler



3
4
5
6
# File 'lib/internet-sampler/sampler.rb', line 3

def initialize redis
  @tracks = []
  @redis = redis
end

Instance Method Details

#add_track(track) ⇒ Object



14
15
16
# File 'lib/internet-sampler/sampler.rb', line 14

def add_track track
  @tracks << track
end

#get_count(slug) ⇒ Object



18
19
20
# File 'lib/internet-sampler/sampler.rb', line 18

def get_count slug
  @redis.get slug if has_track? slug
end

#has_track?(slug) ⇒ Boolean



8
9
10
11
12
# File 'lib/internet-sampler/sampler.rb', line 8

def has_track? slug
  @tracks.select do |track|
    track[:slug] == slug
  end.size == 1
end

#incr(slug) ⇒ Object



22
23
24
# File 'lib/internet-sampler/sampler.rb', line 22

def incr slug
  @redis.incr slug if has_track? slug
end

#track_info(slug) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/internet-sampler/sampler.rb', line 33

def track_info slug
  t = @tracks.select do |track|
    track[:slug] == slug
  end.first
  t[:count] = @redis.get slug
  t
end

#tracksObject



26
27
28
29
30
31
# File 'lib/internet-sampler/sampler.rb', line 26

def tracks
  @tracks.map do |track|
    track[:count] = @redis.get track[:slug]
    track
  end
end