Class: Niftycloud::Request

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/niftycloud/request.rb

Direct Known Subclasses

API

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



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

def endpoint
  @endpoint
end

#secret_access_keyObject

Returns the value of attribute secret_access_key.



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

def secret_access_key
  @secret_access_key
end

Class Method Details

.decode(response) ⇒ Object



32
33
34
35
36
37
# File 'lib/niftycloud/request.rb', line 32

def self.decode(response)
  p response 
  JSON.load response
rescue JSON::ParserError
  raise Error::Parsing.new "The response is not a valid JSON"
end

.parse(body) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/niftycloud/request.rb', line 14

def self.parse(body)
  body = REXML::Document.new(body).root.to_h["reservationSet"]

  if body.is_a? Hash
    ObjectifiedHash.new body
  elsif body.is_a? Array
    PaginatedResponse.new(body.collect! { |e| ObjectifiedHash.new(e) })
  elsif body
    body
  elsif !body
    false
  elsif body.nil?
    false
  else
    raise Error::Parsing.new "Couldn't parse a response body"
  end
end

Instance Method Details

#delete(path, options = {}) ⇒ Object



57
58
59
60
61
# File 'lib/niftycloud/request.rb', line 57

def delete(path, options={})
  set_httparty_config(options)
  set_authorization_header(options)
  validate self.class.delete(@endpoint + path, options)
end

#get(path, options = {}) ⇒ Object



39
40
41
42
43
# File 'lib/niftycloud/request.rb', line 39

def get(path, options={})
  set_httparty_config(options)
  set_authorization_header(options)
  validate self.class.get(@endpoint + path, options) 
end

#post(path, options = {}) ⇒ Object



45
46
47
48
49
# File 'lib/niftycloud/request.rb', line 45

def post(path, options={})
  set_httparty_config(options)
  set_authorization_header(options)
  validate self.class.post(@endpoint + path, options)
end

#put(path, options = {}) ⇒ Object



51
52
53
54
55
# File 'lib/niftycloud/request.rb', line 51

def put(path, options={})
  set_httparty_config(options)
  set_authorization_header(options)
  validate self.class.put(@endpoint + path, options)
end

#set_request_defaults(sudo = nil) ⇒ Object



85
86
87
88
89
# File 'lib/niftycloud/request.rb', line 85

def set_request_defaults(sudo=nil)
  self.class.default_params sudo: sudo
  raise Error::MissingCredentials.new("Please set an endpoint to API") unless @endpoint
  self.class.default_params.delete(:sudo) if sudo.nil?
end

#validate(response) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/niftycloud/request.rb', line 63

def validate(response)
  error_klass = case response.code
  when 400 then Error::BadRequest
  when 401 then Error::Unauthorized
  when 403 then Error::Forbidden
  when 404 then Error::NotFound
  when 405 then Error::MethodNotAllowed
  when 409 then Error::Conflict
  when 422 then Error::Unprocessable
  when 500 then Error::InternalServerError
  when 502 then Error::BadGateway
  when 503 then Error::ServiceUnavailable
  end

  fail error_klass.new(response) if error_klass

  parsed = response.parsed_response
  parsed.client = self if parsed.respond_to?(:client=)
  parsed.parse_headers!(response.headers) if parsed.respond_to?(:parse_headers!)
  parsed
end