class STDDAPI
Public Class Methods
new(stdd_url,http_proxy)
click to toggle source
# File lib/stdd_api.rb, line 7 def initialize(stdd_url,http_proxy) puts "hej" @url = stdd_url ? stdd_url : ['http://www.stddtool.se'] @proxy = http_proxy ? URI.parse('http://'+http_proxy) : OpenStruct.new @connection_error = nil end
Public Instance Methods
add_params_to_path(path, params)
click to toggle source
# File lib/stdd_api.rb, line 81 def add_params_to_path (path, params) if(params) path = "#{path}?".concat(params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&')) end return path end
create_customer(customer_name)
click to toggle source
# File lib/stdd_api.rb, line 14 def create_customer customer_name return if @connection_error p "Creating customer.." uri = URI.parse(@url) begin http = Net::HTTP::Proxy(@proxy.host, @proxy.port).new(uri.host, uri.port) request = Net::HTTP::Post.new("/api/create_customer",initheader = { 'Content-Type' => 'application/json'}) request.body = {"name" => customer_name}.to_json response = http.request(request) case response.code when /20\d/ #success else @connection_error = response.body end parsed = JSON.parse(response.body) if parsed["error"] @connection_error = parsed["error"] else customer_id = parsed["_id"] end rescue @connection_error = "COULD NOT CONNECT TO HOST AT: #{@url}" end if(@connection_error) @io.puts @connection_error else return customer_id end end
get_customer(customer_name)
click to toggle source
# File lib/stdd_api.rb, line 50 def get_customer(customer_name) customer = Customer.new(customer_name) # get existing customer if @connection_error @io.puts "#{@connection_error}" return end uri = URI.parse(@url) path = "/api/get_customer" path = add_params_to_path(path,{:name => customer.name}) req = Net::HTTP::Get.new(path,) response = Net::HTTP::Proxy(@proxy.host, @proxy.port).new(uri.host, uri.port).start {|http| http.request(req) } parsed = JSON.parse(response.body) if(parsed["_id"]) customer.id = parsed["_id"] else customer = create_customer(customer.name) end return customer end