class Agents::GithubNotificationsAgent

Public Instance Methods

check() click to toggle source

TODO: Fix rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize

# File lib/huginn_github_notifications_agent/github_notifications_agent.rb, line 57
def check
  response = HTTParty.get base_url, request_options
  # If there are no new notifications, you will get a "304 Not Modified"
  return if response.code == 304

  notifications = JSON.parse(response.body)
  if response.code > 400
    error("Error during http request: #{response.body}")
    return
  end
  notifications.each do |notif|
    data = notif['subject'].merge(
      repo_name: notif['repository']['full_name']
    )
    subject = ::HuginnGithubNotificationsAgent::Subject.new(data)
    notif['subject'] = subject.to_h
  end
  if emit_single_event?
    create_event payload: { notifications: notifications }
  else
    notifications.each { |notification| create_event payload: notification }
  end
  memory[:last_modified] = response.headers['last-modified']
end
default_options() click to toggle source
# File lib/huginn_github_notifications_agent/github_notifications_agent.rb, line 31
def default_options
  {
    'access_token' => 'my_gh_access_token',
    'events' => 'multiple',
    'last_modified' => true
  }
end
validate_options() click to toggle source
# File lib/huginn_github_notifications_agent/github_notifications_agent.rb, line 39
def validate_options
  unless options['access_token'].present?
    errors.add(:base, 'access_token is required ')
  end
  # rubocop:disable Style/GuardClause
  if last_modified.present? && boolify(last_modified).nil?
    errors.add(:base, 'last_modified must be a boolean value')
  end
  # rubocop:enable Style/GuardClause
end
working?() click to toggle source
# File lib/huginn_github_notifications_agent/github_notifications_agent.rb, line 50
def working?
  !recent_error_logs?
end

Private Instance Methods

base_url() click to toggle source
# File lib/huginn_github_notifications_agent/github_notifications_agent.rb, line 98
def base_url
  'https://api.github.com/notifications'
end
default_headers() click to toggle source
# File lib/huginn_github_notifications_agent/github_notifications_agent.rb, line 108
def default_headers
  {
    'User-Agent' => 'Huginn (https://github.com/cantino/huginn)',
    'Authorization' => 'token ' + interpolated['access_token']
  }
end
emit_single_event?() click to toggle source

rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize

# File lib/huginn_github_notifications_agent/github_notifications_agent.rb, line 86
def emit_single_event?
  options['events'] == 'single'
end
extra_headers() click to toggle source
# File lib/huginn_github_notifications_agent/github_notifications_agent.rb, line 115
def extra_headers
  if use_last_modified?
    { 'If-Modified-Since' => memory[:last_modified] }
  else
    {}
  end
end
last_modified() click to toggle source
# File lib/huginn_github_notifications_agent/github_notifications_agent.rb, line 90
def last_modified
  options['last_modified']
end
request_options() click to toggle source
# File lib/huginn_github_notifications_agent/github_notifications_agent.rb, line 102
def request_options
  {
    headers: default_headers.merge(extra_headers)
  }
end
use_last_modified?() click to toggle source
# File lib/huginn_github_notifications_agent/github_notifications_agent.rb, line 94
def use_last_modified?
  memory[:last_modified].present? && boolify(last_modified)
end