lib/statsmix.rb in statsmix-0.1.10 vs lib/statsmix.rb in statsmix-0.2.1

- old
+ new

@@ -12,25 +12,23 @@ # # 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) @params[:name] = name if @test_metric_name @params[:name] = @test_metric_name end @params[:value] = value if value != nil @params.merge!(options) - @params[:meta] = - if @params[:meta] && !@params[:meta].is_a?(String) - if @params[:meta].respond_to?('to_json') - @params[:meta] = @params[:meta].to_json - end - end + self.check_meta return do_request end # Stats # List stats (index) @@ -38,10 +36,13 @@ # Required: metric_id # Optional: limit, {:startdate => nil, :end_date => nil} # Use start_date and end_date as has keys in the third param to scope the date range of stats based on the generated_at timestamp of a stat # Returns: Net::HTTP object def self.list_stats(metric_id, limit = nil, options = {}) + 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) @params[:metric_id] = metric_id @@ -71,30 +72,38 @@ # # Required: metric_id # Optional: value, params[:generated_at, :meta] # Returns: Net::HTTP object def self.create_stat(metric_id, value = nil, params = {}) + 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) @params[:metric_id] = metric_id - @params.merge!(params) @params[:value] = value if value + @params.merge!(params) + self.check_meta return do_request end # Update stat # # Required: stat_id - # Optional: value, generated_at, meta + # Optional: value, params[:generated_at, :meta] # Returns: Net::HTTP object def self.update_stat(stat_id, value = nil, params = {}) + 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) + @params[:value] = value if value @params.merge!(params) - @params[:value] = value if value != nil + self.check_meta return do_request end # Delete stat # @@ -142,10 +151,13 @@ # # Required: name # Optional: params[:profile_id, :sharing, :include_in_email] # Returns: Net::HTTP object def self.create_metric(name, params = {}) + if params.respond_to?('with_indifferent_access') + params = params.with_indifferent_access + end connect('metrics') @params.merge!(params) @params[:name] = name @request_uri = @url.path + '.' + @format @request = Net::HTTP::Post.new(@request_uri) @@ -155,11 +167,14 @@ # Update metric # # Required: metric_id # Optional: params[:profile_id, :sharing, :include_in_email] # Returns: Net::HTTP object - def self.update_metric(metric_id, params = {}) + def self.update_metric(metric_id, params = {}) + if params.respond_to?('with_indifferent_access') + params = params.with_indifferent_access + 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) @@ -177,11 +192,87 @@ @request_uri = @url.path + '/' + metric_id.to_s + '.' + @format @request = Net::HTTP::Delete.new(@request_uri) return do_request end + # partner api methods + # see http://www.statsmix.com/developers/partner_api + + # list users + # + # Optional: none # Returns: Net::HTTP object + def self.list_users + connect('partners/users') + @request_uri = @url.path + @request = Net::HTTP::Get.new(@request_uri) + return do_request + end + + # Get user + # + # Required: id_or_api_key - id or api key of the user to get + # 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) + return do_request + end + + # Create user + # + # Required: {:email => 'user@example.com'} - if the email already exists in our system, we will return the existing user + # 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) + @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 + return result + end + + # Update user + # + # Required: id_or_api_key - id or api key of the user to get + # 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) + @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 + return result + end + + # Delete user + # + # Required: id_or_api_key - id or api key of the user to get + # 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) + 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 + end + + # Returns: Net::HTTP object def self.response @response end # Returns: string or boolean false @@ -210,10 +301,15 @@ #Returns: boolean def self.ignore @ignore end + #Return: string + def self.user_api_key + @user_api_key + end + def self.test_metric_name=(name) @test_metric_name = name end #Returns: string or nil @@ -279,17 +375,21 @@ @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.do_request @error = false return if @ignore - @request.set_form_data(@params) + #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 + self.set_form_data(@params) @response = @connection.request(@request) if @response.is_a?(Net::HTTPClientError) if 'xml' == @format begin @error = @response.body.match('<error>(.)+</error>')[0].gsub('<error>','').gsub('</error>','') @@ -299,9 +399,35 @@ else @error = JSON.parse(@response.body)['errors']['error'] end end @response.body + 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) + else + "#{self.urlencode(k.to_s)}=#{self.urlencode(v.to_s)}" + end + }.join(sep) + @request.content_type = 'application/x-www-form-urlencoded' + end + + def self.urlencode(str) + str.gsub(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0]) } + end + + def self.check_meta + if @params[:meta] && !@params[:meta].is_a?(String) && !@params[:meta].is_a?(Hash) + raise "Invalid data . :meta should be a hash or a json-encoded string. You passed an object of type: #{@params[:meta].type}" + end + 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