Class: Visitors::Resource

Inherits:
Object
  • Object
show all
Extended by:
Helpers
Includes:
Helpers
Defined in:
lib/visitors/resource.rb

Constant Summary collapse

MONTHS =
(1..12).to_a

Constants included from Helpers

Helpers::SET_NAME

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

assert_valid_field!, count, fields, known_ids, parse_date, redis, stats_for

Constructor Details

#initialize(id, date = nil) ⇒ Resource

Returns a new instance of Resource.

Raises:

  • (ArgumentError)


23
24
25
26
27
# File 'lib/visitors/resource.rb', line 23

def initialize(id, date = nil)
  raise ArgumentError, 'Invalid id' unless id
  @id   = id
  @date = parse_date(date)
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



21
22
23
# File 'lib/visitors/resource.rb', line 21

def date
  @date
end

#idObject (readonly)

Returns the value of attribute id.



21
22
23
# File 'lib/visitors/resource.rb', line 21

def id
  @id
end

Class Method Details

.find(resource_id) ⇒ Object



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

def find(resource_id)
  Hash[*MONTHS.map do |month|
    [month, stats_for(new(resource_id, month).to_redis_key)]
  end.flatten]
end

.incrObject



18
19
20
# File 'lib/visitors/resource.rb', line 18

def increment(resource_id, field)
  new(resource_id).increment(field)
end

.increment(resource_id, field) ⇒ Object



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

def increment(resource_id, field)
  new(resource_id).increment(field)
end

Instance Method Details

#increment(field) ⇒ Object



35
36
37
38
39
40
# File 'lib/visitors/resource.rb', line 35

def increment(field)
  assert_valid_field!(field.to_sym)

  redis.sadd(SET_NAME, id)
  redis.hincrby(key, field, 1)
end

#to_redis_keyObject Also known as: key



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

def to_redis_key
  [id, date].join(':')
end