nya_pr 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/nya_pr/paths.rb DELETED
@@ -1,60 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'fileutils'
4
-
5
- module NyaPr
6
- class Paths
7
- DEFAULT_DATA_DIR = 'data'
8
- REPOSITORIES_FILENAME = 'repositories.csv'
9
- PULL_REQUESTS_FILENAME = 'pull_requests.csv'
10
-
11
- attr_reader :repositories_path, :pull_requests_dir
12
-
13
- def initialize(
14
- repositories_path: nil,
15
- pull_requests_dir: nil,
16
- timestamp: Time.now
17
- )
18
- @repositories_path = repositories_path
19
- @pull_requests_dir = pull_requests_dir
20
- @timestamp = timestamp
21
- end
22
-
23
- def repositories_csv
24
- path = Pathname.new(
25
- repositories_path || File.join(DEFAULT_DATA_DIR, REPOSITORIES_FILENAME)
26
- )
27
-
28
- prepare_parent(path)
29
- path.to_s
30
- end
31
-
32
- def pull_requests_csv
33
- path = pull_requests_run_dir.join(PULL_REQUESTS_FILENAME)
34
-
35
- FileUtils.mkdir_p(path.dirname)
36
-
37
- path.to_s
38
- end
39
-
40
- private
41
-
42
- attr_reader :timestamp
43
-
44
- def pull_requests_run_dir
45
- @pull_requests_run_dir ||= begin
46
- base = Pathname.new(pull_requests_dir || DEFAULT_DATA_DIR)
47
-
48
- base.join("pull_requests_#{formatted_timestamp}")
49
- end
50
- end
51
-
52
- def formatted_timestamp
53
- timestamp.strftime('%Y%m%d_%H%M%S')
54
- end
55
-
56
- def prepare_parent(path)
57
- FileUtils.mkdir_p(path.dirname)
58
- end
59
- end
60
- end
@@ -1,97 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'oj'
4
-
5
- module NyaPr
6
- module Request
7
- include NyaPr::Connection
8
-
9
- MAX_PAGES = 1_000
10
- PER_PAGE = 100
11
-
12
- def get(path, params = {})
13
- _response, body = do_get(path, params)
14
-
15
- body
16
- end
17
-
18
- def paginate(path, params = {})
19
- params = params.merge(per_page: params.fetch(:per_page, PER_PAGE))
20
-
21
- page = 1
22
- result = []
23
-
24
- loop do
25
- raise NyaPr::Error, 'Pagination limit exceeded' if page > MAX_PAGES
26
-
27
- NyaPr.logger.debug("Fetching page #{page} for #{path}")
28
- response, body = do_get(path, params.merge(page: page))
29
-
30
- raise NyaPr::Error, 'Expected paginated response to be an array' unless body.is_a?(Array)
31
-
32
- result.concat(body)
33
-
34
- break if body.empty?
35
- break unless next_page?(response)
36
-
37
- page += 1
38
- end
39
-
40
- result
41
- end
42
-
43
- private
44
-
45
- def do_get(path, params = {})
46
- prepared_path = prepare(path)
47
-
48
- NyaPr.logger.debug("GET #{prepared_path} #{params.inspect}")
49
-
50
- response = connection(client).get(prepared_path, params)
51
- body = parse_body(response)
52
-
53
- NyaPr.logger.debug(
54
- "GET #{prepared_path} -> HTTP #{response.status}"
55
- )
56
-
57
- raise_on_error!(response, body, prepared_path)
58
-
59
- [response, body]
60
- end
61
-
62
- def parse_body(response)
63
- return nil if response.body.nil? || response.body.empty?
64
-
65
- Oj.load(response.body)
66
- rescue Oj::ParseError
67
- raise NyaPr::Error, "Failed to parse response: #{response.body}"
68
- end
69
-
70
- def raise_on_error!(response, body, path)
71
- return if response.success?
72
-
73
- message =
74
- if body.is_a?(Hash)
75
- body['message'] || body
76
- else
77
- body
78
- end
79
-
80
- raise NyaPr::Error.new(
81
- "GET #{path}: GitHub API error #{response.status}: #{message}",
82
- status: response.status
83
- )
84
- end
85
-
86
- def next_page?(response)
87
- response.headers['link']&.include?('rel="next"')
88
- end
89
-
90
- def prepare(path)
91
- path.
92
- delete_prefix('/').
93
- gsub(%r{//}, '/').
94
- gsub(%r{/+\z}, '')
95
- end
96
- end
97
- end