Module: SauceWhisk::RestRequestBuilder

Included in:
Accounts, Assets, Jobs, Sauce, Tunnels
Defined in:
lib/sauce_whisk/rest_request_builder.rb

Instance Method Summary collapse

Instance Method Details

#auth_detailsObject



71
72
73
# File 'lib/sauce_whisk/rest_request_builder.rb', line 71

def auth_details
  {:user => SauceWhisk.username, :password => SauceWhisk.password}
end

#delete(resource_id) ⇒ Object



29
30
31
32
# File 'lib/sauce_whisk/rest_request_builder.rb', line 29

def delete(resource_id)
  resource_to_delete = fully_qualified_resource << "/#{resource_id}"
  make_request({:method => :delete, :url => resource_to_delete}.merge auth_details)
end

#fully_qualified_resourceObject



75
76
77
# File 'lib/sauce_whisk/rest_request_builder.rb', line 75

def fully_qualified_resource
  return (respond_to? :resource) ? "#{SauceWhisk.base_url}/#{resource}" : SauceWhisk.base_url
end

#get(resource_to_fetch = nil) ⇒ Object



9
10
11
12
13
# File 'lib/sauce_whisk/rest_request_builder.rb', line 9

def get(resource_to_fetch=nil)
  resource_url = fully_qualified_resource
  resource_url << "/#{resource_to_fetch}" if resource_to_fetch
  make_request({:method => :get, :url => resource_url}.merge auth_details)
end

#make_request(req_params) ⇒ Object



55
56
57
58
# File 'lib/sauce_whisk/rest_request_builder.rb', line 55

def make_request(req_params)
  SauceWhisk.logger.debug "Performing Request: \n#{req_params}"
  request_from_rest_client req_params
end

#post(opts) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sauce_whisk/rest_request_builder.rb', line 34

def post(opts)
  payload = (opts[:payload].to_json)
  resource_id = opts[:resource] || nil

  url = fully_qualified_resource
  url << "/#{resource_id}" if resource_id

  length = payload.length
  headers = {"Content-Length" => length}
  req_params = {
      :method => :post,
      :url => url,
      :content_type => "application/json",
      :headers => headers
  }

  req_params.merge!({:payload => payload}) unless payload.nil?

  make_request(req_params.merge auth_details)
end

#put(resource_id, body = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sauce_whisk/rest_request_builder.rb', line 15

def put(resource_id, body={})
  url = "#{fully_qualified_resource}/#{resource_id}"
  length = body.length
  headers = {"Content-Length" => length}
  req_params = {
      :method => :put,
      :url => url,
      :payload => body,
      :content_type => :json,
      :headers => headers
  }
  make_request(req_params.merge auth_details)
end

#request_from_rest_client(req_params) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/sauce_whisk/rest_request_builder.rb', line 60

def request_from_rest_client(req_params)
  tries ||= SauceWhisk.rest_retries
  RestClient::Request.execute(req_params)
rescue RestClient::ResourceNotFound => e
  if (tries -= 1) > 0
    retry
  else
    raise e
  end    
end