lib/keen/client.rb in keen-0.5.0 vs lib/keen/client.rb in keen-0.6.0

- old
+ new

@@ -1,14 +1,20 @@ require 'keen/http' require 'keen/version' +require 'keen/client/publishing_methods' +require 'keen/client/querying_methods' + require 'openssl' require 'multi_json' require 'base64' require 'uri' module Keen class Client + include Keen::Client::PublishingMethods + include Keen::Client::QueryingMethods + attr_accessor :project_id, :api_key CONFIG = { :api_host => "api.keen.io", :api_port => 443, @@ -28,16 +34,10 @@ { "Content-Type" => "application/json", "User-Agent" => user_agent } } } - def beacon_url(event_collection, properties) - json = MultiJson.encode(properties) - data = [json].pack("m0").tr("+/", "-_").gsub("\n", "") - "https://#{api_host}#{api_path(event_collection)}?data=#{data}" - end - def initialize(*args) options = args[0] unless options.is_a?(Hash) # deprecated, pass a hash of options instead options = { @@ -48,68 +48,10 @@ @project_id, @api_key = options.values_at( :project_id, :api_key) end - def publish(event_collection, properties) - check_configuration! - check_event_data!(event_collection, properties) - - begin - response = Keen::HTTP::Sync.new( - api_host, api_port, api_sync_http_options).post( - :path => api_path(event_collection), - :headers => api_headers_with_auth("sync"), - :body => MultiJson.encode(properties)) - rescue Exception => http_error - raise HttpError.new("Couldn't connect to Keen IO: #{http_error.message}", http_error) - end - process_response(response.code, response.body.chomp) - end - - def publish_async(event_collection, properties) - check_configuration! - check_event_data!(event_collection, properties) - - deferrable = EventMachine::DefaultDeferrable.new - - http_client = Keen::HTTP::Async.new(api_host, api_port, api_async_http_options) - http = http_client.post({ - :path => api_path(event_collection), - :headers => api_headers_with_auth("async"), - :body => MultiJson.encode(properties) - }) - - 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_collection, properties, options={}) - self.publish(event_collection, properties, options) - end - private def process_response(status_code, response_body) body = MultiJson.decode(response_body) case status_code.to_i @@ -124,24 +66,15 @@ else raise HttpError.new(body) end end - def api_path(event_collection) - "/#{api_version}/projects/#{project_id}/events/#{URI.escape(event_collection)}" + def ensure_project_id! + raise ConfigurationError, "Project ID must be set" unless self.project_id end - def api_headers_with_auth(sync_or_async) - api_headers(sync_or_async) - end - - def check_configuration! - raise ConfigurationError, "Project ID must be set" unless project_id - end - - def check_event_data!(event_collection, properties) - raise ArgumentError, "Event collection can not be nil" unless event_collection - raise ArgumentError, "Event properties can not be nil" unless properties + def ensure_api_key! + raise ConfigurationError, "API Key must be set for queries" unless self.api_key end def method_missing(_method, *args, &block) if config = CONFIG[_method.to_sym] if config.is_a?(Proc)