Module: Wuclan::Twitter::Model::ModelCommon

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.flatten_date(dt) ⇒ Object

Convert date into flat, uniform format This method is idempotent: repeated calls give same result.



35
36
37
38
# File 'lib/wuclan/twitter/model/base.rb', line 35

def self.flatten_date dt
  return dt if dt =~ /\d{14}/
  DateTime.parse(dt).to_flat if dt
end

.unbooleanize(bool) ⇒ Object

Express boolean as 1 (true) or 0 (false). In contravention of typical ruby semantics (but in a way that is more robust for wukong-like batch processing), the number 0, the string ‘0’, nil and false are all considered false. (This also makes the method idempotent: repeated calls give same result.)



63
64
65
66
67
68
69
# File 'lib/wuclan/twitter/model/base.rb', line 63

def self.unbooleanize bool
  case bool
  when 0, '0', false, nil
    then 0
    else 1
  end
end

.zeropad_id(id) ⇒ Object

Zero-pad IDs to a full 10 digits (the max digits for an unsigned 32-bit integer).

nil id will be encoded as 0. Shit happens and we’d rather be idempotent than picky.

Note that sometime in 2010 (or sooner, depending on its growth rate: in 2008 Dec it was 1.8M/day) the status_id will exceed 32 bits. Something will happen then. This method is idempotent: repeated calls give same result.



52
53
54
55
# File 'lib/wuclan/twitter/model/base.rb', line 52

def self.zeropad_id id
  id ||= 0
  '%010d' % [id.to_i]
end

Instance Method Details

#days_since_createdObject



20
21
22
# File 'lib/wuclan/twitter/model/base.rb', line 20

def days_since_created
  (DateTime.now - DateTime.parse_safely(created_at)).to_f
end

#keyObject

By default, take the front num_key_fields of the flattened struct



8
9
10
# File 'lib/wuclan/twitter/model/base.rb', line 8

def key
  to_a[0..(num_key_fields-1)].join("-")
end

#scrape_ageObject

Metrics



16
17
18
# File 'lib/wuclan/twitter/model/base.rb', line 16

def scrape_age
  (DateTime.now - DateTime.parse_safely(scraped_at)).to_f
end