Class: STDDAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/stdd_api.rb

Defined Under Namespace

Classes: Customer, Project, Run

Instance Method Summary collapse

Constructor Details

#initialize(stdd_url, http_proxy) ⇒ STDDAPI

Returns a new instance of STDDAPI.



7
8
9
10
11
12
# 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

Instance Method Details

#add_params_to_path(path, params) ⇒ Object



81
82
83
84
85
86
# 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) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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