-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrepos.rb
More file actions
59 lines (36 loc) · 1.39 KB
/
repos.rb
File metadata and controls
59 lines (36 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
module Search
class Repos
def self.call(args = {})
query = args.delete(:q)
return Kaminari.paginate_array([], total_count: 0) if query.blank?
client = Octokit::Client.new(access_token: args.delete(:access_token))
options = search_options(args)
query = build_query(query, args)
results = client.search_repos(query, options)
repos = results[:items].map { |result| Repo.new(result) }
Kaminari.paginate_array(repos, total_count: results[:total_count]).page(options[:page]).per(options[:per_page])
end
def self.build_query(query, args)
qualifiers = args.slice(:in, :size, :forks, :fork, :created, :pushed, :user, :repo, :language, :stars).map { |qualifier, value| "#{qualifier}:#{value}" }
[query, qualifiers.join(' ')].reject(&:blank?).join(' ')
end
private_class_method :build_query
def self.search_options(args)
page = args.delete(:page).to_s.to_i
per_page = args.delete(:per_page).to_s.to_i
per_page = 100 if per_page < 1
order = args.delete(:order)
order = nil if order.blank?
sort = 'stars' unless order.nil?
{ page: page, per_page: per_page, sort: sort, order: order }
end
private_class_method :search_options
def initialize(args = {})
@args = args
end
def call(args = nil)
@args ||= args
self.class.call(@args)
end
end
end