lib/keen/client.rb in keen-0.0.2 vs lib/keen/client.rb in keen-0.0.4
- old
+ new
@@ -1,123 +1,79 @@
-require 'keen/storage/redis_handler'
-require 'json'
-require "net/http"
+require "keen/async/storage/redis_handler"
+require "keen/async/job"
+require "json"
require "uri"
+require "time"
module Keen
+
class Client
- def self.batch_url(project_id)
- "http://api.keen.io/1.0/projects/#{project_id}/_events"
- end
-
- def initialize(project_id, auth_token)
- end
+ attr_accessor :storage_handler, :project_id, :auth_token
- def add_event(collection_name, event_body)
- validate_collection_name(collection_name)
+ def initialize(project_id, auth_token, options = {})
- end
+ raise "project_id must be string" unless project_id.kind_of? String
+ raise "auth_token must be string" unless auth_token.kind_of? String
- def self.process_queue(options)
- mode = options[:storage_mode].to_sym
- case mode
- when :flat_file
- self.process_queue_from_flat_file(options)
- when :redis
- self.process_queue_from_redis(options)
- else
- raise "Unknown storage_mode sent: `#{mode}`"
- end
+ default_options = {
+ :storage_mode => :redis,
+ }
+
+ options = default_options.update(options)
+
+ @project_id = project_id
+ @auth_token = auth_token
+ @storage_mode = options[:storage_mode]
end
- def self.process_queue_from_redis(options)
- require 'keen/storage/redis_handler'
- handler = Keen::Storage::RedisHandler.new
- collated = handler.process_queue
+ def handler
- # TODO: remove this mock:
- collated = {
- "4f5775ad163d666a6100000e" => {
- "clicks" => [
- Keen::Storage::Item.new({
- :project_id => "4f5775ad163d666a6100000e",
- :auth_token => "a5d4eaf432914823a94ecd7e0cb547b9",
- :collection_name => "clicks",
- :event_body => {:user_id => "12345"},
- }),
- Keen::Storage::Item.new({
- :project_id => "4f5775ad163d666a6100000e",
- :auth_token => "a5d4eaf432914823a94ecd7e0cb547b9",
- :collection_name => "clicks",
- :event_body => {:user_id => "12345"},
- }),
- Keen::Storage::Item.new({
- :project_id => "4f5775ad163d666a6100000e",
- :auth_token => "a5d4eaf432914823a94ecd7e0cb547b9",
- :collection_name => "clicks",
- :event_body => {:user_id => "12345"},
- }),
- ],
- "purchases" => [
- Keen::Storage::Item.new({
- :project_id => "4f5775ad163d666a6100000e",
- :auth_token => "a5d4eaf432914823a94ecd7e0cb547b9",
- :collection_name => "purchases",
- :event_body => {:user_id => "12345"},
- }),
- Keen::Storage::Item.new({
- :project_id => "4f5775ad163d666a6100000e",
- :auth_token => "a5d4eaf432914823a94ecd7e0cb547b9",
- :collection_name => "purchases",
- :event_body => {:user_id => "12345"},
- }),
- Keen::Storage::Item.new({
- :project_id => "4f5775ad163d666a6100000e",
- :auth_token => "a5d4eaf432914823a94ecd7e0cb547b9",
- :collection_name => "purchases",
- :event_body => {:user_id => "12345"},
- }),
- ],
- }
- }
+ unless @storage_handler
+ mode = @storage_mode
- collated.each do |project_id, batch|
- self.send_batch(project_id, batch)
+ case mode
+ when :redis
+ @storage_handler = Keen::Async::Storage::RedisHandler.new
+ else
+ raise "Unknown storage_mode sent to client: `#{mode}`"
+ end
+
end
+
+ @storage_handler
end
-
- def self.send_batch(project_id, batch)
- if not batch
- return
- end
- first_key = batch.keys[0]
- item_list = batch[first_key]
- puts item_list[0].class
- auth_token = item_list[0].auth_token
-
- uri = URI.parse(self.batch_url(project_id))
+ def add_event(collection_name, event_body, timestamp=nil)
+ #
+ # `collection_name` should be a string
+ #
+ # `event_body` should be a JSON-serializable hash
+ #
+ # `timestamp` is optional. If sent, it should be a Time instance.
+ # If it's not sent, we'll use the current time.
- request = Net::HTTP::Post.new(uri.path)
- request.body = batch.to_json
- request["Content-Type"] = "application/json"
- request["Authorization"] = auth_token
+ validate_collection_name(collection_name)
- response = Net::HTTP.start(uri.host, uri.port) {|http|
- http.request(request)
- }
+ unless timestamp
+ timestamp = Time.now
+ end
- puts response
+ event_body[:_timestamp] = timestamp.utc.iso8601
- # TODO DK: need to send this batch of Keen::Storage::Item instances
- # to API! we can just use the first auth_token we find on an Item.
- # If something fails, stick the item into the prior_failures queue
- # using push
+ job = Keen::Async::Job.new(handler, {
+ :project_id => @project_id,
+ :auth_token => @auth_token,
+ :collection_name => collection_name,
+ :event_body => event_body,
+ })
+
+ job.save
end
- def self.process_queue_from_flat_file(options)
- raise "this feature isn't supported yet!!!"
+ def validate_collection_name(collection_name)
+ # TODO
end
+
end
end