travis-api/lib/travis/api/v3.rb
2015-01-21 16:38:25 +01:00

28 lines
1.0 KiB
Ruby

module Travis
module API
module V3
V3 = self
def load_dir(dir, recursive: true)
Dir.glob("#{dir}/*.rb").each { |f| require f[%r[(?<=lib/).+(?=\.rb$)]] }
Dir.glob("#{dir}/*").each { |dir| load_dir(dir) } if recursive
end
def response(payload, headers = {}, content_type: 'application/json'.freeze, status: 200)
payload = JSON.pretty_generate(payload) unless payload.is_a? String
headers = { 'Content-Type'.freeze => content_type, 'Content-Length'.freeze => payload.bytesize.to_s }.merge!(headers)
[status, headers, [payload] ]
end
extend self
load_dir("#{__dir__}/v3")
ClientError = Error .create(status: 400)
NotFound = ClientError .create(:resource, status: 404, template: '%s not found (or insufficient access)')
EnitityMissing = NotFound .create(type: 'not_found')
WrongCredentials = ClientError .create('access denied', status: 403)
WrongParams = ClientError .create('wrong parameters')
end
end
end