Class: V1::SpinsController

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

Overview

Spins controller Provides actions on the Spins

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

#destroyObject


42
43
44
# File 'app/controllers/v1/spins_controller.rb', line 42

def destroy
  # TODO
end

#downloadObject


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/v1/spins_controller.rb', line 76

def download
  @spin = Spin.find_by(id: params[:spin_id], visible: true) || Spin.find_by(name: params[:spin_id], visible: true)
  unless @spin
    render_error_exchange(:spin_not_found, :not_found)
    return
  end
  download_url = @spin.download_release(params[:release_id].to_s)
  if download_url
    @spin.downloads_count += 1
    @spin.save
    redirect_to download_url
  else
    render_error_exchange(:release_not_found, :not_found)
  end
end

#indexObject

Index (search: string - optional ) Provides an index of all spins in the system TODO If you provide a search team, it will return those spins mathing the search TODO: Add paging users/<user_id>/spins Get all spins of a user spins?query=<value> Look spins include value in the name


16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/v1/spins_controller.rb', line 16

def index
  @spins = Spin.where(visible:true)
  @spins = @spins.where('name ILIKE ?', "%#{params[:name]}%") if params[:name]
  @spins = @spins.joins(:user).where('users.github_login ILIKE ?', "%#{params[:author]}%") if params[:author]
  @spins = @spins.order("#{params[:sort]} #{params[:order] || 'DESC'}") if params[:sort]
  if @spins.count.positive?
    return_response @spins, :ok, {}
  else
    render status: :no_content
  end
end

#one_releaseObject


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/v1/spins_controller.rb', line 55

def one_release
  @spin = Spin.find_by(id: params[:spin_id], visible: true) || Spin.find_by(name: params[:spin_id], visible: true)
  unless @spin
    render_error_exchange(:spin_not_found, :not_found)
    return
  end
  target_release = {}
  @spin.format_releases.each do |release|
    if release[:id].to_s ==  params[:release_id].to_s
      target_release = release
      break;
    end
  end
  if target_release.empty?
    render_error_exchange(:release_not_found, :not_found)
    return
  else
    return_response target_release, :ok, {}
  end
end

#releasesObject


46
47
48
49
50
51
52
53
# File 'app/controllers/v1/spins_controller.rb', line 46

def releases
  @spin = Spin.find_by(id: params[:spin_id], visible: true) || Spin.find_by(name: params[:spin_id], visible: true)
  unless @spin
    render_error_exchange(:spin_not_found, :not_found)
    return
  end
  return_response @spin.format_releases, :ok, {}
end

#showObject

Show (id: identification of the spin) Provides a view of the spin TODO: When authenticated, provide extended info users/<user_id>/spins/<spin_id_or_name> Get a specific spin of user


33
34
35
36
37
38
39
40
# File 'app/controllers/v1/spins_controller.rb', line 33

def show
  @spin = Spin.find_by(id: params[:id], visible: true) || Spin.find_by(name: params[:id], visible: true)
  unless @spin
    render_error_exchange(:spin_not_found, :not_found)
    return
  end
  return_response  @spin,  :ok, {}
end