v3: add attributes to service index
This commit is contained in:
parent
9b8a99ea33
commit
9449ada1cd
|
@ -9,9 +9,12 @@ module Travis::API::V3
|
||||||
|
|
||||||
attr_accessor :resolver_cache
|
attr_accessor :resolver_cache
|
||||||
|
|
||||||
def [](key)
|
def [](key, raise_unknown = true)
|
||||||
return key unless key.is_a? Symbol
|
return key unless key.is_a? Symbol
|
||||||
resolver_cache[key] ||= const_get(key.to_s.camelize, false)
|
resolver_cache[key] ||= const_get(key.to_s.camelize, false)
|
||||||
|
rescue NameError => e
|
||||||
|
raise e if raise_unknown
|
||||||
|
raise e unless e.message.include?(key.to_s.camelize)
|
||||||
end
|
end
|
||||||
|
|
||||||
def extended(base)
|
def extended(base)
|
||||||
|
|
|
@ -14,7 +14,7 @@ module Travis::API::V3
|
||||||
def self.representation(name, *fields)
|
def self.representation(name, *fields)
|
||||||
fields.each do |field|
|
fields.each do |field|
|
||||||
class_eval "def #{field}; @model.#{field}; end" unless method_defined?(field)
|
class_eval "def #{field}; @model.#{field}; end" unless method_defined?(field)
|
||||||
available_fields << field.to_s
|
available_attributes << field.to_s
|
||||||
end
|
end
|
||||||
representations[name] = fields
|
representations[name] = fields
|
||||||
end
|
end
|
||||||
|
@ -23,8 +23,8 @@ module Travis::API::V3
|
||||||
@representations ||= {}
|
@representations ||= {}
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.available_fields
|
def self.available_attributes
|
||||||
@available_fields ||= Set.new
|
@available_attributes ||= Set.new
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.render(model, representation = :standard, **options)
|
def self.render(model, representation = :standard, **options)
|
||||||
|
@ -70,7 +70,7 @@ module Travis::API::V3
|
||||||
include.each do |qualified_field|
|
include.each do |qualified_field|
|
||||||
raise WrongParams, 'illegal format for include parameter'.freeze unless /\A(?<prefix>\w+)\.(?<field>\w+)\Z$/ =~ qualified_field
|
raise WrongParams, 'illegal format for include parameter'.freeze unless /\A(?<prefix>\w+)\.(?<field>\w+)\Z$/ =~ qualified_field
|
||||||
next if prefix != excepted_type
|
next if prefix != excepted_type
|
||||||
raise WrongParams, 'no field %p to include'.freeze % qualified_field unless self.class.available_fields.include?(field)
|
raise WrongParams, 'no field %p to include'.freeze % qualified_field unless self.class.available_attributes.include?(field)
|
||||||
|
|
||||||
field &&= field.to_sym
|
field &&= field.to_sym
|
||||||
fields << field unless fields.include?(field)
|
fields << field unless fields.include?(field)
|
||||||
|
|
|
@ -25,16 +25,19 @@ module Travis::API::V3
|
||||||
def render_json
|
def render_json
|
||||||
resources = { }
|
resources = { }
|
||||||
routes.resources.each do |resource|
|
routes.resources.each do |resource|
|
||||||
resources[resource.identifier] ||= {}
|
data = resources[resource.identifier] ||= { :@type => :resource, :actions => {} }
|
||||||
|
if renderer = Renderer[resource.identifier, false] and renderer.respond_to? :available_attributes
|
||||||
|
data[:attributes] = renderer.available_attributes
|
||||||
|
end
|
||||||
resource.services.each do |(request_method, sub_route), service|
|
resource.services.each do |(request_method, sub_route), service|
|
||||||
list = resources[resource.identifier][service] ||= []
|
list = resources[resource.identifier][:actions][service] ||= []
|
||||||
pattern = sub_route ? resource.route + sub_route : resource.route
|
pattern = sub_route ? resource.route + sub_route : resource.route
|
||||||
pattern.to_templates.each do |template|
|
pattern.to_templates.each do |template|
|
||||||
list << { 'request_method'.freeze => request_method, 'uri_template'.freeze => prefix + template }
|
list << { :@type => :template, :request_method => request_method, :uri_template => prefix + template }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
{ :@type => 'home'.freeze, :resources => resources }
|
{ :@type => :home, :resources => resources }
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_json_home
|
def render_json_home
|
||||||
|
|
|
@ -7,25 +7,91 @@ describe Travis::API::V3::ServiceIndex do
|
||||||
let(:response) { get(path, {}, headers) }
|
let(:response) { get(path, {}, headers) }
|
||||||
|
|
||||||
describe "custom json entry point" do
|
describe "custom json entry point" do
|
||||||
let(:expected_resources) {{
|
let(:expected_resources) {
|
||||||
"repository" => {
|
{"requests"=>
|
||||||
"find" => [{"request_method"=>"GET", "uri_template"=>"#{path}repo/{repository.id}"}],
|
{"@type"=>"resource",
|
||||||
"enable" => [{"request_method"=>"POST", "uri_template"=>"#{path}repo/{repository.id}/enable"}],
|
"actions"=>
|
||||||
"disable" => [{"request_method"=>"POST", "uri_template"=>"#{path}repo/{repository.id}/disable"}] },
|
{"find"=>
|
||||||
"repositories" => {
|
[{"@type"=>"template",
|
||||||
"for_current_user" => [{"request_method"=>"GET", "uri_template"=>"#{path}repos"}] },
|
"request_method"=>"GET",
|
||||||
"branch" => {
|
"uri_template"=>"#{path}repo/{repository.id}/requests"}],
|
||||||
"find" => [{"request_method"=>"GET", "uri_template"=>"#{path}repo/{repository.id}/branch/{branch.name}"}]},
|
"create"=>
|
||||||
"build" => {
|
[{"@type"=>"template",
|
||||||
"find" => [{"request_method"=>"GET", "uri_template"=>"#{path}build/{build.id}"}] },
|
"request_method"=>"POST",
|
||||||
"organizations" => {
|
"uri_template"=>"#{path}repo/{repository.id}/requests"}]}},
|
||||||
"for_current_user" => [{"request_method"=>"GET", "uri_template"=>"#{path}orgs"}] },
|
"branch"=>
|
||||||
"organization" => {
|
{"@type"=>"resource",
|
||||||
"find" => [{"request_method"=>"GET", "uri_template"=>"#{path}org/{organization.id}"}] },
|
"actions"=>
|
||||||
"requests" => {
|
{"find"=>
|
||||||
"find" => [{"request_method"=>"GET", "uri_template"=>"#{path}repo/{repository.id}/requests"}],
|
[{"@type"=>"template",
|
||||||
"create" => [{"request_method"=>"POST", "uri_template"=>"#{path}repo/{repository.id}/requests"}]}
|
"request_method"=>"GET",
|
||||||
}}
|
"uri_template"=>"#{path}repo/{repository.id}/branch/{branch.name}"}]},
|
||||||
|
"attributes"=>["name", "last_build", "repository"]},
|
||||||
|
"repository"=>
|
||||||
|
{"@type"=>"resource",
|
||||||
|
"actions"=>
|
||||||
|
{"find"=>
|
||||||
|
[{"@type"=>"template",
|
||||||
|
"request_method"=>"GET",
|
||||||
|
"uri_template"=>"#{path}repo/{repository.id}"}],
|
||||||
|
"enable"=>
|
||||||
|
[{"@type"=>"template",
|
||||||
|
"request_method"=>"POST",
|
||||||
|
"uri_template"=>"#{path}repo/{repository.id}/enable"}],
|
||||||
|
"disable"=>
|
||||||
|
[{"@type"=>"template",
|
||||||
|
"request_method"=>"POST",
|
||||||
|
"uri_template"=>"#{path}repo/{repository.id}/disable"}]},
|
||||||
|
"attributes"=>
|
||||||
|
["id",
|
||||||
|
"slug",
|
||||||
|
"name",
|
||||||
|
"description",
|
||||||
|
"github_language",
|
||||||
|
"active",
|
||||||
|
"private",
|
||||||
|
"owner",
|
||||||
|
"last_build",
|
||||||
|
"default_branch"]},
|
||||||
|
"repositories"=>
|
||||||
|
{"@type"=>"resource",
|
||||||
|
"actions"=>
|
||||||
|
{"for_current_user"=>
|
||||||
|
[{"@type"=>"template",
|
||||||
|
"request_method"=>"GET",
|
||||||
|
"uri_template"=>"#{path}repos"}]}},
|
||||||
|
"build"=>
|
||||||
|
{"@type"=>"resource",
|
||||||
|
"actions"=>
|
||||||
|
{"find"=>
|
||||||
|
[{"@type"=>"template",
|
||||||
|
"request_method"=>"GET",
|
||||||
|
"uri_template"=>"#{path}build/{build.id}"}]},
|
||||||
|
"attributes"=>
|
||||||
|
["id",
|
||||||
|
"number",
|
||||||
|
"state",
|
||||||
|
"duration",
|
||||||
|
"started_at",
|
||||||
|
"finished_at",
|
||||||
|
"repository",
|
||||||
|
"branch"]},
|
||||||
|
"organization"=>
|
||||||
|
{"@type"=>"resource",
|
||||||
|
"actions"=>
|
||||||
|
{"find"=>
|
||||||
|
[{"@type"=>"template",
|
||||||
|
"request_method"=>"GET",
|
||||||
|
"uri_template"=>"#{path}org/{organization.id}"}]},
|
||||||
|
"attributes"=>["id", "login", "name", "github_id"]},
|
||||||
|
"organizations"=>
|
||||||
|
{"@type"=>"resource",
|
||||||
|
"actions"=>
|
||||||
|
{"for_current_user"=>
|
||||||
|
[{"@type"=>"template",
|
||||||
|
"request_method"=>"GET",
|
||||||
|
"uri_template"=>"#{path}orgs"}]}}}
|
||||||
|
}
|
||||||
|
|
||||||
describe 'with /v3 prefix' do
|
describe 'with /v3 prefix' do
|
||||||
let(:path) { '/v3/' }
|
let(:path) { '/v3/' }
|
||||||
|
|
Loading…
Reference in New Issue
Block a user