Refactor repeated pattern into extracted method
Seems like we'll be checking login status before finding a resource a lot, so here's a standard way to do it.
This commit is contained in:
parent
81e93ca710
commit
88d9000042
|
@ -30,6 +30,10 @@ module Travis::API::V3
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def full_access_or_logged_in?
|
||||||
|
full_access? || logged_in?
|
||||||
|
end
|
||||||
|
|
||||||
def visible_repositories(list)
|
def visible_repositories(list)
|
||||||
# naïve implementation, replaced with smart implementation in specific subclasses
|
# naïve implementation, replaced with smart implementation in specific subclasses
|
||||||
return list if full_access?
|
return list if full_access?
|
||||||
|
|
|
@ -66,6 +66,11 @@ module Travis::API::V3
|
||||||
object
|
object
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def check_login_and_find(*args)
|
||||||
|
raise LoginRequired unless access_control.full_access_or_logged_in?
|
||||||
|
find(*args)
|
||||||
|
end
|
||||||
|
|
||||||
def not_found(actually_not_found = false, type = nil)
|
def not_found(actually_not_found = false, type = nil)
|
||||||
type, actually_not_found = actually_not_found, false if actually_not_found.is_a? Symbol
|
type, actually_not_found = actually_not_found, false if actually_not_found.is_a? Symbol
|
||||||
error = actually_not_found ? EntityMissing : NotFound
|
error = actually_not_found ? EntityMissing : NotFound
|
||||||
|
|
|
@ -4,8 +4,7 @@ module Travis::API::V3
|
||||||
params :id, prefix: :env_var
|
params :id, prefix: :env_var
|
||||||
|
|
||||||
def run!
|
def run!
|
||||||
raise LoginRequired unless access_control.logged_in? or access_control.full_access?
|
repository = check_login_and_find(:repository)
|
||||||
raise NotFound unless repository = find(:repository)
|
|
||||||
query.delete(repository)
|
query.delete(repository)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,8 +4,7 @@ module Travis::API::V3
|
||||||
params :id, prefix: :env_var
|
params :id, prefix: :env_var
|
||||||
|
|
||||||
def run!
|
def run!
|
||||||
raise LoginRequired unless access_control.logged_in? or access_control.full_access?
|
repository = check_login_and_find(:repository)
|
||||||
raise NotFound unless repository = find(:repository)
|
|
||||||
query.find(repository)
|
query.find(repository)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,8 +4,7 @@ module Travis::API::V3
|
||||||
params :id, :name, :value, :public, prefix: :env_var
|
params :id, :name, :value, :public, prefix: :env_var
|
||||||
|
|
||||||
def run!
|
def run!
|
||||||
raise LoginRequired unless access_control.logged_in? or access_control.full_access?
|
repository = check_login_and_find(:repository)
|
||||||
raise NotFound unless repository = find(:repository)
|
|
||||||
query.update(repository)
|
query.update(repository)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,8 +4,7 @@ module Travis::API::V3
|
||||||
params :id, :name, :value, :public, prefix: :env_var
|
params :id, :name, :value, :public, prefix: :env_var
|
||||||
|
|
||||||
def run!
|
def run!
|
||||||
raise LoginRequired unless access_control.logged_in? or access_control.full_access?
|
repository = check_login_and_find(:repository)
|
||||||
raise NotFound unless repository = find(:repository)
|
|
||||||
env_var = query(:env_vars).create(repository)
|
env_var = query(:env_vars).create(repository)
|
||||||
result(:env_var, env_var, status: 201)
|
result(:env_var, env_var, status: 201)
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
module Travis::API::V3
|
module Travis::API::V3
|
||||||
class Services::EnvVars::ForRepository < Service
|
class Services::EnvVars::ForRepository < Service
|
||||||
def run!
|
def run!
|
||||||
raise LoginRequired unless access_control.logged_in? or access_control.full_access?
|
repository = check_login_and_find(:repository)
|
||||||
raise NotFound unless repository = find(:repository)
|
|
||||||
find(:env_vars, repository)
|
find(:env_vars, repository)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user