Class: Deploy::Request
- Inherits:
-
Object
- Object
- Deploy::Request
- Defined in:
- lib/deploy/request.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(path, method = :get) ⇒ Request
constructor
A new instance of Request.
-
#make ⇒ Object
Make a request to the Deploy API using net/http.
- #output ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(path, method = :get) ⇒ Request
Returns a new instance of Request.
15 16 17 18 |
# File 'lib/deploy/request.rb', line 15 def initialize(path, method = :get) @path = path @method = method end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
13 14 15 |
# File 'lib/deploy/request.rb', line 13 def data @data end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
12 13 14 |
# File 'lib/deploy/request.rb', line 12 def method @method end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
11 12 13 |
# File 'lib/deploy/request.rb', line 11 def path @path end |
Instance Method Details
#make ⇒ Object
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.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/deploy/request.rb', line 30 def make uri = URI.parse([Deploy.configuration.account, @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) http.use_ssl = true if uri.scheme == 'https' 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 case http_result when Net::HTTPSuccess @success = true when Net::HTTPServiceUnavailable @success = raise Deploy::Errors::ServiceUnavailable when Net::HTTPForbidden, Net::HTTPUnauthorized @success = raise Deploy::Errors::AccessDenied, "Access Denied for '#{Deploy.configuration.username}'" when Net::HTTPNotFound @success = raise Deploy::Errors::CommunicationError, "Not Found at #{uri}" when Net::HTTPClientError @success = false else @success = raise Deploy::Errors::CommunicationError, http_result.body end self end |
#output ⇒ Object
24 25 26 |
# File 'lib/deploy/request.rb', line 24 def output @output || nil end |
#success? ⇒ Boolean
20 21 22 |
# File 'lib/deploy/request.rb', line 20 def success? @success || false end |