Skip to content

Commit

Permalink
Merge 5be6633 into 0b45ebf
Browse files Browse the repository at this point in the history
  • Loading branch information
aljesusg committed Feb 8, 2018
2 parents 0b45ebf + 5be6633 commit 80a8a07
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 30 deletions.
13 changes: 13 additions & 0 deletions app/controllers/v1/spin_candidates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,18 @@ def publish
# If valid create or update the Spin
# If not valid, the log should be updated.
end

def unpublish
sc = SpinCandidate.find(params[:spin_candidate_id])
if sc.spin
if sc.unpublish
return_response sc, :ok, {}
else
render_error_exchange(:spin_candidate_not_published, :precondition_failed)
end
else
render_error_exchange(:spin_candidate_not_published, :not_found)
end
end
end
end
67 changes: 49 additions & 18 deletions app/controllers/v1/spins_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module V1
#
##
class SpinsController < ApiController
before_action :authenticate_user!, except: [:index, :show]
before_action :authenticate_user!, except: [:index, :show, :releases, :one_release, :download]
###
# Index (search: string - optional )
# Provides an index of all spins in the system
Expand All @@ -14,16 +14,7 @@ class SpinsController < ApiController
# users/<user_id>/spins Get all spins of a user
# spins?query=<value> Look spins include value in the name
def index
if params[:user_id]
@user = User.find_by_github_login(params[:user_id])
unless @user
return_response status: :no_content
return
end
@spins = Spin.where(user_id: @user.id, visible: true ) if @user
else
@spins = Spin.where(visible:true)
end
@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]
Expand All @@ -40,13 +31,7 @@ def index
# TODO: When authenticated, provide extended info
# users/<user_id>/spins/<spin_id_or_name> Get a specific spin of user
def show
if params[:user_id]
@user = User.find_by_github_login(params[:user_id])
return_response status: :not_found unless @user
@spin = Spin.find_by(user_id: @user.id, visible: true, id: params[:id]) || Spin.find_by(user_id: @user.id, visible: true, name: params[:id])
else
@spin = Spin.find_by(id: params[:id], visible: true) || Spin.find_by(name: params[:id], visible: true)
end
@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
Expand All @@ -57,5 +42,51 @@ def show
def destroy
# TODO
end

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

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

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
end
end
60 changes: 59 additions & 1 deletion app/models/spin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,10 @@ def update_values(user)
self.min_miq_version= metadata_json['min_miq_version'].downcase.bytes[0] - 'a'.bytes[0],
self.metadata = metadata_json
self.metadata_raw = metadata_raw
self.releases= releases,
self.releases= go_json(releases),
self.user= user,
self.user_login= user.github_login
self.downloads_url = ''
true
end

Expand Down Expand Up @@ -187,4 +188,61 @@ def refresh_tags
spin_log(validation) unless validation.nil?
end
end

def go_json(data)
releases = []
data.each do |one_release|
obj = {}
one_release.each do |k,v|
if k.to_s != "author"
obj[k.to_s] = v
else
author_json = {}
v.each do |kt,vt|
author_json[kt.to_s] = vt
end
obj[k.to_s] = author_json
end
end
releases.push(obj)
end
{ "releases":releases }
end

def format_releases(id = nil)
result = []
releases.first["releases"].each do |rele|
if id && id == rele["id"].to_s
result = rele["zipball_url"]
break;
else
result = result.push(
{
id: rele["id"],
draft: rele["draft"],
tag: rele["tag_name"],
name: rele["name"],
prerelease: rele["prerelease"],
created_at: rele["created_at"],
published_at: rele["published_at"],
author:{
login: rele["author"]["login"],
id: rele["author"]["id"],
url: rele["author"]["html_url"],
avatar_url: rele["author"]["avatar_url"]
}
}
)
end
end
result
end

def download_release(release_id)
result = format_releases(release_id)
if result.empty?
return nil
end
result
end
end
9 changes: 9 additions & 0 deletions app/models/spin_candidate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ def publish_spin user:
end
false
end

def unpublish
spin.visible = false
if spin.save
self.published = false
return true if save
end
false
end
end
6 changes: 6 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ en:
user_not_found:
title: This user is not in the database
detail: Maybe the id of user is wrong
release_not_found:
title: Relase not found
detail: Maybe the id is wrong
spin_candidate_not_found:
title: Spin Candidate not found in the database
detail: Check that the id of the spin exists
Expand All @@ -88,3 +91,6 @@ en:
spin_candidate_not_validated:
title: Spin Candidate is not validated
detail: Check de extra information or logs
spin_candidate_not_published:
title: Spin Candidate is not published
detail: You need to publish the spin
7 changes: 6 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,16 @@
post 'refresh'
end
post 'publish', to: 'spin_candidates#publish'
post 'unpublish', to: 'spin_candidates#unpublish'
post 'validate', to: 'spin_candidates#validate'
end

resources :tags, only: [:index, :show]
resources :spins, only: [:index, :show]
resources :spins, only: [:index, :show] do
get 'releases', to: 'spins#releases'
get 'releases/:release_id', to: 'spins#one_release'
get 'releases/:release_id/download', to: 'spins#download'
end
end


Expand Down
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180208112242) do
ActiveRecord::Schema.define(version: 20180208130939) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -79,6 +79,7 @@
t.jsonb "releases", default: []
t.bigint "spin_candidate_id"
t.integer "downloads_count", default: 0
t.string "downloads_url", default: ""
t.index ["published"], name: "index_spins_on_published"
t.index ["spin_candidate_id"], name: "index_spins_on_spin_candidate_id"
t.index ["user_id"], name: "index_spins_on_user_id"
Expand Down
4 changes: 2 additions & 2 deletions product/json_returns.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ users:
github_avatar_url: "avatar"
github_company: "company"
spins:
attributes: ["id","user_login", "name", "description", "watchers_count" ,"stargazers_count"]
expand: ["readme", "license_name", "license_html_url", "license_key", "default_branch", "user_id", "clone_url", "forks_count", "open_issues_count", "metadata", "releases"]
attributes: ["id","user_login", "name", "description", "watchers_count" ,"stargazers_count", "downloads_count"]
expand: ["readme", "license_name", "license_html_url", "license_key", "default_branch", "user_id", "clone_url", "forks_count", "open_issues_count", "metadata"]
staff: ["created_at", "updated_at"]
admin: ["visible", "published"]
columns:
Expand Down
16 changes: 9 additions & 7 deletions spec/requests/v1/spins_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@
expect(json['data'].length).to eq(3)
end

it 'all spins of an existing user' do
get "/#{prefix}/users/#{user.github_login}/spins"
expect(response).to have_http_status(200)
expect(json).to be_kind_of(Hash)
expect(json['data']).to be_kind_of(Array)
expect(json['data'].length).to eq(2)
end
# it 'all spins of an existing user' do
# get "/#{prefix}/users/#{user.github_login}/spins"
# expect(response).to have_http_status(200)
# expect(json).to be_kind_of(Hash)
# expect(json['data']).to be_kind_of(Array)
# expect(json['data'].length).to eq(2)
# end

pending 'Look spins of a user'

it 'one spin by name' do
get "/#{prefix}/spins/#{spin_exchange.name}"
Expand Down

0 comments on commit 80a8a07

Please sign in to comment.