lib/statsmix.rb in statsmix-0.0.5 vs lib/statsmix.rb in statsmix-0.0.6

- old
+ new

@@ -1,30 +1,36 @@ require 'net/http' class StatsMix BASE_URI = 'http://www.statsmix.com/api/v2/' + # BASE_URI = 'http://localhost:3000/api/v2/' GEM_VERSION = File.exist?('../VERSION') ? File.read('../VERSION') : "" def initialize(api_key, format = 'xml') @api_key = api_key @format = format @user_agent = "StatsMix Ruby Gem " + GEM_VERSION end - # -- Stats -- + def connect(resource) + # Resources available: stats, metrics, TODO: profiles + @url = URI.parse(BASE_URI + resource) + @connection = Net::HTTP.new(@url.host, @url.port) + + end + # Stats + # List stats (index) # # Required: metric_id # Optional: limit def list_stats(metric_id, limit = nil) - url = URI.parse(BASE_URI + "stats") - http = Net::HTTP.new(url.host, url.port) - - request = Net::HTTP::Get.new(url.path + '.' + @format) + connect('stats') + request = Net::HTTP::Get.new(@url.path + '.' + @format) request["User-Agent"] = @user_agent form_hash = Hash.new form_hash["api_key"] = @api_key form_hash["metric_id"] = metric_id @@ -32,40 +38,36 @@ form_hash["limit"] = limit end request.set_form_data(form_hash) - response = http.request(request) - puts response.body, response.code, response.message + response = @connection.request(request) + return response.body end # Get stat # # Required: stat_id # Optional: none def get_stat(stat_id) - url = URI.parse(BASE_URI + "stats") - http = Net::HTTP.new(url.host, url.port) - - request = Net::HTTP::Get.new(url.path + '/' + stat_id.to_s + '.' + @format) + connect('stats') + request = Net::HTTP::Get.new(@url.path + '/' + stat_id.to_s + '.' + @format) request["User-Agent"] = @user_agent request.set_form_data({"api_key" => @api_key}) - response = http.request(request) - puts response.body, response.code, response.message + response = @connection.request(request) + return response.body end # Create stat # # Required: metric_id # Optional: value, generated_at, meta def create_stat(metric_id, value = 1, generated_at = Time.now, meta = nil) - url = URI.parse(BASE_URI + "stats") - http = Net::HTTP.new(url.host, url.port) - - request = Net::HTTP::Post.new(url.path + '.' + @format) + connect('stats') + request = Net::HTTP::Post.new(@url.path + '.' + @format) request["User-Agent"] = @user_agent form_hash = Hash.new form_hash["api_key"] = @api_key form_hash["value"] = value @@ -75,23 +77,21 @@ form_hash["meta"] = meta end request.set_form_data(form_hash) - response = http.request(request) - puts response.body, response.code, response.message + response = @connection.request(request) + return response.body end # Update stat # # Required: stat_id # Optional: value, generated_at, meta def update_stat(stat_id, value = nil, generated_at = nil, meta = nil) - url = URI.parse(BASE_URI + "stats") - http = Net::HTTP.new(url.host, url.port) - - request = Net::HTTP::Put.new(url.path + '/' + stat_id.to_s + '.' + @format) + connect('stats') + request = Net::HTTP::Put.new(@url.path + '/' + stat_id.to_s + '.' + @format) request["User-Agent"] = @user_agent form_hash = Hash.new form_hash["api_key"] = @api_key if value != nil @@ -104,42 +104,38 @@ form_hash["meta"] = meta end request.set_form_data(form_hash) - response = http.request(request) - puts response.body, response.code, response.message + response = @connection.request(request) + return response.body end # Delete stat # # Required: stat_id # Optional: none def delete_stat(stat_id) - url = URI.parse(BASE_URI + "stats") - http = Net::HTTP.new(url.host, url.port) - - request = Net::HTTP::Delete.new(url.path + '/' + stat_id.to_s + '.' + @format) + connect('stats') + request = Net::HTTP::Delete.new(@url.path + '/' + stat_id.to_s + '.' + @format) request["User-Agent"] = @user_agent request.set_form_data({"api_key" => @api_key}) - response = http.request(request) - puts response.body, response.code, response.message + response = @connection.request(request) + return response.body end - # -- Metrics -- + # Metrics # List metrics # # Required: none # Optional: profile_id, limit def list_metrics(profile_id = nil, limit = nil) - url = URI.parse(BASE_URI + "metrics") - http = Net::HTTP.new(url.host, url.port) - - request = Net::HTTP::Get.new(url.path + '.' + @format) + connect('metrics') + request = Net::HTTP::Get.new(@url.path + '.' + @format) request["User-Agent"] = @user_agent form_hash = Hash.new form_hash["api_key"] = @api_key if profile_id != nil @@ -149,40 +145,36 @@ form_hash["limit"] = limit end request.set_form_data(form_hash) - response = http.request(request) - puts response.body, response.code, response.message + response = @connection.request(request) + return response.body end # Get metric # # Required: metric_id # Optional: none def get_metric(metric_id) - url = URI.parse(BASE_URI + "metrics") - http = Net::HTTP.new(url.host, url.port) - - request = Net::HTTP::Get.new(url.path + '/' + metric_id.to_s + '.' + @format) + connect('metrics') + request = Net::HTTP::Get.new(@url.path + '/' + metric_id.to_s + '.' + @format) request["User-Agent"] = @user_agent request.set_form_data({"api_key" => @api_key}) - response = http.request(request) - puts response.body, response.code, response.message + response = @connection.request(request) + return response.body end # Create metric # # Required: name # Optional: profile_id, generated_at def create_metric(name, profile_id = nil) - url = URI.parse(BASE_URI + "metrics") - http = Net::HTTP.new(url.host, url.port) - - request = Net::HTTP::Post.new(url.path + '.' + @format) + connect('metrics') + request = Net::HTTP::Post.new(@url.path + '.' + @format) request["User-Agent"] = @user_agent form_hash = Hash.new form_hash["api_key"] = @api_key form_hash["name"] = name @@ -190,23 +182,21 @@ form_hash["profile_id"] = profile_id end request.set_form_data(form_hash) - response = http.request(request) - puts response.body, response.code, response.message + response = @connection.request(request) + return response.body end # Update metric # # Required: metric_id # Optional: name, sharing, include_in_email def update_metric(metric_id, name = nil, sharing = nil, include_in_email = nil) - url = URI.parse(BASE_URI + "metrics") - http = Net::HTTP.new(url.host, url.port) - - request = Net::HTTP::Put.new(url.path + '/' + metric_id.to_s + '.' + @format) + connect('metrics') + request = Net::HTTP::Put.new(@url.path + '/' + metric_id.to_s + '.' + @format) request["User-Agent"] = @user_agent form_hash = Hash.new form_hash["api_key"] = @api_key if name != nil @@ -219,26 +209,24 @@ form_hash["include_in_email"] = include_in_email end request.set_form_data(form_hash) - response = http.request(request) - puts response.body, response.code, response.message + response = @connection.request(request) + return response.body end # Delete metric # # Required: metric_id # Optional: none def delete_metric(metric_id) - url = URI.parse(BASE_URI + "metrics") - http = Net::HTTP.new(url.host, url.port) - - request = Net::HTTP::Delete.new(url.path + '/' + metric_id.to_s + '.' + @format) + connect('metrics') + request = Net::HTTP::Delete.new(@url.path + '/' + metric_id.to_s + '.' + @format) request["User-Agent"] = @user_agent request.set_form_data({"api_key" => @api_key}) - response = http.request(request) - puts response.body, response.code, response.message + response = @connection.request(request) + return response.body end end \ No newline at end of file