lib/statsmix.rb in statsmix-0.3.0 vs lib/statsmix.rb in statsmix-1.0.0

- old
+ new

@@ -1,27 +1,31 @@ require 'net/http' require 'net/https' +require 'socket' require 'rubygems' require 'json' class StatsMix - BASE_URI = 'https://api.statsmix.com/api/v2/' - GEM_VERSION = File.exist?('../VERSION') ? File.read('../VERSION') : "" + HOST = 'api.statsmix.com' + BASE_URI = "http://#{HOST}/api/v2/" + UDP_PORT = 8125 + GEM_VERSION = File.exist?('VERSION') ? File.read('VERSION') : "" # Track an event # # Required: name of metric # Optional: value, options {:generated_at} # Returns: Net::HTTP object def self.track(name, value = nil, options = {}) if options.respond_to?('with_indifferent_access') options = options.with_indifferent_access end + self.connect('track') @request_uri = @url.path + '.' + @format - @request = Net::HTTP::Get.new(@request_uri) + @request = Net::HTTP::Get.new(@request_uri, {"User-Agent" => @user_agent}) @params[:name] = name if @test_metric_name @params[:name] = @test_metric_name end @params[:value] = Float(value) unless value.nil? @@ -41,11 +45,11 @@ if options.respond_to?('with_indifferent_access') options = options.with_indifferent_access end self.connect('stats') @request_uri = @url.path + '.' + @format - @request = Net::HTTP::Get.new(@request_uri) + @request = Net::HTTP::Get.new(@request_uri, {"User-Agent" => @user_agent}) @params[:metric_id] = metric_id @params.merge!(options) if @params[:start_date] && @params[:start_date].is_a?(ActiveSupport::TimeWithZone) @params[:start_date] = @params[:start_date].utc.strftime('%Y-%m-%d %H:%M:%S') @@ -62,11 +66,11 @@ # Optional: none # Returns: Net::HTTP object def self.get_stat(stat_id) connect('stats') @request_uri = @url.path + '/' + stat_id.to_s + '.' + @format - @request = Net::HTTP::Get.new(@request_uri) + @request = Net::HTTP::Get.new(@request_uri, {"User-Agent" => @user_agent}) return do_request end # Create stat # @@ -77,11 +81,11 @@ if params.respond_to?('with_indifferent_access') params = params.with_indifferent_access end connect('stats') @request_uri = @url.path + '.' + @format - @request = Net::HTTP::Post.new(@request_uri) + @request = Net::HTTP::Post.new(@request_uri, {"User-Agent" => @user_agent}) @params[:metric_id] = metric_id @params[:value] = value if value @params.merge!(params) self.check_meta return do_request @@ -96,11 +100,11 @@ if params.respond_to?('with_indifferent_access') params = params.with_indifferent_access end connect('stats') @request_uri = @url.path + '/' + stat_id.to_s + '.' + @format - @request = Net::HTTP::Put.new(@request_uri) + @request = Net::HTTP::Put.new(@request_uri, {"User-Agent" => @user_agent}) @params[:value] = value if value @params.merge!(params) self.check_meta return do_request end @@ -111,11 +115,11 @@ # Optional: none # Returns: Net::HTTP object def self.delete_stat(stat_id) connect('stats') @request_uri = @url.path + '/' + stat_id.to_s + '.' + @format - @request = Net::HTTP::Delete.new(@request_uri) + @request = Net::HTTP::Delete.new(@request_uri, {"User-Agent" => @user_agent}) return do_request end # Metrics @@ -125,11 +129,11 @@ # Optional: profile_id, limit # Returns: Net::HTTP object def self.list_metrics(profile_id = nil, limit = nil) connect('metrics') @request_uri = @url.path + '.' + @format - @request = Net::HTTP::Get.new(@request_uri) + @request = Net::HTTP::Get.new(@request_uri, {"User-Agent" => @user_agent}) @params[:profile_id] = profile_id if profile_id != nil @params[:limit] = limit if limit != nil return do_request @@ -141,11 +145,11 @@ # Optional: none # Returns: Net::HTTP object def self.get_metric(metric_id) connect('metrics') @request_uri = @url.path + '/' + metric_id.to_s + '.' + @format - @request = Net::HTTP::Get.new(@request_uri) + @request = Net::HTTP::Get.new(@request_uri, {"User-Agent" => @user_agent}) return do_request end # Create metric # @@ -158,11 +162,11 @@ end connect('metrics') @params.merge!(params) @params[:name] = name @request_uri = @url.path + '.' + @format - @request = Net::HTTP::Post.new(@request_uri) + @request = Net::HTTP::Post.new(@request_uri, {"User-Agent" => @user_agent}) return do_request end # Update metric # @@ -175,11 +179,11 @@ end connect('metrics') @params = [] if @params.nil? @params.merge!(params) @request_uri = @url.path + '/' + metric_id.to_s + '.' + @format - @request = Net::HTTP::Put.new(@request_uri) + @request = Net::HTTP::Put.new(@request_uri, {"User-Agent" => @user_agent}) return do_request end # Delete metric @@ -188,11 +192,11 @@ # Optional: none # Returns: Net::HTTP object def self.delete_metric(metric_id) connect('metrics') @request_uri = @url.path + '/' + metric_id.to_s + '.' + @format - @request = Net::HTTP::Delete.new(@request_uri) + @request = Net::HTTP::Delete.new(@request_uri, {"User-Agent" => @user_agent}) return do_request end # partner api methods # see http://www.statsmix.com/developers/partner_api @@ -202,11 +206,11 @@ # Optional: none # Returns: Net::HTTP object def self.list_users connect('partners/users') @request_uri = @url.path - @request = Net::HTTP::Get.new(@request_uri) + @request = Net::HTTP::Get.new(@request_uri, {"User-Agent" => @user_agent}) return do_request end # Get user # @@ -214,11 +218,11 @@ # Optional: none # Returns: Net::HTTP object def self.get_user(id_or_api_key) connect('partners/users') @request_uri = @url.path + '/' + id_or_api_key.to_s + '.' + @format - @request = Net::HTTP::Get.new(@request_uri) + @request = Net::HTTP::Get.new(@request_uri, {"User-Agent" => @user_agent}) return do_request end # Create user # @@ -226,11 +230,11 @@ # Optional: :email,:name,:plan,:company,:url, :metrics[] # Returns: Net::HTTP object def self.create_user(params = {}) connect('partners/users') @request_uri = @url.path + '.' + @format - @request = Net::HTTP::Post.new(@request_uri) + @request = Net::HTTP::Post.new(@request_uri, {"User-Agent" => @user_agent}) @params.merge!(params) result = do_request unless self.error @user_api_key = result.scan(/<api_key>[0-9a-zA-Z]*<\/api_key>/)[0].gsub(/<\/?api_key>/,'') end @@ -243,11 +247,11 @@ # Optional: params with keys :email,:name,:plan,:company,:url # Returns: Net::HTTP object def self.update_user(id_or_api_key, params = {}) connect('partners/users') @request_uri = @url.path + '/' + id_or_api_key.to_s + '.' + @format - @request = Net::HTTP::Put.new(@request_uri) + @request = Net::HTTP::Put.new(@request_uri, {"User-Agent" => @user_agent}) @params.merge!(params) result = do_request unless self.error @user_api_key = result.scan(/<api_key>[0-9a-zA-Z]*<\/api_key>/)[0].gsub(/<\/?api_key>/,'') end @@ -260,11 +264,11 @@ # Optional: none # Returns: Net::HTTP object def self.delete_user(id_or_api_key) connect('partners/users') @request_uri = @url.path + '/' + id_or_api_key.to_s + '.' + @format - @request = Net::HTTP::Delete.new(@request_uri) + @request = Net::HTTP::Delete.new(@request_uri, {"User-Agent" => @user_agent}) result = do_request unless self.error @user_api_key = result.scan(/<api_key>[0-9a-zA-Z]*<\/api_key>/)[0].gsub(/<\/?api_key>/,'') end return result @@ -319,11 +323,32 @@ # Returns: string def self.api_key @api_key end + + #Expects: boolean + def self.debugging=(value) + @debugging = value + end + + # Returns: boolean + def self.debugging + @debugging + end + + #Expects: boolean + def self.use_udp=(value) + @use_udp = value + end + + # Returns: boolean + def self.use_udp + @use_udp + end + def self.api_from_env return nil if ENV['STATSMIX_URL'].nil? url = ENV['STATSMIX_URL'] pieces = url.gsub('http://','').gsub('https://','').split('/') @api_key = pieces[2] @@ -360,35 +385,43 @@ @error = false end def self.connect(resource) self.setup - if @api_key.nil? raise "API key not set. You must set it first with StatsMix.api_key = [your api key]" end # Resources available: stats, metrics, TODO: profiles @url = URI.parse(BASE_URI + resource) @connection = Net::HTTP.new(@url.host, @url.port) @connection.use_ssl = (@url.scheme == 'https') - + @connection.verify_mode = OpenSSL::SSL::VERIFY_NONE - + + @connection.use_ssl = (@url.scheme == 'https') + @connection.verify_mode = OpenSSL::SSL::VERIFY_NONE + @request = Hash.new @request["User-Agent"] = @user_agent @params = Hash.new if @params.respond_to?('with_indifferent_access') @params = @params.with_indifferent_access end @params[:api_key] = @api_key end + def self.user_agent + return @user_agent + end + def self.do_request @error = false return if @ignore + return self.do_udp_request if @use_udp #had to add code to support properly encoding array values. See http://blog.assimov.net/post/653645115/post-put-arrays-with-ruby-net-http-set-form-data + p @params self.set_form_data(@params) @response = @connection.request(@request) if @response.is_a?(Net::HTTPClientError) if 'xml' == @format begin @@ -400,10 +433,21 @@ @error = JSON.parse(@response.body)['errors']['error'] end end @response.body end + + def self.do_udp_request + data = @params.to_json.inspect + @socket ||= UDPSocket.new + @socket.send(data, 0, HOST, UDP_PORT) + if @debugging + text, sender = @socket.recvfrom(2048) + p "SENT #{data}" + p text + end + end #based on http://blog.assimov.net/post/653645115/post-put-arrays-with-ruby-net-http-set-form-data def self.set_form_data(params, sep = '&') @request.body = params.map {|k,v| if v.instance_of?(Array) v.map {|e| "#{self.urlencode(k.to_s)}[]=#{urlencode(e.to_s)}"}.join(sep) @@ -424,14 +468,16 @@ end if @params[:meta].nil? && @params['meta'] @params[:meta] = @params['meta'] @params.delete('meta') end + return if @use_udp if @params[:meta] && !@params[:meta].is_a?(String) if @params[:meta].respond_to?('to_json') @params[:meta] = @params[:meta].to_json end end end + end StatsMix.api_from_env \ No newline at end of file