lib/keen/client.rb in keen-0.4.2 vs lib/keen/client.rb in keen-0.4.3

- old
+ new

@@ -1,15 +1,19 @@ -require 'keen/version' require 'keen/http' +require 'keen/version' +require 'openssl' +require 'multi_json' +require 'base64' module Keen class Client attr_accessor :project_id, :api_key CONFIG = { :api_host => "api.keen.io", :api_port => 443, + :api_version => "3.0", :api_sync_http_options => { :use_ssl => true, :verify_mode => OpenSSL::SSL::VERIFY_PEER, :verify_depth => 5, :ca_file => File.expand_path("../../../config/cacert.pem", __FILE__) }, @@ -18,10 +22,16 @@ "Content-Type" => "application/json", "User-Agent" => "keen-gem v#{Keen::VERSION}" } } + def beacon_url(event_name, properties) + json = MultiJson.encode(properties) + data = [json].pack("m0").tr("+/", "-_").gsub("\n", "") + "https://#{api_host}/#{api_version}/projects/#{@project_id}/events/#{event_name}?api_key=#{@api_key}&data=#{data}" + end + def initialize(*args) options = args[0] unless options.is_a?(Hash) # deprecated, pass a hash of options instead options = { @@ -57,24 +67,33 @@ http = http_client.post({ :path => api_path(event_name), :headers => api_headers_with_auth, :body => MultiJson.encode(properties) }) - http.callback { - begin - response = process_response(http.response_header.status, http.response.chomp) - deferrable.succeed(response) - rescue Exception => e - deferrable.fail(e) - end - } - http.errback { - Keen.logger.warn("Couldn't connect to Keen IO: #{http.error}") - deferrable.fail(Error.new("Couldn't connect to Keen IO: #{http.error}")) - } - deferrable + if defined?(EM::Synchrony) + if http.error + Keen.logger.warn("Couldn't connect to Keen IO: #{http.error}") + raise HttpError.new("Couldn't connect to Keen IO: #{http.error}") + else + process_response(http.response_header.status, http.response.chomp) + end + else + http.callback { + begin + response = process_response(http.response_header.status, http.response.chomp) + deferrable.succeed(response) + rescue Exception => e + deferrable.fail(e) + end + } + http.errback { + Keen.logger.warn("Couldn't connect to Keen IO: #{http.error}") + deferrable.fail(Error.new("Couldn't connect to Keen IO: #{http.error}")) + } + deferrable + end end # deprecated def add_event(event_name, properties, options={}) self.publish(event_name, properties, options) @@ -97,10 +116,10 @@ raise HttpError.new(body) end end def api_path(collection) - "/3.0/projects/#{project_id}/events/#{collection}" + "/#{api_version}/projects/#{project_id}/events/#{collection}" end def api_headers_with_auth api_headers.merge("Authorization" => api_key) end