Class: Biostars::API::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/biostars/api/stats.rb

Overview

Statistics as of the Nth day after day-0 (the day of the first ever post) or statistics as of the given date.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Stats

Instantiate the Biostars::API::Stats.



43
44
45
46
47
# File 'lib/biostars/api/stats.rb', line 43

def initialize(attributes)
  attributes.each do |k,v| 
    instance_variable_set "@#{k}", v unless v.nil?
  end
end

Instance Attribute Details

#answersFixnum (readonly)

Returns total number of answers as of the given day/date.

Returns:

  • (Fixnum)

    total number of answers as of the given day/date.



10
11
12
# File 'lib/biostars/api/stats.rb', line 10

def answers
  @answers
end

#commentsFixnum (readonly)

Returns total number of comments as of the given day/date .

Returns:

  • (Fixnum)

    total number of comments as of the given day/date .



13
14
15
# File 'lib/biostars/api/stats.rb', line 13

def comments
  @comments
end

#dateString (readonly)

Returns the current date, ISO 8601 format.

Returns:

  • (String)

    the current date, ISO 8601 format



16
17
18
# File 'lib/biostars/api/stats.rb', line 16

def date
  @date
end

#new_postsArray (readonly)

Returns number of new posts in the given day/date .

Returns:

  • (Array)

    number of new posts in the given day/date .



19
20
21
# File 'lib/biostars/api/stats.rb', line 19

def new_posts
  @new_posts
end

#new_usersArray (readonly)

Returns number of new users in the given day/date .

Returns:

  • (Array)

    number of new users in the given day/date .



22
23
24
# File 'lib/biostars/api/stats.rb', line 22

def new_users
  @new_users
end

#new_votesArray (readonly)

Returns number of new votes in the given day/date .

Returns:

  • (Array)

    number of new votes in the given day/date .



25
26
27
# File 'lib/biostars/api/stats.rb', line 25

def new_votes
  @new_votes
end

#questionsFixnum (readonly)

Returns total number of questions as of the given day/date.

Returns:

  • (Fixnum)

    total number of questions as of the given day/date.



28
29
30
# File 'lib/biostars/api/stats.rb', line 28

def questions
  @questions
end

#timestampFixnum (readonly)

Returns date, unix epoch time format.

Returns:

  • (Fixnum)

    date, unix epoch time format.



31
32
33
# File 'lib/biostars/api/stats.rb', line 31

def timestamp
  @timestamp
end

#toplevelFixnum (readonly)

Returns total number of toplevel post as of the given day/date.

Returns:

  • (Fixnum)

    total number of toplevel post as of the given day/date.



34
35
36
# File 'lib/biostars/api/stats.rb', line 34

def toplevel
  @toplevel
end

#usersFixnum (readonly)

Returns total number of users as of the given day/date.

Returns:

  • (Fixnum)

    total number of users as of the given day/date.



37
38
39
# File 'lib/biostars/api/stats.rb', line 37

def users
  @users
end

#votesFixnum (readonly)

Returns total number of votes as of the given day/date.

Returns:

  • (Fixnum)

    total number of votes as of the given day/date.



40
41
42
# File 'lib/biostars/api/stats.rb', line 40

def votes
  @votes
end

Class Method Details

.find_by_date(year = Date.today.year, month = Date.today.month, day = (Date.today.day-1)) ⇒ Stats

Statistics as of the given date.

Parameters:

  • year (Fixnum) (defaults to: Date.today.year)

    year to search for

  • month (Fixnum) (defaults to: Date.today.month)

    month to search for

  • day (Fixnum) (defaults to: (Date.today.day-1))

    day to search for

Returns:

  • (Stats)

    returns a Stats object.

Raises:



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/biostars/api/stats.rb', line 101

def self.find_by_date(year=Date.today.year, month=Date.today.month, day=(Date.today.day-1))
  raise Biostars::StatsError unless year.is_a?(Fixnum) || month.is_a?(Fixnum) || day.is_a?(Fixnum)

  url = "stats/date/%s/%s/%s" % [
    year,
    sprintf('%02d', month),
    sprintf('%02d', day),
  ]

  find url
end

.find_by_day(day = Date.today.day) ⇒ Stats

Statistics as of the Nth day after day-0 (the day of the first ever post).

Parameters:

  • day (Date) (defaults to: Date.today.day)

    number of days after day-0, a number.

Returns:

  • (Stats)

    returns a Stats object.

Raises:



88
89
90
91
92
# File 'lib/biostars/api/stats.rb', line 88

def self.find_by_day(day=Date.today.day)
  raise Biostars::StatsError, "Expecting a Date Object" unless day.is_a? Fixnum

  find "stats/day/#{day}"
end

.latestStats

Helper method to look up stats for the prior date.

Returns:

  • (Stats)

    returns a Stats object.



79
80
81
# File 'lib/biostars/api/stats.rb', line 79

def self.latest
  find_by_date
end

Instance Method Details

#all_postsArray

Returns an Array of Post objects for all Posts on the given day/date.

Returns:

  • (Array)

    of Post objects.



52
53
54
55
56
# File 'lib/biostars/api/stats.rb', line 52

def all_posts
  new_posts.collect do |post_id|
    Biostars::API::Post.find post_id
  end
end

#all_usersArray

Returns an Array of User objects for all Users on the given day/date.

Returns:

  • (Array)

    of User objects.



70
71
72
73
74
# File 'lib/biostars/api/stats.rb', line 70

def all_users
  new_users.collect do |user_id|
    Biostars::API::User.find user_id
  end
end

#all_votesArray

Returns an Array of Vote objects for all Votes on the given day/date.

Returns:

  • (Array)

    of Vote objects.



61
62
63
64
65
# File 'lib/biostars/api/stats.rb', line 61

def all_votes
  new_votes.collect do |vote_id|
    Biostars::API::Vote.find vote_id
  end
end