Class: V1::TopController

Inherits:
ApiController show all
Defined in:
app/controllers/v1/top_controller.rb

Constant Summary

Constants included from ApiController::Parameters

ApiController::Parameters::PARAMS

Instance Method Summary collapse

Methods inherited from ApiController

#check_params_required, #return_response, #version

Methods included from ApiController::Metadata

#meta_attributes, #meta_pagination

Methods included from ApiController::Pagination

#compile_page, #select_page

Methods included from ApiController::Parameters

#check_params, #expand_resources?, #numeric?, #true?

Instance Method Details

#indexObject


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
# File 'app/controllers/v1/top_controller.rb', line 3

def index

  @spin_starred = Spin.select('id','full_name','stargazers_count').order('stargazers_count DESC').limit(10)
  @spin_watched = Spin.select('id','full_name','watchers_count').order('watchers_count DESC').limit(10)
  @spin_downloaded= Spin.select('id','full_name','downloads_count').order('downloads_count DESC').limit(10)

  @top_tags = Tag.joins(:spins).group("name","id").order('count_id DESC').count("id")
  @top_contributors = User.joins(:spins).group("github_login","id").order('count_id DESC').count("id")
  @newest_users = User.select("id","github_login","created_at").order('created_at DESC').limit(10)

  most_starred = []
  @spin_starred.each do |spin|
    most_starred = most_starred.push({ "name": spin.full_name, "Stars": spin.stargazers_count, "id": spin.id })
  end

  most_watched = []
  @spin_watched.each do |spin|
    most_watched = most_watched.push({ "name": spin.full_name, "Watchers": spin.watchers_count, "id": spin.id })
  end

  most_download = []
  @spin_downloaded.each do |spin|
    most_download = most_download.push({ "name": spin.full_name, "Downloads": spin.downloads_count, "id": spin.id })
  end

  top_tags = []
  @top_tags.each do |tag, count|
    break if top_tags.count == 10
    top_tags = top_tags.push({ "name": tag.first, "# Spins": count, "id": tag.second })
  end

  top_contributors = []
  @top_contributors.each do |user, count|
    break if top_contributors.count == 10
    top_contributors = top_contributors.push({ "name": user.first, "# Spins": count, "id": user.second })
  end

  newest = []
  @newest_users.each do |user|
    newest = newest.push({ "name": user., "Added on": user.created_at, "id": user.id })
  end

  response = {
    "data": {
      "Most Starred": most_starred,
      "Most Watched": most_watched,
      "Most Downloaded": most_download,
      "Top Tags": top_tags,
      "Top Contributors": top_contributors,
      "Newest": newest
    }
  }

  return_response response, :ok, {}
end