v3: add @warnings
This commit is contained in:
parent
b4a3bdeb0e
commit
09df061268
|
@ -37,4 +37,3 @@ module Travis
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
module Travis::API::V3
|
module Travis::API::V3
|
||||||
class Result
|
class Result
|
||||||
attr_accessor :access_control, :type, :resource, :status, :href, :meta_data
|
attr_accessor :access_control, :type, :resource, :status, :href, :meta_data, :warnings
|
||||||
|
|
||||||
def initialize(access_control, type, resource = [], status: 200, **meta_data)
|
def initialize(access_control, type, resource = [], status: 200, **meta_data)
|
||||||
|
@warnings = []
|
||||||
@access_control, @type, @resource, @status, @meta_data = access_control, type, resource, status, meta_data
|
@access_control, @type, @resource, @status, @meta_data = access_control, type, resource, status, meta_data
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -10,6 +11,15 @@ module Travis::API::V3
|
||||||
super or method.to_sym == type.to_sym
|
super or method.to_sym == type.to_sym
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def warn(message, **info)
|
||||||
|
warnings << { :@type => 'warning'.freeze, :message => message, **info }
|
||||||
|
end
|
||||||
|
|
||||||
|
def ignored_param(param, reason: nil, **info)
|
||||||
|
message = reason ? "query parameter #{param} #{reason}, ignored" : "query parameter #{param} ignored"
|
||||||
|
warn(message, warning_type: :ignored_parameter, parameter: param, **info)
|
||||||
|
end
|
||||||
|
|
||||||
def <<(value)
|
def <<(value)
|
||||||
resource << value
|
resource << value
|
||||||
self
|
self
|
||||||
|
@ -19,7 +29,7 @@ module Travis::API::V3
|
||||||
href = self.href
|
href = self.href
|
||||||
href = V3.location(env) if href.nil? and env['REQUEST_METHOD'.freeze] == 'GET'.freeze
|
href = V3.location(env) if href.nil? and env['REQUEST_METHOD'.freeze] == 'GET'.freeze
|
||||||
include = params['include'.freeze].to_s.split(?,.freeze)
|
include = params['include'.freeze].to_s.split(?,.freeze)
|
||||||
Renderer[type].render(resource,
|
add_info Renderer[type].render(resource,
|
||||||
href: href,
|
href: href,
|
||||||
script_name: env['SCRIPT_NAME'.freeze],
|
script_name: env['SCRIPT_NAME'.freeze],
|
||||||
include: include,
|
include: include,
|
||||||
|
@ -27,6 +37,14 @@ module Travis::API::V3
|
||||||
meta_data: meta_data)
|
meta_data: meta_data)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def add_info(payload)
|
||||||
|
if warnings.any?
|
||||||
|
payload = { :@warnings => [] }.merge!(payload) unless payload.include? :@warnings
|
||||||
|
payload[:@warnings].concat(warnings)
|
||||||
|
end
|
||||||
|
payload
|
||||||
|
end
|
||||||
|
|
||||||
def method_missing(method, *args)
|
def method_missing(method, *args)
|
||||||
return super unless method.to_sym == type.to_sym
|
return super unless method.to_sym == type.to_sym
|
||||||
raise ArgumentError, 'wrong number of arguments (1 for 0)'.freeze if args.any?
|
raise ArgumentError, 'wrong number of arguments (1 for 0)'.freeze if args.any?
|
||||||
|
|
|
@ -16,8 +16,11 @@ module Travis::API::V3
|
||||||
|
|
||||||
raise NotFound unless factory
|
raise NotFound unless factory
|
||||||
|
|
||||||
service = factory.new(access_control, factory.filter_params(env_params).merge(params))
|
filtered = factory.filter_params(env_params)
|
||||||
|
service = factory.new(access_control, filtered.merge(params))
|
||||||
result = service.run
|
result = service.run
|
||||||
|
|
||||||
|
env_params.each_key { |key| result.ignored_param(key, reason: "not whitelisted".freeze) unless filtered.include?(key) }
|
||||||
render(result, env_params, env)
|
render(result, env_params, env)
|
||||||
rescue Error => error
|
rescue Error => error
|
||||||
result = Result.new(access_control, :error, error)
|
result = Result.new(access_control, :error, error)
|
||||||
|
|
|
@ -130,14 +130,19 @@ describe Travis::API::V3::Services::Owner::Find do
|
||||||
before { get("/v3/owner/example-org?organization.id=#{other.id}") }
|
before { get("/v3/owner/example-org?organization.id=#{other.id}") }
|
||||||
example { expect(last_response).to be_ok }
|
example { expect(last_response).to be_ok }
|
||||||
example { expect(JSON.load(body)).to be == {
|
example { expect(JSON.load(body)).to be == {
|
||||||
"@type" => "organization",
|
"@type" => "organization",
|
||||||
"@href" => "/v3/org/#{org.id}",
|
"@href" => "/v3/org/#{org.id}",
|
||||||
"@permissions" => { "read"=>true, "sync"=>false },
|
"@permissions" => { "read"=>true, "sync"=>false },
|
||||||
"id" => org.id,
|
"id" => org.id,
|
||||||
"login" => "example-org",
|
"login" => "example-org",
|
||||||
"name" => nil,
|
"name" => nil,
|
||||||
"github_id" => nil,
|
"github_id" => nil,
|
||||||
"avatar_url" => nil
|
"avatar_url" => nil,
|
||||||
|
"@warnings" => [{
|
||||||
|
"@type" => "warning",
|
||||||
|
"message" => "query parameter organization.id not whitelisted, ignored",
|
||||||
|
"warning_type" => "ignored_parameter",
|
||||||
|
"parameter" => "organization.id"}]
|
||||||
}}
|
}}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -198,7 +203,12 @@ describe Travis::API::V3::Services::Owner::Find do
|
||||||
"github_id" => nil,
|
"github_id" => nil,
|
||||||
"avatar_url" => nil,
|
"avatar_url" => nil,
|
||||||
"is_syncing" => nil,
|
"is_syncing" => nil,
|
||||||
"synced_at" => nil
|
"synced_at" => nil,
|
||||||
|
"@warnings" => [{
|
||||||
|
"@type" => "warning",
|
||||||
|
"message" => "query parameter user.id not whitelisted, ignored",
|
||||||
|
"warning_type" => "ignored_parameter",
|
||||||
|
"parameter" => "user.id"}]
|
||||||
}}
|
}}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user