Class: Deploy::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, method = :get) ⇒ Request

Returns a new instance of Request.



7
8
9
10
# File 'lib/deploy/request.rb', line 7

def initialize(path, method = :get)
  @path = path
  @method = method
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/deploy/request.rb', line 5

def data
  @data
end

#methodObject (readonly)

Returns the value of attribute method.



4
5
6
# File 'lib/deploy/request.rb', line 4

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/deploy/request.rb', line 4

def path
  @path
end

Instance Method Details

#makeObject

Make a request to the Deploy API using net/http. Data passed can be a hash or a string Hashes will be converted to JSON before being sent to the remote service.



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
49
50
51
52
# File 'lib/deploy/request.rb', line 22

def make
  uri = URI.parse([Deploy.configuration., @path].join('/'))
  http_request = http_class.new(uri.request_uri)
  http_request.basic_auth(Deploy.configuration.username, Deploy.configuration.api_key)
  http_request["Accept"] = "application/json"
  http_request["Content-type"] = "application/json"

  http = Net::HTTP.new(uri.host, uri.port)
  if uri.scheme == 'https'
    http.use_ssl = true
  end

  data = self.data.to_json if self.data.is_a?(Hash) && self.data.respond_to?(:to_json)
  http_result = http.request(http_request, data)
  @output = http_result.body
  @success = case http_result
  when Net::HTTPSuccess
    true
  when Net::HTTPServiceUnavailable
    raise Deploy::Errors::ServiceUnavailable
  when Net::HTTPForbidden, Net::HTTPUnauthorized
    raise Deploy::Errors::AccessDenied, "Access Denied for '#{Deploy.configuration.username}'"
  when Net::HTTPNotFound
    raise Deploy::Errors::CommunicationError, "Not Found at #{uri.to_s}"
  when Net::HTTPClientError
    false
  else
    raise Deploy::Errors::CommunicationError, http_result.body
  end
  self
end

#outputObject



16
17
18
# File 'lib/deploy/request.rb', line 16

def output
  @output || nil
end

#success?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/deploy/request.rb', line 12

def success?
  @success || false
end