Module: Helpers

Included in:
Visitors::Resource, Visitors::Resource
Defined in:
lib/visitors/helpers.rb

Defined Under Namespace

Classes: UnsupportedField

Constant Summary collapse

SET_NAME =
'resource_ids'

Instance Method Summary collapse

Instance Method Details

#assert_valid_field!(name) ⇒ Object



18
19
20
21
22
23
# File 'lib/visitors/helpers.rb', line 18

def assert_valid_field!(name)
  unless fields.include?(name)
    error_message = "Invalid field #{name.inspect}. Valid fields are #{fields.inspect}"
    raise UnsupportedField, error_message
  end
end

#countObject



29
30
31
# File 'lib/visitors/helpers.rb', line 29

def count
  redis.scard(SET_NAME)
end

#fieldsObject



14
15
16
# File 'lib/visitors/helpers.rb', line 14

def fields
  Visitors.config.fields || []
end

#known_idsObject



33
34
35
# File 'lib/visitors/helpers.rb', line 33

def known_ids
  redis.smembers(SET_NAME)
end

#parse_date(date) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/visitors/helpers.rb', line 37

def parse_date(date)
  date = date.to_s
  case date
  when /\d{4}/
    date
  when /\d{1,2}/
    Time.now.strftime("#{'%02d' % date}%y")
  else
    Time.now.strftime('%m%y')
  end
end

#redisObject



9
10
11
12
# File 'lib/visitors/helpers.rb', line 9

def redis
  @redis ||= Redis::Namespace.new(Visitors.config.redis_namespace,
                                  :redis => redis_connection)
end

#stats_for(key) ⇒ Object



25
26
27
# File 'lib/visitors/helpers.rb', line 25

def stats_for(key)
  redis.hgetall(key)
end