Class: SentryTopErrors::SentryClient

Inherits:
Object
  • Object
show all
Defined in:
lib/sentry_top_errors/sentry_client.rb

Constant Summary collapse

SENTRY_HOST =
'https://sentry.io'
HTTP_OPTIONNS =
{
  tcp_nodelay: true,
  connect_timeout: 10,
  read_timeout: 40,
  idempotent: true,
  retry_limit: 3
}

Instance Method Summary collapse

Constructor Details

#initialize(sentry_key:, enable_cache: false) ⇒ SentryClient

Returns a new instance of SentryClient.



11
12
13
14
# File 'lib/sentry_top_errors/sentry_client.rb', line 11

def initialize(sentry_key: , enable_cache: false)
  @sentry_key = sentry_key
  @enable_cache = enable_cache
end

Instance Method Details

#cached(key, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sentry_top_errors/sentry_client.rb', line 59

def cached(key, &block)
  if @enable_cache
    cache_file = "sentry_cache/#{key}.yaml"
    if File.exist?(cache_file)
      YAML.load_file(cache_file, aliases: true, permitted_classes: [
        SentryTopErrors::ApiResponse::FromArray, SentryTopErrors::ApiResponse::FromHash, Symbol, Excon::Response, Excon::Headers
      ])
    else
      result = block.call()
      File.write(cache_file, YAML.dump(result))
      result
    end
  else
    block.call()
  end
end

#drop_cacheObject



76
77
78
79
80
# File 'lib/sentry_top_errors/sentry_client.rb', line 76

def drop_cache
  Dir.glob("sentry_cache/*.yaml").each do |file|
    File.unlink(file)
  end
end

#get_all_with_cursor(http_method, url_path, http_options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sentry_top_errors/sentry_client.rb', line 25

def get_all_with_cursor(http_method, url_path, http_options = {})
  objects = http(http_method, url_path, http_options)
  link_header = objects.headers['link']

  while link_header && link_header =~ /rel="next"/
    cursor_match = link_header.match(/results="true";\s+cursor="([\d:]+?)"$/)
    if cursor_match
      http_options[:query] ||= {}
      http_options[:query][:cursor] = cursor_match[1]
      next_objects = http(http_method, url_path, http_options)
      objects.concat(next_objects)
      link_header = next_objects.headers['link']
    else
      link_header = ""
      # puts "No next link in response"
    end
  end

  objects
end

#http(method, path, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/sentry_top_errors/sentry_client.rb', line 16

def http(method, path, options = {})
  @conn ||= Excon.new(SENTRY_HOST, HTTP_OPTIONNS)
  options[:headers] ||= {}
  options[:headers]['Authorization'] ||= "Bearer #{@sentry_key}"
  response = @conn.request({method: method, path: path}.merge(options))

  SentryTopErrors::ApiResponse.new_from_res(response)
end

#list_issues(org_slug, project_slug, stats_period: '24h') ⇒ Object



52
53
54
55
56
57
# File 'lib/sentry_top_errors/sentry_client.rb', line 52

def list_issues(org_slug, project_slug, stats_period: '24h')
  cached("list_issues_#{project_slug}") do
    query = {statsPeriod: stats_period}
    get_all_with_cursor(:get, "api/0/projects/#{org_slug}/#{project_slug}/issues/", query: query)
  end
end

#list_projectsObject



46
47
48
49
50
# File 'lib/sentry_top_errors/sentry_client.rb', line 46

def list_projects
  cached(:all_projects) do
    get_all_with_cursor(:get, 'api/0/projects/')
  end
end