moar refactoring on responders, fix specs
This commit is contained in:
parent
3f5cf5f33a
commit
903f249cfb
|
@ -7,7 +7,7 @@ class Travis::Api::App
|
||||||
class Artifacts < Endpoint
|
class Artifacts < Endpoint
|
||||||
# Fetches an artifact by it's *id*.
|
# Fetches an artifact by it's *id*.
|
||||||
get '/:id' do |id|
|
get '/:id' do |id|
|
||||||
respond_with one(params) || not_found
|
respond_with one(params)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,7 +8,7 @@ class Travis::Api::App
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/:id' do
|
get '/:id' do
|
||||||
respond_with one(params).run || not_found # TODO hrmmmmmm
|
respond_with one(params)
|
||||||
end
|
end
|
||||||
|
|
||||||
# get '/repositories/:repository_id/builds' do # v1
|
# get '/repositories/:repository_id/builds' do # v1
|
||||||
|
|
|
@ -8,8 +8,7 @@ class Travis::Api::App
|
||||||
end
|
end
|
||||||
|
|
||||||
put '/:id?', scope: :private do
|
put '/:id?', scope: :private do
|
||||||
update(id: params[:id] || params[:hook][:id], active: params[:hook][:active])
|
respond_with update(id: params[:id] || params[:hook][:id], active: params[:hook][:active])
|
||||||
204
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,7 +8,7 @@ class Travis::Api::App
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/:id' do
|
get '/:id' do
|
||||||
respond_with one(params).run || not_found # TODO hrmmmmmm
|
respond_with one(params).run
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,17 +9,17 @@ class Travis::Api::App
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/:id' do
|
get '/:id' do
|
||||||
respond_with one(params).run || not_found
|
respond_with one(params)
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO the format constraint neither seems to work nor fail?
|
# TODO the format constraint neither seems to work nor fail?
|
||||||
get '/:id/cc.:format', format: 'xml' do # v1
|
get '/:id/cc.:format', format: 'xml' do # v1
|
||||||
respond_with one(params).run || not_found
|
respond_with one(params)
|
||||||
end
|
end
|
||||||
|
|
||||||
# get '/:owner_name/:name.?:format?' do # v1
|
# get '/:owner_name/:name.?:format?' do # v1
|
||||||
# get '/repos/:owner_name/:name.?:format?' do # v2
|
# get '/repos/:owner_name/:name.?:format?' do # v2
|
||||||
# respond_with service(:repositories, :one, params).run(:raise => params[:format] != 'png')
|
# respond_with service(:repositories, :one, params)
|
||||||
# end
|
# end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -28,13 +28,11 @@ class Travis::Api::App
|
||||||
end
|
end
|
||||||
|
|
||||||
put '/:id?', scope: :private do
|
put '/:id?', scope: :private do
|
||||||
update(params[:user])
|
respond_with update(params[:user])
|
||||||
204
|
|
||||||
end
|
end
|
||||||
|
|
||||||
post '/sync', scope: :private do
|
post '/sync', scope: :private do
|
||||||
service(:users, :sync)
|
respond_with service(:users, :sync)
|
||||||
204
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,7 +8,7 @@ class Travis::Api::App
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/:id' do
|
get '/:id' do
|
||||||
respond_with one(params).run || not_found # TODO hrmmmmm
|
respond_with one(params)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,13 +7,8 @@ class Travis::Api::App
|
||||||
# course). These values will be encoded in JSON.
|
# course). These values will be encoded in JSON.
|
||||||
module RespondWith
|
module RespondWith
|
||||||
def respond_with(resource, options = {})
|
def respond_with(resource, options = {})
|
||||||
options[:format] ||= format_from_content_type || params[:format] || :json
|
options[:format] ||= format_from_content_type || params[:format] || 'json'
|
||||||
responders.each do |responder|
|
halt respond(resource, options).to_json
|
||||||
responder = responder.new(self, resource, options)
|
|
||||||
resource = responder.apply if responder.apply?
|
|
||||||
end
|
|
||||||
resource = resource.to_json unless resource.is_a?(String) # TODO when does this happen?
|
|
||||||
halt resource
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def body(value = nil, options = {}, &block)
|
def body(value = nil, options = {}, &block)
|
||||||
|
@ -23,8 +18,18 @@ class Travis::Api::App
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def responders
|
def respond(resource, options)
|
||||||
[Responders::Service, Responders::Json, Responders::Image, Responders::Xml]
|
responders(resource, options).each do |const|
|
||||||
|
responder = const.new(self, resource, options)
|
||||||
|
resource = responder.apply if responder.apply?
|
||||||
|
end
|
||||||
|
resource
|
||||||
|
end
|
||||||
|
|
||||||
|
def responders(resource, options)
|
||||||
|
[:Service, :Json, :Image, :Xml].map do |name|
|
||||||
|
Responders.const_get(name)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO is there no support for this kind of mime types?
|
# TODO is there no support for this kind of mime types?
|
||||||
|
|
|
@ -3,7 +3,7 @@ module Travis::Api::App::Helpers::Responders
|
||||||
attr_reader :endpoint, :resource, :options
|
attr_reader :endpoint, :resource, :options
|
||||||
|
|
||||||
def initialize(endpoint, resource, options = {})
|
def initialize(endpoint, resource, options = {})
|
||||||
@endpoint = endpoint
|
@endpoint = endpoint
|
||||||
@resource = resource
|
@resource = resource
|
||||||
@options = options
|
@options = options
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,16 +8,16 @@ module Travis::Api::App::Helpers::Responders
|
||||||
|
|
||||||
def apply
|
def apply
|
||||||
headers['Expires'] = Time.now.utc.httpdate
|
headers['Expires'] = Time.now.utc.httpdate
|
||||||
halt send_file(filename(resource), type: :png, disposition: :inline)
|
halt send_file(filename, type: :png, disposition: :inline)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def filename(resource)
|
def filename
|
||||||
"#{root}/public/images/result/#{result(resource)}.png"
|
"#{root}/public/images/result/#{result}.png"
|
||||||
end
|
end
|
||||||
|
|
||||||
def result(resource)
|
def result
|
||||||
NAMES[resource.try(:last_build_result_on, branch: params[:branch])]
|
NAMES[resource.try(:last_build_result_on, branch: params[:branch])]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -4,18 +4,19 @@ module Travis::Api::App::Helpers::Responders
|
||||||
DEFAULT_VERSION = 'v2'
|
DEFAULT_VERSION = 'v2'
|
||||||
|
|
||||||
def apply?
|
def apply?
|
||||||
!resource.is_a?(String) && options[:format] == 'json'
|
options[:format] == 'json' && !resource.is_a?(String)
|
||||||
end
|
end
|
||||||
|
|
||||||
def apply
|
def apply
|
||||||
resource = builder.new(self.resource, request.params).data if builder
|
halt result.to_json
|
||||||
resource ||= self.resource || {}
|
|
||||||
resource.merge!(flash: flash) unless flash.empty?
|
|
||||||
halt resource.to_json
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def result
|
||||||
|
builder ? builder.new(resource, request.params).data : resource
|
||||||
|
end
|
||||||
|
|
||||||
def builder
|
def builder
|
||||||
@builder ||= Travis::Api.builder(resource, { :version => version }.merge(options))
|
@builder ||= Travis::Api.builder(resource, { :version => version }.merge(options))
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,10 +6,27 @@ module Travis::Api::App::Helpers::Responders
|
||||||
|
|
||||||
def apply
|
def apply
|
||||||
# TODO add caching headers depending on the resource
|
# TODO add caching headers depending on the resource
|
||||||
result = resource.run || {}
|
data = result
|
||||||
flash.concat(resource.messages) if resource.respond_to?(:messages)
|
halt 404 if data.nil?
|
||||||
result
|
flash.concat(data.messages) if resource.respond_to?(:messages)
|
||||||
|
data
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
# Services potentially return all sorts of things
|
||||||
|
# If it's a string, true or false we'll wrap it into a hash.
|
||||||
|
# If it's an active record instance or scope we just pass it on
|
||||||
|
# so it can be processed by the json responder.
|
||||||
|
# If it's nil we also pass it but immediately yield not_found.
|
||||||
|
def result
|
||||||
|
case result = resource.run
|
||||||
|
when String, true, false
|
||||||
|
{ result: result }
|
||||||
|
else
|
||||||
|
result
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ describe 'Builds' do
|
||||||
|
|
||||||
it 'GET /builds?repository_id=1' do
|
it 'GET /builds?repository_id=1' do
|
||||||
response = get '/builds', { repository_id: repo.id }, headers
|
response = get '/builds', { repository_id: repo.id }, headers
|
||||||
response.should deliver_json_for(repo.builds.was_started.order('id DESC'), version: 'v1')
|
response.should deliver_json_for(repo.builds.order('id DESC'), version: 'v1')
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'GET /builds/1' do
|
it 'GET /builds/1' do
|
||||||
|
|
|
@ -10,7 +10,7 @@ describe 'Jobs' do
|
||||||
|
|
||||||
it '/jobs?queue=builds.common' do
|
it '/jobs?queue=builds.common' do
|
||||||
response = get '/jobs', { queue: 'builds.common' }, headers
|
response = get '/jobs', { queue: 'builds.common' }, headers
|
||||||
response.should deliver_json_for(Job.queued('builds.common'), versin: 'v1')
|
response.should deliver_json_for(Job.queued('builds.common'), version: 'v1')
|
||||||
end
|
end
|
||||||
|
|
||||||
it '/jobs/:job_id' do
|
it '/jobs/:job_id' do
|
||||||
|
|
|
@ -9,7 +9,7 @@ describe 'Builds' do
|
||||||
|
|
||||||
it 'GET /builds?repository_id=1' do
|
it 'GET /builds?repository_id=1' do
|
||||||
response = get '/builds', { repository_id: repo.id }, headers
|
response = get '/builds', { repository_id: repo.id }, headers
|
||||||
response.should deliver_json_for(repo.builds.was_started.order('id DESC'), version: 'v2')
|
response.should deliver_json_for(repo.builds.order('id DESC'), version: 'v2')
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'GET /builds/1' do
|
it 'GET /builds/1' do
|
||||||
|
|
Loading…
Reference in New Issue
Block a user