Class: Fluent::GithubActivities::UsersManager

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/github-activities/users_manager.rb

Constant Summary collapse

DEFAULT_LAST_EVENT_TIMESTAMP =
-1

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ UsersManager

Returns a new instance of UsersManager.



30
31
32
33
34
35
36
# File 'lib/fluent/plugin/github-activities/users_manager.rb', line 30

def initialize(params={})
  @users = params[:users]

  @positions = {}
  @pos_file = params[:pos_file]
  @pos_file = Pathname(@pos_file) if @pos_file
end

Instance Method Details

#generate_initial_requestsObject



38
39
40
41
42
# File 'lib/fluent/plugin/github-activities/users_manager.rb', line 38

def generate_initial_requests
  @users.collect do |user|
    new_events_request(user)
  end
end

#new_events_request(user, options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fluent/plugin/github-activities/users_manager.rb', line 44

def new_events_request(user, options={})
  request = {
    :type => TYPE_EVENTS,
    :user => user,
  }
  response = options[:previous_response]
  if response
    now = options[:now] || Time.now
    interval = response["X-Poll-Interval"].to_i
    time_to_process = now.to_i + interval
    request[:previous_entity_tag] = response["ETag"] ||
                                      options[:previous_entity_tag]
    request[:process_after] = time_to_process
  else
    request[:previous_entity_tag] = options[:previous_entity_tag]
  end
  request
end

#position_for(user) ⇒ Object



63
64
65
66
# File 'lib/fluent/plugin/github-activities/users_manager.rb', line 63

def position_for(user)
  load_positions
  @positions[user]
end

#save_position_for(user, params) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fluent/plugin/github-activities/users_manager.rb', line 68

def save_position_for(user, params)
  load_positions
  @positions[user] ||= {}

  if params[:entity_tag]
    @positions[user]["entity_tag"] = params[:entity_tag]
  end

  if params[:last_event_timestamp] and
       params[:last_event_timestamp] != DEFAULT_LAST_EVENT_TIMESTAMP
    old_timestamp = @positions[user]["last_event_timestamp"]
    if old_timestamp.nil? or old_timestamp < params[:last_event_timestamp]
      @positions[user]["last_event_timestamp"] = params[:last_event_timestamp]
    end
  end

  save_positions
end