Class: MagicAdmin::Http::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/magic-admin/http/response.rb

Overview

Http Request and its methods are accessible on the Magic instance by the http_client.http_request attribute. It provides methods to interact with the http_request.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_resp) ⇒ Response

The constructor allows you to create HTTP Response Object when your application interacting with the Magic API

Arguments:

http_resp: Magic API response.

Returns:

A HTTP Response object that provides access to
all the supported resources.

Examples:

Response.new(<http_resp>)


59
60
61
62
63
# File 'lib/magic-admin/http/response.rb', line 59

def initialize(http_resp)
  @content = http_resp.body
  @data = JSON.parse(http_resp.body, symbolize_names: true)
  @status_code = http_resp.code.to_i
end

Instance Attribute Details

#contentObject (readonly)

attribute reader for response body



16
17
18
# File 'lib/magic-admin/http/response.rb', line 16

def content
  @content
end

#dataObject (readonly)

attribute reader for response data



13
14
15
# File 'lib/magic-admin/http/response.rb', line 13

def data
  @data
end

#status_codeObject (readonly)

attribute reader for response status_code



19
20
21
# File 'lib/magic-admin/http/response.rb', line 19

def status_code
  @status_code
end

Class Method Details

.from_net_http(http_resp, request) ⇒ Object

Description:

Method parse Magic API response

Arguments:

http_resp: Magic API response.
request: request object.

Returns:

A HTTP Response object or raise an error

Raises:

  • (error)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/magic-admin/http/response.rb', line 30

def self.from_net_http(http_resp, request)
  resp = Response.new(http_resp)
  error = case http_resp
          when Net::HTTPUnauthorized then AuthenticationError
          when Net::HTTPBadRequest then BadRequestError
          when Net::HTTPForbidden then ForbiddenError
          when Net::HTTPTooManyRequests then RateLimitingError
          when Net::HTTPServerError then APIError
          when Net::HTTPGatewayTimeout then APIError
          when Net::HTTPServiceUnavailable then APIError
          when Net::HTTPBadGateway then APIError
          end
  return resp unless error

  raise error.new(resp.data[:message], resp.error_opt(request))
end

Instance Method Details

#error_opt(request) ⇒ Object

Description:

Method provides you error info hash

Arguments:

request: request object.

Returns:

hash with following keys.
http_status:
status_code:
http_response:
http_message:
http_error_code:
http_request_params:
http_request_header:
http_method:


81
82
83
84
85
86
87
88
89
90
91
# File 'lib/magic-admin/http/response.rb', line 81

def error_opt(request)
  {
    http_status: data[:status],
    http_code: status_code,
    http_response: content,
    http_message: data[:message],
    http_error_code: data[:error_code],
    http_request_params: request.body,
    http_method: request.method
  }
end