lib/buildbox/api.rb in buildbox-0.0.2 vs lib/buildbox/api.rb in buildbox-0.0.3
- old
+ new
@@ -4,10 +4,11 @@
require 'openssl'
require 'json'
def initialize(options)
@options = options
+ @queue = ::Queue.new
end
def crash(exception, information = {})
payload = {
:exception => exception.class.name,
@@ -18,31 +19,60 @@
payload[:meta][:worker_uuid] = worker_uuid if worker_uuid
payload[:meta][:build_uuid] = information[:build] if information[:build]
payload[:meta][:client_version] = Buildbox::VERSION
- request(:post, "crashes", :crash => payload)
+ request(:post, "crashes", payload)
end
def register(payload)
- request(:post, "workers", :worker => payload)
+ request(:post, "workers", payload)
end
def login
request(:get, "user")
end
- def update(build, payload)
- request(:put, "workers/#{worker_uuid}/builds/#{build.uuid}", :build => payload)
- end
-
def builds(options = {})
request(:get, "workers/#{worker_uuid}/builds")
end
+ def update_build_state_async(*args)
+ async :update_build_state, args
+ end
+
+ def update_build_state(build_uuid, state)
+ request(:put, "workers/#{worker_uuid}/builds/#{build_uuid}", :state => state)
+ end
+
+ def update_build_result_async(*args)
+ async :update_build_result, args
+ end
+
+ def update_build_result(build_uuid, result_uuid, attributes)
+ request(:put, "workers/#{worker_uuid}/builds/#{build_uuid}/results/#{result_uuid}", attributes)
+ end
+
private
+ def async(method, args)
+ @queue << [ method, args ]
+
+ if !@thread || !@thread.alive?
+ @thread = Thread.new do
+ loop do
+ async_call = @queue.pop
+ Thread.exit if async_call.nil?
+
+ self.send(async_call[0], *async_call[1])
+ end
+ end
+
+ @thread.abort_on_exception = true
+ end
+ end
+
def http(uri)
Net::HTTP.new(uri.host, uri.port).tap do |http|
http.use_ssl = uri.scheme == "https"
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
@@ -55,19 +85,18 @@
when :post then Net::HTTP::Post
else raise "No request class defined for `#{method}`"
end
uri = URI.parse(endpoint(path))
- request = klass.new(uri.request_uri)
+ request = klass.new(uri.request_uri, 'Content-Type' => 'application/json',
+ 'Accept' => 'application/json')
if payload.nil?
Buildbox.logger.debug "#{method.to_s.upcase} #{uri}"
else
- normalized_payload = normalize_payload(payload)
- request.set_form_data normalized_payload
-
- Buildbox.logger.debug "#{method.to_s.upcase} #{uri} #{normalized_payload.inspect}"
+ request.body = JSON.dump(payload)
+ Buildbox.logger.debug "#{method.to_s.upcase} #{uri} #{payload.inspect}"
end
Response.new http(uri).request(request)
end
@@ -76,40 +105,8 @@
end
def endpoint(path)
(Buildbox.configuration.use_ssl ? "https://" : "http://") +
"#{Buildbox.configuration.endpoint}/v#{Buildbox.configuration.api_version}/#{path}"
- end
-
- # { :foo => { :bar => { :bang => "yolo" } } } => { "foo[bar][bang]" => "yolo" }
- def normalize_payload(params, key=nil)
- params = flatten_keys(params) if params.is_a?(Hash)
- result = {}
- params.each do |k,v|
- case v
- when Hash
- result[k.to_s] = normalize_params(v)
- when Array
- v.each_with_index do |val,i|
- result["#{k.to_s}[#{i}]"] = val.to_s
- end
- else
- result[k.to_s] = v.to_s
- end
- end
- result
- end
-
- def flatten_keys(hash, newhash={}, keys=nil)
- hash.each do |k, v|
- k = k.to_s
- keys2 = keys ? keys+"[#{k}]" : k
- if v.is_a?(Hash)
- flatten_keys(v, newhash, keys2)
- else
- newhash[keys2] = v
- end
- end
- newhash
end
end
end